Hi. I'm trying to design a CIC filter wich has decimator factor (R=64), differencial delay (M=1), number of stages (N=2) and sampling frequency (Fs=256kHz). I used the following matlab code to simulate its behavior: R = 64; %decimation factor N = 5000; %delay buffer depth delayBuffer = zeros(1, N/R); intOut1 = 0; intOut2 = 0; combOut2 = 0; comb2_delay = 0; n = 0:.01:N; xn = sin(2*pi*(0:.1:N)); %input out = [ ]; %output for ii = 1:length(xn) %integrator intOut1 = intOut1 + xn(ii); intOut2 = intOut2 + intOut1; if mod(ii,64)==1 %comb combOut1 = intOut2 - delayBuffer(end); delayBuffer(2:end) = delayBuffer(1:end-1); delayBuffer(end) = intOut2; combOut2 = combOut1 - comb2_delay; comb2_delay = combOut1; end out = [out combOut2]; end After that, I designed a compensation FIR filter (inverse sinc) with the following code: %%%%%% CIC filter parameters %%%%%% R = 64; %% Decimation factor M = 1; %% Differential delay N = 2; %% Number of stages B =12; %% Coeffi. Bit-width Fs = 256e3; %% (High) Sampling freq in Hz before decimation Fc = 2000; %% Pass band edge in Hz %%%%%%% fir2.m parameters %%%%%% L = 20; %% Filter order; must be even Fo = R*Fc/Fs; %% Normalized Cutoff freq; 0<Fo<=0.5/M; %% outside the pass band %%%%%%% CIC Compensator Design using fir2.m %%%%%% p = 2e3; %% Granularity s = 0.25/p; %% Step size fp = [0:s:Fo]; %% Pass band frequency samples fs = (Fo+s):s:0.5; %% Stop band frequency samples f = [fp fs]*2; %% Normalized frequency samples; 0<=f<=1 Mp = ones(1,length(fp)); %% Pass band response; Mp(1)=1 Mp(2:end) = abs( M*R*sin(pi*fp(2:end)/R)./sin(pi*M*fp(2:end))).^N; Mf = [Mp zeros(1,length(fs))]; f(end) = 1; h = fir2(L,f,Mf); %% Filter length L+1 h = h/max(h); %% Floating point coefficients hz =round(h*power(2,B-1)-1); %% Fixed point coefficients **** The problem: when I simulate the systemC code, the putput signal of the CIC is a sine signal, because the input is a sine signal, but the output of the compensation filter is not a sine signal. I use 12 bits for CIC and FIR filter. The compensation filter decimates by 2. I don't know whats wrong. Is it everything wrong? Could anybody help me? Please! Ronan
CIC filter and compensation filter - help
Started by ●June 24, 2008
Reply by ●June 25, 20082008-06-25
On Jun 24, 11:16�pm, "ronan" <ronancri...@hotmail.com> wrote:> Hi. > > I'm trying to design a CIC filter wich has decimator factor (R=64), > differencial delay (M=1), number of stages (N=2) and sampling frequency > (Fs=256kHz). I used the following matlab code to simulate its behavior: > > R = 64; � � �%decimation factor > N = 5000; � � %delay buffer depth > delayBuffer = zeros(1, N/R); > intOut1 = 0; � > intOut2 = 0; > combOut2 = 0; > comb2_delay = 0; > > n = 0:.01:N; > xn = sin(2*pi*(0:.1:N)); � %input � � � � � � � � � � � � � � � � � � � � > > out = [ ]; � %output > > for ii = 1:length(xn) > > � � %integrator > � � intOut1 = intOut1 + xn(ii); > � � intOut2 = intOut2 + intOut1; > > � � if mod(ii,64)==1 > > � � � � %comb > � � � � combOut1 = intOut2 - delayBuffer(end); > � � � � delayBuffer(2:end) = delayBuffer(1:end-1); > � � � � delayBuffer(end) = intOut2; � > � � � � combOut2 = combOut1 - comb2_delay; > � � � � comb2_delay = combOut1; � � > � � � end > � � � out = [out combOut2]; � � � � > end > > After that, I designed a compensation FIR filter (inverse sinc) with the > following code: > > %%%%%% CIC filter parameters %%%%%% > R = 64; � � � � � � � � � � � � �%% Decimation factor > M = 1; � � � � � � � � � � � � �%% Differential delay > N = 2; � � � � � � � � � � � � �%% Number of stages > B =12; � � � � � � � � � � � � %% Coeffi. Bit-width > Fs = 256e3; � � � � � � � � �%% (High) Sampling freq in Hz before > decimation > Fc = 2000; � � � � � � � � � �%% Pass band edge in Hz > %%%%%%% fir2.m parameters %%%%%% > L = 20; � � � � � � � � � � � �%% Filter order; must be even > Fo = R*Fc/Fs; � � � � � � � � � %% Normalized Cutoff freq; 0<Fo<=0.5/M; > � � � � � � � � � � � � � � � � %% outside the pass band > > %%%%%%% CIC Compensator Design using fir2.m %%%%%% > p = 2e3; � � � � � � � � � � � �%% Granularity > s = 0.25/p; � � � � � � � � � � %% Step size > fp = [0:s:Fo]; � � � � � � � � �%% Pass band frequency samples > fs = (Fo+s):s:0.5; � � � � � � �%% Stop band frequency samples > f = [fp fs]*2; � � � � � � � � �%% Normalized frequency samples; 0<=f<=1 > Mp = ones(1,length(fp)); � � � �%% Pass band response; Mp(1)=1 > Mp(2:end) = abs( M*R*sin(pi*fp(2:end)/R)./sin(pi*M*fp(2:end))).^N; > Mf = [Mp zeros(1,length(fs))]; > f(end) = 1; > h = fir2(L,f,Mf); � � � � � � �%% Filter length L+1 > h = h/max(h); � � � � � � � � �%% Floating point coefficients > hz =round(h*power(2,B-1)-1); � %% Fixed point coefficients > > **** The problem: when I simulate the systemC code, the putput signal of > the CIC is a sine signal, because the input is a sine signal, but the > output of the compensation filter is not a sine signal. I use 12 bits for > CIC and FIR filter. The compensation filter decimates by 2. > > I don't know whats wrong. Is it everything wrong? > > Could anybody help me? Please! > > RonanWhat you could do is use the MATLAB functions for CIC filter (that means you will create a filter object) and then create a FIR filter object with the coeff that you generated with the code(I think you got that from ALTERA am I right?) you showed us (dont forget to create it as a multirate filter since it will decimate by 2) and then cascade them with the matlab function. I dont know much about systemC, but when I had to implement a system like these one I used IPcores from Xilinx. I believe ALTERA has better filter IPcores. Good Luck Alfredo
Reply by ●June 25, 20082008-06-25
A CIC compensation filter is not an inverse filter. Rather, it compensates for CIC "droop" which is the roll-off shape of the CIC filter response. A good appoach is to use the remez exchange algorithm to build a FIR filter. For debugging, plot your filter responses and take note to the gain at the frequencies of interest. In MATLAB you can use the freqz function. -sludge