generateFilteringCode
Generate MATLAB code for a filter cascade
Description
generateFilteringCode(
creates a MATLAB® function that contains code to create the stages of a filter cascade,
FC
)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
.
Examples
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", OptimizeFor="MPIS", Tolerance=0, ToleranceUnits="absolute") Conversion ratio: 1:4 Input sample rate: 2000 Output sample rate: 500
filtCasc = dsp.FilterCascade with properties: Stage1: [1×1 dsp.FIRDecimator] Stage2: [1×1 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)
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) 25.2 and DSP System Toolbox 25.2. % Generated on: 10-Aug-2025 08:54:40 % 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
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
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)