# dsp.HDLCICDecimation

Decimate signal using cascaded integrator-comb filter — optimized for HDL code generation

## Description

The `dsp.HDLCICDecimation` System object™ decimates an input signal by using a cascaded integrator-comb (CIC) decimation filter. CIC filters are a class of linear phase FIR filters consisting of a comb part and an integrator part. The CIC decimation filter structure consists of N sections of cascaded integrators, a rate change factor of R, and then N sections of cascaded comb filters. For more information about CIC decimation filters, see Algorithms.

The System object supports fixed and variable decimation rates for scalar inputs and only fixed decimation for vector inputs. For both types of inputs, the System object provides a scalar output. The System object provides an architecture suitable for HDL code generation and hardware deployment.

The System object supports real and complex fixed-point inputs.

To filter input data with an HDL-optimized CIC decimation filter:

1. Create the `dsp.HDLCICDecimation` object and set its properties.

2. Call the object with arguments, as if it were a function.

## Creation

### Syntax

``cicDecFilt = dsp.HDLCICDecimation``
``cicDecFilt = dsp.HDLCICDecimation(Name,Value)``

### Description

````cicDecFilt = dsp.HDLCICDecimation` creates an HDL-optimized CIC decimation filter System object, `cicDecFilt`, with default properties.```

example

````cicDecFilt = dsp.HDLCICDecimation(Name,Value)` creates the filter with properties set using one or more name-value pairs. Enclose each property name in single quotes.```

## Properties

expand all

Unless otherwise indicated, properties are nontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and the `release` function unlocks them.

If a property is tunable, you can change its value at any time.

Set this property to `true` (`1`) to operate the System object with a variable decimation rate specified by the `decimFactor` input argument. Set this property to `false` `(0)` to operate the object with a fixed decimation rate, specified by the `DecimationFactor` property.

For vector inputs, the object does not support variable decimation rate.

Specify the decimation factor as an integer from 2 to 2048. This value represents the rate with which you want to decimate the input.

When you set the `VariableDownsample` property to `true`, this property sets the upper bound of the range of valid values for the `decimFactor` argument.

Specify the differential delay of the comb part of the filter as either `1` or `2` cycles.

Specify the number of sections in either the integrator or the comb part of the filter as an integer from 1 to 6.

Choose the data type of the filtered output data.

• `'Full precision'` — The output data type has a word length equal to the input word length plus gain bits.

• `'Same word length as input'` — The output data type has a word length equal to the input word length.

• `'Minimum section word lengths'` — The output data type uses the word length you specify in the OutputWordLength property. When you choose this option, the System object applies a Pruning algorithm internally. For more information about Pruning, see Output Data Type. This option is not supported when `VariableDownsample` is `true`.

Word length of the output, specified as an integer from 2 to 104.

Note

When this value is less than `7` the output data values might overflow.

#### Dependencies

To enable this property, set the `OutputDataType` property to `'Minimum section word lengths'`.

Set this property to `true` to compensate for the output gain of the filter.

Depending on the type of decimation you specify and the value of this property, the latency of the object changes. Here, N means the number of sections and vecLen means the length of the vector.

For a scalar input with fixed decimation (`VariableDownsample` is `false`):

• With gain correction off, the latency of the object is 3 + N clock cycles.

• With gain correction on, the latency of the object is 3 + N + 9 clock cycles.

For a scalar input with variable decimation (`VariableDownsample` is `true`):

• With gain correction off, the latency of the object is 4 + N clock cycles.

• With gain correction on, the latency of the object is 4 + N + 9 clock cycles.

For a vector input with fixed decimation (`VariableDownsample` is `false`):

