Main Content


Generate MATLAB code to filter signals using parallel filter structure

Since R2023b


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.


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';

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

% Generated by MATLAB(R) 24.1 and DSP System Toolbox 24.1.
% Generated on: 12-Feb-2024 21:11:52

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

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

%% 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