%% Design Compensation Decimator for CIC Decimator
% Create a CIC decimator. Cascade the decimator with a gain.

cicdecim = dsp.CICDecimator('DecimationFactor',6,...
    'NumSections',6);
decimcasc = dsp.FilterCascade(cicdecim,1/gain(cicdecim));

%
% Design a compensation decimator and cascade it with the filter cascade, decimcasc. This operation nests a dsp.FilterCascade object as a stage in another filter cascade. The CIC compensation decimator has an inherent gain, gain(cicdecim). The factor of 1/gain(cicdecim) from the decimation filter cascade, decimcasc, compensates for the compensation filter gain.

fs = 16e3;     % Sampling frequency of input of compensation decimator
fPass = 4e3;   % Passband frequency
fStop = 4.5e3; % Stopband frequency
ciccomp = dsp.CICCompensationDecimator(cicdecim,...
    'DecimationFactor',2, ...
    'PassbandFrequency',fPass, ...
    'StopbandFrequency',fStop, ...
    'SampleRate',fs);
filtchain = dsp.FilterCascade(decimcasc,ciccomp);

%
% Visualize the frequency response of the cascade of cascades.

f = fvtool(decimcasc,ciccomp,filtchain,'Fs',[fs*6,fs,fs*6],...
    'Arithmetic','fixed');
legend(f,'CIC Decimator','CIC Compensation Decimator',...
    'Overall Response');

%
% Copyright 2012 The MathWorks, Inc.