Implement CIC Decimation Filter for HDL

This example shows how to use the CIC Decimation HDL Optimized block to filter and downsample data. The example performs these steps:

  1. Generate frames of random input samples.

  2. Generate reference output data from the dsp.CICDecimation System object™.

  3. Run the Simulink model that contains the CIC Decimation HDL Optimized block.

  4. Compare the Simulink block output data with the reference data.

You can generate HDL code from the subsystem in the Simulink® model.

Set up input data parameters. The model uses these workspace variables to configure the CIC Decimation HDL Optimized block.

R = 8; % Decimation factor
M = 1; % Differential delay
N = 3; % Number of sections

Generate frames of random input samples. To generate reference output data for comparison, apply the samples to the dsp.CICDecimation System object.

framesize = R*randi([5 20],1,1);
dataSamples  = fi(randn(framesize,1),1,16,8);
obj = dsp.CICDecimator('DecimationFactor',R,'DifferentialDelay',M,'NumSections',N);
cicFcnOutput = step(obj,dataSamples ).';
release(obj);

Generate a stream of samples by converting frames to samples, and provide those samples (sampleIn) as input to the Simulink model along with the corresponding valid signal (validIn).

idlecyclesbetweensamples = 0;
idlecyclesbetweenframes = 0;

data = []; valid=[];
for ii = 1:length(dataSamples)
    data = [data dataSamples(ii) ...
        zeros(1,idlecyclesbetweensamples)];
    valid = [valid true(1,1) ...
        false(1,idlecyclesbetweensamples)];
 end

sampleIn = cast([data,zeros(1,idlecyclesbetweenframes)],'like',dataSamples);
validIn = logical([valid,zeros(1,idlecyclesbetweenframes)]);

sampletime = 1;
simTime = length(validIn);

Run the Simulink model.

modelname = 'HDLCICDecimationModel';
open_system(modelname);
sim(modelname);

Capture the output data from the Simulink model, and compare it against the output from the dsp.CICDecimation System object.

sampleOut = squeeze(sampleOut_ts.Data).';
validOut  = squeeze(validOut_ts.Data);
cicOutput = sampleOut(validOut);

fprintf('\nHDL CIC Decimation\n');
difference = (abs(cicOutput-cicFcnOutput(1:length(cicOutput)))>0);
fprintf('\nTotal number of samples differed between Simulink block output and MATLAB function output is: %d \n',sum(difference));
HDL CIC Decimation

Total number of samples differed between Simulink block output and MATLAB function output is: 0 

See Also

Blocks