Main Content

generateFilteringCode

Generate MATLAB code to filter signals using parallel filter structure

Since R2023b

Description

generateFilteringCode(PF) generates a MATLAB® function in the editor that creates the branches of the parallel filter PF and processes input data through each of these branches.

If the filters in each stage support code generation, you can generate C/C++ code from this function.

generateFilteringCode(PF,fileName) generates code and saves the resulting function to the file specified in fileName. If the file name you specify already exists, its content is replaced.

example

Examples

collapse all

Design a parallel filter with two branches. Generate a MATLAB function to construct this filter using the individual filter objects, filter signals using these objects, and sum the outputs.

Construct a dsp.ParallelFilter object with dsp.FIRFilter on the first branch and dsp.Delay on the second branch.

pf = dsp.ParallelFilter(dsp.FIRFilter,dsp.Delay)
pf = 
  dsp.ParallelFilter with properties:

          Branch1: [1x1 dsp.FIRFilter]
          Branch2: [1x1 dsp.Delay]
    CloneBranches: true

Using the generateFilteringCode function, generate a MATLAB function to construct this filter. You cannot generate C/C++ code from the dsp.ParallelFilter object directly, but you can generate C/C++ code from the generated function. The function is saved in a file called myParallelFilter.m in the current directory.

fName = 'myParallelFilter.m';
generateFilteringCode(pf,fName);

The myParallelFilter function creates a parallel filter and calls each branch object in turn.

type myParallelFilter
function y = myParallelFilter(x)
%MYPARALLELFILTER Construct a parallel filter and process its branches

% MATLAB Code
% Generated by MATLAB(R) 24.2 and DSP System Toolbox 24.2.
% Generated on: 05-Sep-2024 23:46:52

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

%% Construction
persistent fir1 dly2
if isempty(fir1)
    fir1 = dsp.FIRFilter();
    dly2 = dsp.Delay();
end

%% Process
y1 = fir1(x);

y2 = dly2(x);

y = y1 + y2;

Input Arguments

collapse all

Parallel filter, specified as a dsp.ParallelFilter System object.

Name of the file where the generated function is saved, specified as a character vector or string scalar.

Data Types: char | string

Version History

Introduced in R2023b