%% Compensation Decimator Design
% Note: If you are using R2016a or an earlier release, replace each call to the object with the equivalent step syntax. For example, obj(x) becomes  step(obj,x).
% Design a compensation decimator for an existing CIC decimator having six sections and a decimation factor of 6.

CICDecim = dsp.CICDecimator('DecimationFactor',6, ...
    'NumSections',6);

%
% Construct the compensation decimator. Specify a decimation factor of 2, an input sample rate of 16 kHz, a passband frequency of 4 kHz, and a stopband frequency of 4.5 kHz.

fs = 16e3;
fPass = 4e3;
fStop = 4.5e3;

CICCompDecim = dsp.CICCompensationDecimator(CICDecim, ...
    'DecimationFactor',2,'PassbandFrequency',fPass, ...
    'StopbandFrequency',fStop,'SampleRate',fs);

%
% Visualize the frequency response of the cascade. Normalize all magnitude responses to 0 dB.

filtCasc = dsp.FilterCascade(CICDecim,CICCompDecim);

f = fvtool(CICDecim, CICCompDecim, filtCasc, ...
    'Fs', [fs*6 fs fs*6]);

f.NormalizeMagnitudeto1 = 'on';
legend(f,'CIC Decimator','CIC Compensation Decimator', ...
    'Overall Response');

%
% Apply the design to a 1200-sample random input signal. Store the decimated output along the first dimension of the y array.

x = dsp.SignalSource(fi(rand(1200,1),1,16,15),'SamplesPerFrame',120);

y = fi(zeros(100,1),1,32,20);

for ind = 1:10
    x2 = CICDecim(x());
    y(((ind-1)*10)+1:ind*10,1) = CICCompDecim(x2);
end

% Copyright 2012 The MathWorks, Inc.