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.

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

example

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 = designRateConverter(DecimationFactor=4,...
    InputSampleRate=2000,Bandwidth=200,StopbandAttenuation=60,...
    Verbose=true)
designRateConverter(InterpolationFactor=1, DecimationFactor=4, InputSampleRate=2000, Bandwidth=200, StopbandAttenuation=60, MaxStages=Inf, CostMethod="estimate", Tolerance=0, ToleranceUnits="absolute")

Conversion ratio: 1:4
Input sample rate: 2000
Output sample rate: 500
filtCasc = 
  dsp.FilterCascade with properties:

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

Verify your design.

 info(filtCasc)
ans = 
    'Discrete-Time Filter Cascade
     ----------------------------
     Number of stages: 2
     Stage cloning: enabled
     
     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                             
     
     '

 freqz(filtCasc)

Figure contains 2 axes objects. Axes object 1 with title Phase, xlabel Normalized Frequency (\times\pi rad/sample), ylabel Phase (degrees) contains an object of type line. Axes object 2 with title Magnitude, xlabel Normalized Frequency (\times\pi rad/sample), ylabel Magnitude (dB) contains an object 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) 24.2 and DSP System Toolbox 24.2.
% Generated on: 05-Sep-2024 23:47:01

% 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.0021878514650437845 0 -0.010189095418136306 0 0.031140395225498115 0 -0.082785931644222821 0 0.30979571849010851 0.5 0.30979571849010851 0 -0.082785931644222821 0 0.031140395225498115 0 -0.010189095418136306 0 0.0021878514650437845]);
    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