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
Generate Code to Filter Using Cascade
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)
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 filter cascade and process each stage % MATLAB Code % Generated by MATLAB(R) 9.14 and DSP System Toolbox 9.16. % Generated on: 03-Mar-2023 05:38:05 % To generate C/C++ code from this function use the codegen command. % Type 'help codegen' for more information. %#codegen %% Construction persistent filter1 filter2 if isempty(filter1) filter1 = 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]); filter2 = 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 = filter1( x ); y = filter2( y1);
Input Arguments
FC
— Filter cascade
dsp.FilterCascade
System object™
dsp.FilterCascade
Filter cascade, specified as a dsp.FilterCascade
System object.
fileName
— File name
character vector | string scalar
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 コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- 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)