DSPRelated.com
Code

CIC Filter Analysis script

October 27, 20106 comments Coded in Matlab

A Matlab script written to aid in the design and analysis of CIC decimation filters. The script plots the frequency response of the filter, as well as the response of a matching FIR compensation filter (designed using fir2()). Users can edit the script to specify downsampling factor, CIC filter order, compensation filter length, and other crucial parameters. All parameters are clearly labeled in code comments.

Floating point and fixed point compensation filter coefficients are stored in the Matlab workspace when the script finishes executing. Floating point coefficients are stored in the array h, and fixed point coefficients are stored in the array hz.

Please feel free to post any feedback or questions in the comments section below.

%==========================================================================
% CIC Filter Analysis Script
%
% Compute CIC filter frequency response, compensation filter coefficients,
% and compensated frequency response for a given set of design paramenters.
%
%
%   Author:  Tom Chatt
%
%   Revision History:
%==========================================================================
%   09/23/2010              Tom Chatt                   Initial Version
%==========================================================================

clear all; close all; clc;

%=============================
% CIC Filter Design Parameters
%=============================
N = 3;                     % Sinc/Comb-Filter order
M = 1;                     % Stage delay (should be 1 or 2).
D = 64;                    % Decimation factor
fs = 10e6;                 % Pre-decimation sampling Frequency
fs_lo = fs / D;            % Post-decimation sampling frequency

%==============================
% Compensator Design Parameters
%==============================

B = 12; % Compensator coefficient bit width

% fir2 parameters
fc = fs/D/2;      % Ideal cutoff frequency
L = 16;           % Filter order - 1. Must be even. Final filter length is L + 1.
Fo = D*fc/fs;     % Normalized cutoff frequency

%=====================================================
% Generate and plot filters
%
% Floating point compensator filter coef. stored in h
% Fixed point compensator filter coef. stored in hz
%
%======================================================

% CIC Compensator using fir2
p = 2e3;                    % Granularity
s = 0.25 / p;               % Step size
fpass = 0:s:Fo;             % Pass band frequency samples
fstop = (Fo + s) : s : 0.5; % Stop band frequency samples
ff = [fpass fstop] * 2;     % Normalized frequency samples, 0 <= f <= 1

Mp = ones(1,length(fpass)); %% Pass band response; Mp(1)=1
Mp(2:end) = abs( M*D*sin(pi*fpass(2:end)/D)./sin(pi*M*fpass(2:end))).^N;
Mf = [Mp zeros(1,length(fstop))];
ff(end) = 1;
h = fir2(L,ff,Mf); %% Filter length L+1
h = h/max(h); %% Floating point coefficients
hz = round(h*power(2,B-1)-1)/(2^(B-1)); %% Fixed point coefficients

f = linspace(0,1,4*length(ff));
f_lo = [ff ff+1 ff+2 ff+3];
H = (D^-N*abs(D*M*sin(pi*M*D*f) ./ (pi*M*D*f)).^N);
HH = (D^-N*abs(D*M*sin(pi*M*ff) ./ (pi*M*ff)).^N);
HHH = (D^-N*abs(D*M*sin(pi*M*f_lo) ./ (pi*M*f_lo)).^N);

H_comp = abs(fft(hz,length(ff)));

%=========
% Plotting
%=========

figure('name','CIC Frequency Response','Numbertitle','off');
subplot(211);
plot(f*fs, db(H), 'b');
grid on;
title([{'Wideband frequency response of CIC filter'};{sprintf('D = %i, N = %i',D,N)}]);
xlabel('Frequency (Hz)');
ylabel('|H(e^{j\omega})| (dB)');
axis([0,fs,-1250,0]);

subplot(212);
plot(f_lo*fs_lo, db(HHH));
grid on;
title('Narrowband frequency response of CIC filter');
xlabel('Frequency (Hz)');
ylabel('|H(e^{j\omega})| (dB)');
axis([0,4*fs_lo,-200,5]);

figure('name','CIC with Compensator Filter','Numbertitle','off');
subplot(211);
hold on;
grid on;
plot(f_lo*fs_lo, db(HHH), 'b');
plot(f_lo*fs_lo, db([H_comp H_comp H_comp H_comp]), 'g');
plot(f_lo*fs_lo, db(HHH .* [H_comp H_comp H_comp H_comp]), 'r');
axis([0,4*fs_lo,-200,25])
title([{'CIC with Compensator FIR'};{sprintf('Compensator length = %i, Coefficient precision = %i',L,B)}]);
xlabel('Frequency (Hz)');
ylabel('|H(e^{j\omega})| (dB)');

subplot(212);
hold on;
grid on;
plot(ff*fs_lo, HH, 'b');
plot(ff*fs_lo, H_comp, 'g');
plot(ff*fs_lo, HH .* H_comp, 'r');
axis([0,1*fs_lo,0,1.1]);
title('Closeup of CIC + compensator passband');
xlabel('Frequency (Hz)');
ylabel('|H(e^{j\omega})|');
legend('CIC','Compensator Filter','Net Response','location','northeast');