Main Content

generateFilteringCode

Generate MATLAB code for a filter cascade

Description

generateFilteringCode(FC) creates a MATLAB® function that contains code to create the stages of a filter cascade, FC, and calls each stage in sequence. If the filters in each stage support code generation, you can generate C/C++ code from the function returned by generateFilteringCode.

example

generateFilteringCode(FC,fileName) generates code and saves the resulting function to the file specified in fileName.

Examples

collapse all

Design a two-stage decimator with a 100-Hz transition width, a 2-kHz sampling frequency, and 60-dB attenuation in the stopband. The decimator needs to downsample by a factor of 4.

filtCasc = designMultistageDecimator(4,2000,100,60)
filtCasc = 
  dsp.FilterCascade with properties:

         Stage1: [1x1 dsp.FIRDecimator]
         Stage2: [1x1 dsp.FIRDecimator]
    CloneStages: false

Verify your design by using fvtool.

 info(filtCasc)
ans = 
    'Discrete-Time Filter Cascade
     ----------------------------
     Number of stages: 2
     Stage cloning: disabled
     
     Stage1: dsp.FIRDecimator
     -------
     Discrete-Time FIR Multirate Filter (real)               
     -----------------------------------------               
     Filter Structure   : Direct-Form FIR Polyphase Decimator
     Decimation Factor  : 2                                  
     Polyphase Length   : 10                                 
     Filter Length      : 19                                 
     Stable             : Yes                                
     Linear Phase       : Yes (Type 1)                       
                                                             
     Arithmetic         : double                             
     
     
     Stage2: dsp.FIRDecimator
     -------
     Discrete-Time FIR Multirate Filter (real)               
     -----------------------------------------               
     Filter Structure   : Direct-Form FIR Polyphase Decimator
     Decimation Factor  : 2                                  
     Polyphase Length   : 18                                 
     Filter Length      : 35                                 
     Stable             : Yes                                
     Linear Phase       : Yes (Type 1)                       
                                                             
     Arithmetic         : double                             
     
     '

 fvtool(filtCasc)

Figure Figure 1: Magnitude Response (dB) contains an axes object. The axes object with title Magnitude Response (dB), xlabel Frequency (Hz), ylabel Magnitude (dB) contains 2 objects of type line.

Generate code to filter data using this design. You cannot generate C/C++ code from the dsp.FilterCascade object directly, but you can generate C/C++ code from the generated function. The function defines the filter stages and calls them in sequence. The function is saved in a file called myDecimator.m in the current directory.

 generateFilteringCode(filtCasc,'myDecimator');

The myDecimator function creates a filter cascade and calls each stage object in turn.

 type myDecimator
function y = myDecimator(x)
%MYDECIMATOR Construct a filter cascade and process its stages

% MATLAB Code
% Generated by MATLAB(R) 23.2 and DSP System Toolbox 23.2.
% Generated on: 19-Aug-2023 11:58:12

% To generate C/C++ code from this function use the codegen command.
% Type 'help codegen' for more information.
%#codegen

%% Construction
persistent firdn1 firdn2
if isempty(firdn1)
    firdn1 = dsp.FIRDecimator(  ...
        Numerator=[0.0021878514650437138 0 -0.010189095418136306 0 0.031140395225498142 0 -0.082785931644222849 0 0.30979571849010856 0.5 0.30979571849010856 0 -0.082785931644222849 0 0.031140395225498142 0 -0.010189095418136306 0 0.0021878514650437138]);
    firdn2 = dsp.FIRDecimator(  ...
        Numerator=[0.0011555011750488237 0 -0.0027482166351233102 0 0.0057681982289523072 0 -0.010736374060960912 0 0.018592020073668478 0 -0.031093723586671229 0 0.052603914610235683 0 -0.099130756073130377 0 0.31592697826202448 0.5 0.31592697826202448 0 -0.099130756073130377 0 0.052603914610235683 0 -0.031093723586671229 0 0.018592020073668478 0 -0.010736374060960912 0 0.0057681982289523072 0 -0.0027482166351233102 0 0.0011555011750488237]);
end

%% Process
y1 = firdn1(x);
y = firdn2(y1);

Input Arguments

collapse all

Filter cascade, specified as a dsp.FilterCascade System object.

File name where the generated function is saved, specified as a character vector or string scalar.

Data Types: char | string

Version History

Introduced in R2014b