Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

dsp.FilterCascade System object

Create a cascade of filter System objects

Description

The dsp.FilterCascade object creates a multistage System object™ that enables cascading of filter System objects, delays, and scalar gains. To see the System objects you can assign, at the MATLAB® command prompt, enter:

dsp.FilterCascade.helpSupportedSystemObjects

You can use the cascaded filter for filtering a signal, and for generating the filtering code. You can pass the dsp.FilterCascade System object as a stage to another dsp.FilterCascade System object. You can also pass dsp.FilterCascade System object as an input to the cascade function. This object supports variable-size signals if the stages within support variable-size signals.

To filter a signal using the cascaded filter:

  1. Define and set up your dsp.FilterCascade System object. See Construction.

  2. Call step to filter each channel of the input signal using the stages in the cascade. The size, data type, and complexity of the input signal must be supported by the stages in the filter cascade.

  3. Run the dsp.FilterCascade.generateFilteringCode method to generate MATLAB code using the design specified through the System objects.

Note

Starting in R2016b, instead of using the step method to perform the operation defined by the System object, you can call the object with arguments, as if it were a function. For example, y = step(obj,x) and y = obj(x) perform equivalent operations.

Construction

filtCasc = dsp.FilterCascade returns a System object, filtCasc. This System object has a single stage of the dsp.FIRFilter object.

filtCasc = dsp.FilterCascade(Filter_1,Filter_2,...) returns a multistage System object, filtCasc, with the first stage set to Filter_1, the second stage set to Filter_2, and so on. Filter_1, Filter_2, etc. can be a filter System object, a dsp.FilterCascade System object, delay, or a scalar gain.

filtCasc = cascade(Filter_1,Filter_2,...) returns a dsp.FilterCascade System object, filtCasc, with the first stage set to Filter_1, the second stage set to Filter_2, and so on. Filter_1, Filter_2, etc. can be a filter System object, a dsp.FilterCascade System object, or a scalar gain. cascade function does not support delay as a filter stage.

Properties

Stagei (Stage1, Stage2,...)

Specify the ith stage of the dsp.FilterCascade System object. You can assign any of the filter System objects, delay, or a scalar gain to this property. To see the System objects you can assign, at the MATLAB command prompt, enter:

dsp.FilterCascade.helpSupportedSystemObjects

The default value of this property is dsp.FIRFilter. For more information, see dsp.FIRFilter.

Methods

addStageAdd a new filter stage to the cascade
generateFilteringCodeGenerate MATLAB code to filter using cascade
getNumStagesGet the number of stages in the cascade object
releaseStagesRelease the locked state of all stages in the cascade
removeStageRemove a stage from filter cascade
stepProcess inputs using the filter cascade

More Analysis Methods for Filter System Objects.

Common to All System Objects
clone

Create System object with same property values

getNumInputs

Expected number of inputs to a System object

getNumOutputs

Expected number of outputs of a System object

isLocked

Check locked states of a System object (logical)

release

Allow System object property value changes

Examples

expand all

Note: This example runs only in R2016b or later. If you are using an earlier release, replace each call to the function with the equivalent step syntax. For example, myObject(x) becomes step(myObject,x).

Design a bandpass filter by cascading:

  • A highpass filter with a stopband frequency of 5000 Hz and a passband frequency of 8000 Hz.

  • A lowpass filter with a passband frequency of 12,000 Hz and a stopband frequency of 15,000 Hz.

Visualize the frequency response using fvtool.

lpFilt = dsp.LowpassFilter('StopbandFrequency',15000,...
    'PassbandFrequency',12000);
hpFilt = dsp.HighpassFilter('StopbandFrequency',5000,...
    'PassbandFrequency',8000);

bpFilt = dsp.FilterCascade(lpFilt,hpFilt);

fvtool(bpFilt);
legend('Bandpass Filter');

Pass a noisy sine wave as the input to the bandpass filter. The input is a sum of three sine waves with frequencies at 3 kHz, 10 kHz, and 15 kHz. The sampling frequency is 48 kHz. View the input and the filtered output on a spectrum analyzer.

Sine1 = dsp.SineWave('Frequency',3e3,'SampleRate',48e3,'SamplesPerFrame',6000);
Sine2 = dsp.SineWave('Frequency',10e3,'SampleRate',48e3,'SamplesPerFrame',6000);
Sine3 = dsp.SineWave('Frequency',15e3,'SampleRate',48e3,'SamplesPerFrame',6000);

SpecAna = dsp.SpectrumAnalyzer('PlotAsTwoSidedSpectrum',false, ...
    'SampleRate',Sine1.SampleRate, ...
    'NumInputPorts',2,...
    'ShowLegend',true, ...
    'YLimits',[-160,60]);

SpecAna.ChannelNames = {'Original noisy signal','Filtered signal'};


for i = 1 : 1000
    x = Sine1()+Sine2()+Sine3()+0.1.*randn(Sine1.SamplesPerFrame,1);
    y = bpFilt(x);
    SpecAna(x,y);

end
release(SpecAna)

The tones at 3 kHz and 15 kHz are attenuated, and the tone at 10 kHz is preserved by the bandpass filter.

Create a CIC decimator. Cascade the decimator with a gain.

cicdecim = dsp.CICDecimator('DecimationFactor', 6, ...
    'NumSections', 6);
decimcasc = dsp.FilterCascade(cicdecim, 1/gain(cicdecim));

Design a compensation decimator and cascade it with the filter cascade, decimcasc.

fs = 16e3;     % Sampling frequency of input of compensation decimator
fPass = 4e3;   % Passband frequency
fStop = 4.5e3; % Stopband frequency
ciccomp = dsp.CICCompensationDecimator(cicdecim, ...
    'DecimationFactor', 2, ...
    'PassbandFrequency', fPass, ...
    'StopbandFrequency', fStop, ...
    'SampleRate', fs);
filtchain = dsp.FilterCascade(decimcasc, ciccomp);

Visualize the frequency response of the cascade of cascades.

f = fvtool(decimcasc, ciccomp, filtchain, 'Fs',[fs*6,fs,fs*6],...
    'Arithmetic', 'fixed');
legend(f,'CIC Decimator','CIC Compensation Decimator', ...
    'Overall Response');

The CIC compensation decimator has an inherent gain, gain(cicdecim). By cascading with a factor of 1/gain(cicdecim), the filter cascade compensates for this gain.

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.

decimSpec = fdesign.decimator(4, 'Nyquist', 4, 'Tw,Ast', 100,60,2000);
filtCasc = design(decimSpec, 'multistage', 'SystemObject', true);

Verify your design using fvtool.

 info(filtCasc)
 fvtool(filtCasc)
ans =

    'Discrete-Time Filter Cascade
     ----------------------------
     Number of stages: 2
     
     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                             
     
     '

Generate code to filter data using this design. The code is saved in a file called stepDecimator.m in the current directory.

 generateFilteringCode(filtCasc, 'stepDecimator');

The stepDecimator function creates a filter cascade and calls the step method on each stage.

 type stepDecimator
function y = stepDecimator(x)
%STEPDECIMATOR Construct filter cascade and process each stage

% MATLAB Code
% Generated by MATLAB(R) 9.3 and DSP System Toolbox 9.5.
% Generated on: 01-Sep-2017 16:52:03

% 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.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]);
    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);

Extended Capabilities

Introduced in R2014b

Was this topic helpful?