V1

2022/01/06阅读：99主题：默认主题

# CIC滤波器模型验证

• 验证修改后的CIC模型的正确；
• 生成后期RTL调试所需的原始数据及对比数据。

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  Author       :     Scat Lin%  Function     :     cic_module_front_to_back.m%  Description  :     cic滤波器模型,5级CIC，16倍降采样，积分器从前向后计算%  Inputs       :%                     cic 滤波器%                     x input signal%                     N 滤波器级数%                     M 滤波器阶数%                     Dv 抽值倍数%                     bit_num 输入信号位数%%  Time         ：    2021/12/11  15:19%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function y = cic_module_front_to_back(x,N,M,Dv,bit_num)    %% parameters    Inte_reg = zeros(1,N);    Comb_reg = zeros(1,N);    Signal_length = length(x);    y = zeros(1,fix(Signal_length/Dv));    ov_bit_num = bit_num + N * log2(M*Dv);%   ov_bit_num = 52;        ctrl = 1;  % 位数钳制控制        %% CIC    for loop_S = 1 : Signal_length        sum_temp = Inte_reg(1,1) + x(loop_S);        Inte_reg(1,1) = binary_num_clam(sum_temp,ov_bit_num,ctrl);        for loop_N = 2 : 1 : N            sum_temp = Inte_reg(1,loop_N) + Inte_reg(1,loop_N-1);            Inte_reg(1,loop_N) = binary_num_clam(sum_temp,ov_bit_num,ctrl);   % 钳制信号位数        end                Comb_temp = Inte_reg(1,N);        if mod(loop_S,Dv) == 0            for loop_N = 1:N                temp_reg  = Comb_temp;                sum_temp = Comb_temp - Comb_reg(1,loop_N);                Comb_temp = binary_num_clam(sum_temp,ov_bit_num,ctrl);                Comb_reg(1,loop_N) = temp_reg;            end            y(ceil(loop_S/Dv)) = Comb_temp;        end    end

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  Author       :     Scat Lin%  Function     :     cic_module_back_to_front.m%  Description  :     cic滤波器模型,5级CIC，16倍降采样，积分器从后向前计算%  Inputs       :%                     cic 滤波器%                     x input signal%                     N 滤波器级数%                     M 滤波器阶数%                     Dv 抽值倍数%                     bit_num 输入信号位数%%  Time         ：    2021/12/11  15:37%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function y = cic_module_back_to_front(x,N,M,Dv,bit_num)     %% parameters    Inte_reg = zeros(1,N);    Comb_reg = zeros(1,N);    Signal_length = length(x);    y = zeros(1,fix(Signal_length/Dv));    ov_bit_num = bit_num + N * log2(M*Dv);%   ov_bit_num = 52;        ctrl = 1;  % 位数钳制控制        %% CIC    for loop_S = 1 : Signal_length        for loop_N = N : -1 : 2            sum_temp = Inte_reg(1,loop_N) + Inte_reg(1,loop_N-1);            Inte_reg(1,loop_N) = binary_num_clam(sum_temp,ov_bit_num,ctrl);   % 钳制信号位数        end        sum_temp = Inte_reg(1,1) + x(loop_S);        Inte_reg(1,1) = binary_num_clam(sum_temp,ov_bit_num,ctrl);                Comb_temp = Inte_reg(1,N);        if mod(loop_S,Dv) == 0            for loop_N = 1:N                temp_reg  = Comb_temp;                sum_temp = Comb_temp - Comb_reg(1,loop_N);                Comb_temp = binary_num_clam(sum_temp,ov_bit_num,ctrl);                Comb_reg(1,loop_N) = temp_reg;            end            y(ceil(loop_S/Dv)) = Comb_temp;        end    end

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  Author       :     Scat Lin%  Function     :     binary_num_clam.m%  Description  :     钳制二进制位数%  Inputs       :     %                     dec_num  输入十进制数据%                     bit_num  钳制位数%                     ctrl     控制，默认为1，在为0时不进行位数钳制%  Example      :     钳制位数为 4%                    -->  binary_num_clam(5,4)  =  5%                            5 = '0101'%                    -->  binary_num_clam(25,4) = -7%                            25 = '011001'%                    -->  binary_num_clam(-5,4)  =  5%                            -5 = '1011'%                    -->  binary_num_clam(-25,4)  =  7%                            -25 = '100111'%%  Time         ：    2021/12/18  15:02%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function bin_sum = binary_num_clam(dec_num,bit_num,ctrl)        ctrl_t = 1;    if nargin == 3        ctrl_t = ctrl;    end        if ctrl_t == 0        bin_sum = dec_num;        return;    end        if dec_num >= 0        bin_num = dec2bin(dec_num);        if length(bin_num) < bit_num            bin_sum = dec_num;        else            bit_temp = bin_num(end - bit_num + 1 : end);            if bit_temp(1) == '1'                bin_sum = bin2dec(bit_temp) - power(2,bit_num);            else                bin_sum = bin2dec(bit_temp);            end        end    else        if dec_num < -power(2,bit_num-1)            cnt_bit = bit_num;            while dec_num < -power(2,cnt_bit)                cnt_bit = cnt_bit + 1;            end            bin_num = dec2bin(power(2,cnt_bit)+dec_num,cnt_bit);            bit_temp = bin_num(end - bit_num + 1 : end);            if bit_temp(1) == '1'                bin_sum = bin2dec(bit_temp) - power(2,bit_num);            else                bin_sum = bin2dec(bit_temp);            end        else            bin_sum = dec_num;        end    end

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  Author       :     Scat Lin%  Function     :     test_cic_filter.m%  Description  :     用于测试cic滤波器,5级CIC，16倍降采样%%  ---> 测试CIC滤波器模型；%  ---> 测试CIC滤波器RTL：%                1、生成十六进制测试序列%                2、生成十六进制对照序列%                3、读取生成序列序列，,matlab分析%%  Time         ：    2021/12/11  15:19%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%clear,clc%% control sel%--> 0 : 对CIC的matlab模型验证%--> 1 : 生成待测试的数据，十六进制 + testbench校验用的数据，十六进制ctrl_sel = 0 ;     %% parameters cycle_num =    40;      % 仿真周期数，信号为正弦波N_bit     =    8;       % 数据位数,二进制fs        =    256000;  % 过采样，采样率fz        =    100;     % 信号的频率wr_addr_s =    'D:\work\Study\CIC\signal_quantified.txt';   %% 没有经过CIC下采样滤波的信号wr_addr_n =    'D:\work\Study\CIC\signal_aftercic.txt';   %% 经过CIC下采样滤波的信号,来来进行testbench比较%% 生成采样信号% 带符号数据time_s       =   0 : 1/fs : (((cycle_num*fs)/fz)-1)/fs;y_initial    =   sin(2*pi*fz*time_s);y_quantified =   fix((power(2,N_bit-1)-1).*y_initial);if ctrl_sel == 1    % 输出txt文件    fid = fopen(wr_addr_s,'wt');    for loopi = 1 : length(y_quantified)        % 十进制转带符号十六进制(二进制)        if y_quantified(loopi) < 0            temp_hex = dec2hex(power(2,N_bit)+y_quantified(loopi),ceil(N_bit/4));        else            temp_hex = dec2hex(y_quantified(loopi),ceil(N_bit/4));        end        fprintf(fid,'%s\n',temp_hex);    end    fclose(fid);end% display quantified signalfigure(1) plot(time_s,y_quantified);title('原数据波形')        %% matlab 模型验证 or 生成测试用比较用例% 与matlab自带的cic模型进行比较% 对于输入到原型滤波器的数据，需要先加入四个延时if (ctrl_sel == 0) || ( ctrl_sel == 1 )    matlab_y = cic_module_back_to_front(y_quantified,5,1,16,N_bit);    y        = cic_module_front_to_back([zeros(1,4),y_quantified(1:end-4)],5,1,16,N_bit);    figure(2)     plot(downsample(time_s,16),y','r',downsample(time_s,16),matlab_y,'k');    figure(3)    plot(downsample(time_s,16),matlab_y,'r');    title('CIC滤波数据波形')    figure(4)    plot(downsample(time_s,16),y,'r');    title('CIC滤波数据波形')end%% % 信号经过cic滤波之后的数据if ctrl_sel == 1    % 输出txt文件    fid = fopen(wr_addr_n,'wt');    for loopi = 1 : length(y)        % 十进制转带符号十六进制(二进制)        if y(loopi) < 0            temp_hex = dec2hex(power(2,CIC_bit)+y(loopi),ceil(CIC_bit/4));        else            temp_hex = dec2hex(y(loopi),ceil(CIC_bit/4));        end        fprintf(fid,'%s\n',temp_hex);    end    fclose(fid);endif ctrl_sel == 0    % 模型验证比较    cmp_y_m = matlab_y - y;    figure    plot(cmp_y_m);        if sum(cmp_y_m) == 0        fprintf('模型是正确的！！！！\n');    else        fprintf('模型验证失败\n');    endend

V1