%% Decimate a Signal Using a CICDecimator Object
% 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).
% Create a dsp.CICDecimator System object™ with DecimationFactor set to 4. Decimate a signal from 44.1 kHz to 11.025 kHz.

cicdec = dsp.CICDecimator(4);  
cicdec.FixedPointDataType = 'Minimum section word lengths'; 
cicdec.OutputWordLength = 16;

%
% Create a fixed-point sinusoidal input signal of 1024 samples, with a sampling frequency of 44.1e3 Hz.

Fs = 44.1e3;             
n = (0:1023)';            % 0.0232 sec signal
x = fi(sin(2*pi*1e3/Fs*n),true,16,15);

%
% Create a dsp.SignalSource object.

src = dsp.SignalSource(x,64);

%
% Decimate the output with 16 samples per frame.

y = zeros(16,16);
for ii = 1:16
     y(ii,:) = cicdec(src());   
end

%
% Plot the first frame of the original and decimated signals. Output latency is 2 samples.

gainCIC = ...
   (cicdec.DecimationFactor*cicdec.DifferentialDelay)^cicdec.NumSections;
stem(n(1:56)/Fs,double(x(4:59))) 
hold on;     
stem(n(1:14)/(Fs/cicdec.DecimationFactor),double(y(1,3:end))/gainCIC,'r','filled')
xlabel('Time (sec)')
ylabel('Signal Amplitude')
legend('Original signal','Decimated signal','Location','north')
hold off;

%
% Using the info method in 'long' format, obtain the word lengths and fraction lengths of the fixed-point filter sections and the filter output.

info(cicdec,'long')

% Copyright 2012 The MathWorks, Inc.