DSPRelated.com
Forums

CIC filter and compensation filter - help

Started by ronan June 24, 2008
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


On Jun 24, 11:16&#4294967295;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; &#4294967295; &#4294967295; &#4294967295;%decimation factor > N = 5000; &#4294967295; &#4294967295; %delay buffer depth > delayBuffer = zeros(1, N/R); > intOut1 = 0; &#4294967295; > intOut2 = 0; > combOut2 = 0; > comb2_delay = 0; > > n = 0:.01:N; > xn = sin(2*pi*(0:.1:N)); &#4294967295; %input &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; > > out = [ ]; &#4294967295; %output > > for ii = 1:length(xn) > > &#4294967295; &#4294967295; %integrator > &#4294967295; &#4294967295; intOut1 = intOut1 + xn(ii); > &#4294967295; &#4294967295; intOut2 = intOut2 + intOut1; > > &#4294967295; &#4294967295; if mod(ii,64)==1 > > &#4294967295; &#4294967295; &#4294967295; &#4294967295; %comb > &#4294967295; &#4294967295; &#4294967295; &#4294967295; combOut1 = intOut2 - delayBuffer(end); > &#4294967295; &#4294967295; &#4294967295; &#4294967295; delayBuffer(2:end) = delayBuffer(1:end-1); > &#4294967295; &#4294967295; &#4294967295; &#4294967295; delayBuffer(end) = intOut2; &#4294967295; > &#4294967295; &#4294967295; &#4294967295; &#4294967295; combOut2 = combOut1 - comb2_delay; > &#4294967295; &#4294967295; &#4294967295; &#4294967295; comb2_delay = combOut1; &#4294967295; &#4294967295; > &#4294967295; &#4294967295; &#4294967295; end > &#4294967295; &#4294967295; &#4294967295; out = [out combOut2]; &#4294967295; &#4294967295; &#4294967295; &#4294967295; > end > > After that, I designed a compensation FIR filter (inverse sinc) with the > following code: > > %%%%%% CIC filter parameters %%%%%% > R = 64; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;%% Decimation factor > M = 1; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;%% Differential delay > N = 2; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;%% Number of stages > B =12; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; %% Coeffi. Bit-width > Fs = 256e3; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;%% (High) Sampling freq in Hz before > decimation > Fc = 2000; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;%% Pass band edge in Hz > %%%%%%% fir2.m parameters %%%%%% > L = 20; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;%% Filter order; must be even > Fo = R*Fc/Fs; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; %% Normalized Cutoff freq; 0<Fo<=0.5/M; > &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; %% outside the pass band > > %%%%%%% CIC Compensator Design using fir2.m %%%%%% > p = 2e3; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;%% Granularity > s = 0.25/p; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; %% Step size > fp = [0:s:Fo]; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;%% Pass band frequency samples > fs = (Fo+s):s:0.5; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;%% Stop band frequency samples > f = [fp fs]*2; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;%% Normalized frequency samples; 0<=f<=1 > Mp = ones(1,length(fp)); &#4294967295; &#4294967295; &#4294967295; &#4294967295;%% 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); &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;%% Filter length L+1 > h = h/max(h); &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295; &#4294967295;%% Floating point coefficients > hz =round(h*power(2,B-1)-1); &#4294967295; %% 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
What 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
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