• With gain correction off, the latency of the object is `floor`((vecLen – 1) * (N/vecLen)) + 1 + N + (2 + (vecLen + 1) * N clock cycles.

• With gain correction on, the latency of the object is `floor`((vecLen – 1) * (N/vecLen)) + 1 + N + (2 + (vecLen + 1) * N) + 9 clock cycles.

Note

For vector inputs, the object does not support variable decimation.

When you set this property to `true`, the System object expects a `reset` input argument.

## Usage

### Syntax

``[dataOut,validOut] = cicDecFilt(dataIn,validIn)``
``[dataOut,validOut] = cicDecFilt(dataIn,validIn,decimFactor)``
``[dataOut,validOut] = cicDecFilt(dataIn,validIn,reset)``
``[dataOut,validOut] = cicDecFilt(dataIn,validIn,decimFactor,reset)``

### Description

````[dataOut,validOut] = cicDecFilt(dataIn,validIn)` filters and decimates the input data using a fixed decimation factor only when `validIn` is `true`.```
````[dataOut,validOut] = cicDecFilt(dataIn,validIn,decimFactor)` filters the input data using the specified variable decimation factor, `decimFactor`. The `VariableDownsample` property must be set to `true`. ```
````[dataOut,validOut] = cicDecFilt(dataIn,validIn,reset)` filters the input data when `reset` is `false` and clears filter internal states when `reset` is `true`. The System object expects the `reset` argument only when you set the ResetIn property to `true`.```
````[dataOut,validOut] = cicDecFilt(dataIn,validIn,decimFactor,reset)` filters the input data when `reset` is `false` and clears filter internal states when `reset` is `true`. The System object expects the `reset` argument only when you set the ResetIn property to `true`. The `VariableDownsample` property is set to `true`.```

### Input Arguments

expand all

Specify input data as a scalar or a column vector of length 1 to 64. The input data must be a signed integer or signed fixed point with a word length less than or equal to 32. `DecimationFactor` property must be an integer multiple of the input frame size.

Data Types: `int8` | `int16` | `int32` | `fi`
Complex Number Support: Yes

Control signal that indicates if the input data is valid.

When `validIn` is `1` (`true`), the System object captures the value from the `dataIn` input argument. When `validIn` is `0` (`false`), the System object ignores the `dataIn` input value.

Data Types: `logical`

Specifies the decimation rate.

The `decimFactor` value must be of data type `ufix12` data type and an integer in the range from 2 to the `DecimationFactor` property value.

#### Dependencies

To enable this argument, set the VariableDownsample property to `true`.

Data Types: `fi(0,12,0)`

Clear internal states, specified as a logical scalar.

When this value is `1` (`true`), the System object stops the current calculation and clears all internal states. When this value is `0` (`false`) and `validIn` is `1` (`true`), the System object starts a new filtering operation.

#### Dependencies

To enable this argument, set the ResetIn property to `true`.

Data Types: `logical`

### Output Arguments

expand all

CIC decimated output data, returned as a scalar.

The OutputDataType property sets the output data type of this argument. See OutputDataType.

Data Types: `int8` | `int16` | `int32` | `fi`
Complex Number Support: Yes

Control signal that indicates if the data from the `dataOut` output argument is valid. When this value is `1` (`true`), the System object returns valid data from the `dataOut` output argument. When this value is `0` (`false`), the values of the `dataOut` output argument are not valid.

Data Types: `logical`

## Object Functions

To use an object function, specify the System object as the first input argument. For example, to release system resources of a System object named `obj`, use this syntax:

`release(obj)`

expand all

 `getLatency` Latency of CIC decimation filter
 `step` Run System object algorithm `release` Release resources and allow changes to System object property values and input characteristics `reset` Reset internal states of System object

## Examples

collapse all

This example shows how to use a `dsp.HDLCICDecimation` System object™ to filter and downsample the data. This object supports scalar and vector inputs. In this example, two functions are provided to work with scalar and vector input data separately. You can generate the HDL code from these functions.

Generate Frames of Random Input Samples

Set up these workspace variables for the object to use. The object supports fixed and variable decimation rates for scalar inputs and only a fixed decimation rate for vector inputs. The example runs the `HDLCIC_maxR8` function when you set the scalar variable to `true` and runs the `HDLCIC_vec` function when you set the scalar variable to `false`. For scalar inputs, choose a range of the input varRValue values and set the decimation factor value, R, to the maximum expected decimation factor. For vector inputs, the input data must be a column vector of size 1 to 64 and R must be an integer multiple of the input frame size.

```R = 8; % decimation factor M = 1; % differential delay N = 3; % number of sections scalar = false; % true for scalar; false for vector if scalar varRValue = [4, 5, 6, 7, 8]; vecSize = 1; else varRValue = R; vecSize = randsrc(1,1,factor(R)); end numFrames = length(varRValue); dataSamples = cell(1,numFrames); varRtemp = cell(1,numFrames); framesize = zeros(1,numFrames); refOutput = []; WL = 0; % Word length FL = 0; % Fraction length ```

Generate Reference Output from `dsp.CICDecimation` System Object™

Generate frames of random input samples and apply the samples to the `dsp.CICDecimation` System object. The output generated from this System object is used as a reference data for comparison. The System object does not support a variable decimation rate, so you must create and release the object for each change in decimation factor value.

```totalsamples = 0; for i = 1:numFrames framesize(i) = varRValue(i)*randi([5 20],1,1); dataSamples{i} = fi(randn(vecSize,framesize(i)),1,16,8); ref_cic = dsp.CICDecimator('DifferentialDelay',M,... 'NumSections',N,... 'DecimationFactor',varRValue(i)); refOutput = [refOutput,ref_cic(dataSamples{i}(:)).']; release(ref_cic); end ```

Run a Function that contains `dsp.HDLCICDecimation` System Object

Set the properties of the System object to match the input data parameters and run the respective function based on the input type. These functions operate on a stream of data samples rather than a frame. You can generate HDL code from these functions.

The example uses the `HDLCIC_maxR8` function for a scalar input.

```function [dataOut,validOut] = HDLCIC_maxR8(dataIn,validIn,R) %HDLCIC_maxR8 % Performs CIC decimation with an input decimation factor up to 8. % sampleIn is a scalar fixed-point value. % validIn is a logical scalar value. % You can generate HDL code from this function. persistent cic8; if isempty(cic8) cic8 = dsp.HDLCICDecimation('DecimationFactor',8,... 'VariableDownsample',true,... 'DifferentialDelay',1,... 'NumSections',3); end [dataOut,validOut] = cic8(dataIn,validIn,R); end ```

The example uses the `HDLCIC_vec` function for a vector input.

```function [dataOut,validOut] = HDLCIC_vec(dataIn,validIn) %HDLCIC_vec % Performs CIC decimation with an input vector. % sampleIn is a fixed-point vector. % validIn is a logical scalar value. % You can generate HDL code from this function. persistent cicVec; if isempty(cicVec) cicVec = dsp.HDLCICDecimation('DecimationFactor',8,... 'VariableDownsample',false,... 'DifferentialDelay',1,... 'NumSections',3); end [dataOut,validOut] = cicVec(dataIn,validIn); end ```

To flush remaining data, run the object by inserting the required number of idle cycles after each frame through `latency` variable. For more information, see GainCorrection property.

Initialize the output to a size large enough to accommodate the output data. The final size is expected to be smaller than `totalsamples` due to decimation.

```latency = floor((vecSize - 1)*(N/vecSize))+ 1+ N +(2+(vecSize + 1)*N)+ 9; dataOut = zeros(1,totalsamples+numFrames*latency); validOut = zeros(1,totalsamples+numFrames*latency); idx=0; for ij = 1:numFrames if scalar % scalar input with variable decimation for ii = 1:length(dataSamples{ij}) idx = idx+1; [dataOut(idx),validOut(idx)] = HDLCIC_maxR8(... dataSamples{ij}(ii),... true,... fi(varRValue(ij),0,12,0)); end for ii = 1:latency idx = idx+1; [dataOut(idx),validOut(idx)] = HDLCIC_maxR8(... fi(0,1,16,8),... false,... fi(varRValue(ij),0,12,0)); end else % vector input with fixed decimation for ii = 1:size(dataSamples{ij},2) idx = idx+1; [dataOut(idx),validOut(idx)] = HDLCIC_vec(... dataSamples{ij}(:,ii),... true); end for ii = 1:latency idx = idx+1; [dataOut(idx),validOut(idx)] = HDLCIC_vec(... fi(zeros(vecSize,1),1,16,8),... false); end end end ```

Compare the Function Output with the Reference Data

Compare the function results against the output from the `dsp.CICDecimation` object.

```cicOutput = dataOut(validOut==1); fprintf('\nHDL CIC Decimation\n'); difference = (abs(cicOutput-refOutput(1:length(cicOutput)))>0); fprintf('\nTotal number of samples differed between Behavioral and HDL simulation: %d \n',sum(difference)); ```
```HDL CIC Decimation Total number of samples differed between Behavioral and HDL simulation: 0 ```

The latency of the `dsp.HDLCICDecimation` System object™ varies depending on how many integrator and comb sections your filter has, input vector size, and whether you enable gain correction. Use the `getLatency` function to find the latency of a particular filter configuration. The latency is the number of cycles between the first valid input and the first valid output, assuming the input is continuously valid.

Create a `dsp.HDLCICDecimation` System object™ and request the latency. The default filter has two sections, and gain correction is disabled.

`hdlcic = dsp.HDLCICDecimation`
```hdlcic = dsp.HDLCICDecimation with properties: VariableDownsample: false DecimationFactor: 2 DifferentialDelay: 1 NumSections: 2 OutputDataType: 'Full precision' GainCorrection: false ResetIn: false ```
`L_def = getLatency(hdlcic)`
```L_def = 5 ```

Modify the filter object to have three integrator and comb sections. Check the resulting change in latency.

```hdlcic.NumSections = 3; L_3sec = getLatency(hdlcic)```
```L_3sec = 6 ```

Enable the gain correction on the filter object with vector input size 2. Check the resulting change in latency.

```hdlcic.GainCorrection = true; vecSize = 2; L_wgain = getLatency(hdlcic,vecSize)```
```L_wgain = 25 ```

expand all

## References

[1] Hogenauer, E. “An Economical Class of Digital Filters for Decimation and Interpolation.” IEEE Transactions on Acoustics, Speech, and Signal Processing 29, no. 2 (April 1981): 155–62. https://doi.org/10.1109/TASSP.1981.1163535.

Introduced in R2019b

Watch now