DSP System Toolbox
User's Guide
R2015a
How to Contact MathWorks
Latest news:
www.mathworks.com
Sales and services:
www.mathworks.com/sales_and_services
User community:
www.mathworks.com/matlabcentral
Technical support:
www.mathworks.com/support/contact_us
Phone:
508-647-7000
The MathWorks, Inc.
3 Apple Hill Drive
Natick, MA 01760-2098
DSP System Toolbox User's Guide
COPYRIGHT 20112015 by The MathWorks, Inc.
The software described in this document is furnished under a license agreement. The software may be used
or copied only under the terms of the license agreement. No part of this manual may be photocopied or
reproduced in any form without prior written consent from The MathWorks, Inc.
FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation
by, for, or through the federal government of the United States. By accepting delivery of the Program
or Documentation, the government hereby agrees that this software or documentation qualifies as
commercial computer software or commercial computer software documentation as such terms are used
or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and
conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and
govern the use, modification, reproduction, release, performance, display, and disclosure of the Program
and Documentation by the federal government (or other entity acquiring for or through the federal
government) and shall supersede any conflicting contractual terms or conditions. If this License fails
to meet the government's needs or is inconsistent in any respect with federal procurement law, the
government agrees to return the Program and Documentation, unused, to The MathWorks, Inc.
Trademarks
MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand
names may be trademarks or registered trademarks of their respective holders.
Patents
MathWorks products are protected by one or more U.S. patents. Please see
www.mathworks.com/patents for more information.
Revision History
April 2011
September 2011
March 2012
September 2012
March 2013
September 2013
March 2014
October 2014
March 2015
First printing
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Revised for Version 8.0 (R2011a)
Revised for Version 8.1 (R2011b)
Revised for Version 8.2 (R2012a)
Revised for Version 8.3 (R2012b)
Revised for Version 8.4 (R2013a)
Revised for Version 8.5 (R2013b)
Revised for Version 8.6 (R2014a)
Revised for Version 8.7 (R2014b)
Revised for Version 9.0 (R2015a)
Contents
DSP Tutorials
Introduction to Streaming Signal Processing in MATLAB . .
1-2
Filter Frames of a Noisy Sine Wave Signal in MATLAB . . .
1-10
Filter Frames of a Noisy Sine Wave Signal in Simulink . . .
Open Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Inspect Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Compare Original and Filtered Signal . . . . . . . . . . . . . . . . .
1-13
1-14
1-15
1-18
Lowpass Filter Design in MATLAB . . . . . . . . . . . . . . . . . . . .
1-20
Lowpass IIR Filter Design in Simulink . . . . . . . . . . . . . . . .
Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Butterworth Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Chebyshev Type I Filter . . . . . . . . . . . . . . . . . . . . . . . . . . .
Chebyshev Type II Filter . . . . . . . . . . . . . . . . . . . . . . . . . .
Elliptic Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Minimum-Order Designs . . . . . . . . . . . . . . . . . . . . . . . . . . .
1-31
1-31
1-32
1-34
1-36
1-38
1-40
Create Moving Average System object . . . . . . . . . . . . . . . . .
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create the Class Definition . . . . . . . . . . . . . . . . . . . . . . . . .
Moving Average Filter Properties . . . . . . . . . . . . . . . . . . . .
Moving Average Filter Constructor . . . . . . . . . . . . . . . . . . .
Moving Average Filter Setup . . . . . . . . . . . . . . . . . . . . . . .
Moving Average Filter Step . . . . . . . . . . . . . . . . . . . . . . . .
Moving Average Filter Reset . . . . . . . . . . . . . . . . . . . . . . . .
Input Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Object Saving and Loading . . . . . . . . . . . . . . . . . . . . . . . . .
System object Usage in MATLAB . . . . . . . . . . . . . . . . . . . .
Simulink Customization Methods . . . . . . . . . . . . . . . . . . . .
System object Usage in Simulink . . . . . . . . . . . . . . . . . . . .
1-44
1-44
1-45
1-45
1-46
1-47
1-47
1-48
1-48
1-48
1-49
1-50
1-50
iii
iv
Contents
Tunable Lowpass Filtering of Audio Input in Simulink . . .
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1-52
1-53
1-54
Desktop Real-time Audio Acceleration with MATLAB
Coder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Notch Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
C Code Generation Speedup . . . . . . . . . . . . . . . . . . . . . . . .
1-57
1-57
1-57
1-58
Fixed-Point Filter Design in MATLAB . . . . . . . . . . . . . . . . .
1-60
Sample Rate Conversion of Audio Signal in MATLAB and
Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Design and Analysis of Sample Rate Converter . . . . . . . . . .
Using the Sample Rate Converter in MATLAB . . . . . . . . . .
Using the Sample Rate Converter in Simulink . . . . . . . . . .
Specifying Tolerance to Reduce Filter Order . . . . . . . . . . . .
1-69
1-69
1-69
1-71
1-73
1-76
Visualizing Multiple Signals Using Logic Analyzer . . . . . . .
Model Programmable FIR Filter . . . . . . . . . . . . . . . . . . . . .
Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Use the Logic Analyzer . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modify the Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1-78
1-78
1-79
1-80
1-82
Signal Visualization and Measurements in MATLAB . . . . .
1-92
Filter Frames of a Noisy Sine Wave Signal using Testbench
Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1-107
Create Composite System object . . . . . . . . . . . . . . . . . . . . .
Create MultiNotchFilter . . . . . . . . . . . . . . . . . . . . . . . . . .
Contain System Objects as Private Properties . . . . . . . . . .
Work with Dependent Properties . . . . . . . . . . . . . . . . . . .
Use the Multi-Notch Filter - Initialization . . . . . . . . . . . . .
Use the Multi-Notch Filter - Streaming . . . . . . . . . . . . . . .
1-116
1-116
1-116
1-117
1-118
1-118
Measure Audio Latency in MATLAB and Simulink . . . . . .
1-121
Input, Output, and Display
Discrete-Time Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Time and Frequency Terminology . . . . . . . . . . . . . . . . . . . . .
Recommended Settings for Discrete-Time Simulations . . . . .
Other Settings for Discrete-Time Simulations . . . . . . . . . . . .
2-2
2-2
2-3
2-6
Continuous-Time Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Continuous-Time Source Blocks . . . . . . . . . . . . . . . . . . . . .
Continuous-Time Nonsource Blocks . . . . . . . . . . . . . . . . . .
2-11
2-11
2-11
Create Sample-Based Signals . . . . . . . . . . . . . . . . . . . . . . . . .
Create Signals Using Constant Block . . . . . . . . . . . . . . . . .
Create Signals Using Signal from Workspace Block . . . . . .
2-12
2-12
2-14
Sample-Based Row Vector Processing Changes . . . . . . . . .
2-17
Create Frame-Based Signals . . . . . . . . . . . . . . . . . . . . . . . . .
Create Signals Using Sine Wave Block . . . . . . . . . . . . . . . .
Create Signals Using Signal from Workspace Block . . . . . .
2-19
2-19
2-22
Create Multichannel Sample-Based Signals . . . . . . . . . . . . .
Multichannel Sample-Based Signals . . . . . . . . . . . . . . . . . .
Create Multichannel Signals by Combining Single-Channel
Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create Multichannel Signals by Combining Multichannel
Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-26
2-26
Create Multichannel Frame-Based Signals . . . . . . . . . . . . .
Multichannel Frame-Based Signals . . . . . . . . . . . . . . . . . . .
Create Multichannel Signals Using Concatenate Block . . . .
2-32
2-32
2-32
Deconstruct Multichannel Sample-Based Signals . . . . . . . .
Split Multichannel Signals into Individual Signals . . . . . . .
Split Multichannel Signals into Several Multichannel
Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-36
2-36
Deconstruct Multichannel Frame-Based Signals . . . . . . . . .
Split Multichannel Signals into Individual Signals . . . . . . .
Reorder Channels in Multichannel Frame-Based Signals . .
2-43
2-43
2-48
2-26
2-29
2-39
Import and Export Sample-Based Signals . . . . . . . . . . . . . .
Import Sample-Based Vector Signals . . . . . . . . . . . . . . . . .
Import Sample-Based Matrix Signals . . . . . . . . . . . . . . . . .
Export Sample-Based Signals . . . . . . . . . . . . . . . . . . . . . . .
2-52
2-52
2-55
2-59
Import and Export Frame-Based Signals . . . . . . . . . . . . . . .
Import Frame-Based Signals . . . . . . . . . . . . . . . . . . . . . . . .
Export Frame-Based Signals . . . . . . . . . . . . . . . . . . . . . . . .
2-64
2-64
2-67
Musical Instrument Digital Interface . . . . . . . . . . . . . . . . . .
About MIDI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
MIDI Control Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using MIDI Control Surfaces with MATLAB and Simulink .
2-72
2-72
2-72
2-73
Display Time-Domain Data . . . . . . . . . . . . . . . . . . . . . . . . . . .
Configure the Time Scope Properties . . . . . . . . . . . . . . . . .
Use the Simulation Controls . . . . . . . . . . . . . . . . . . . . . . . .
Modify the Time Scope Display . . . . . . . . . . . . . . . . . . . . . .
Inspect Your Data (Scaling the Axes and Zooming) . . . . . . .
Manage Multiple Time Scopes . . . . . . . . . . . . . . . . . . . . . . .
2-77
2-78
2-83
2-84
2-86
2-89
Display Frequency-Domain Data in Spectrum Analyzer . .
2-93
Visualize Central Limit Theorem in Array Plot . . . . . . . . .
Display a Uniform Distribution . . . . . . . . . . . . . . . . . . . . .
Display the Sum of Many Uniform Distributions . . . . . . . .
Inspect Your Data by Zooming . . . . . . . . . . . . . . . . . . . . .
2-100
2-100
2-101
2-103
Display Multiple Signals in the Time Scope . . . . . . . . . . . .
Multiple Signal Input . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Multiple Time Offsets . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Multiple Displays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-105
2-105
2-107
2-108
Data and Signal Management
Sample- and Frame-Based Concepts . . . . . . . . . . . . . . . . . . . .
Sample- and Frame-Based Signals . . . . . . . . . . . . . . . . . . . .
Model Sample- and Frame-Based Signals in MATLAB and
Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
vi
Contents
3-2
3-2
3-3
What Is Sample-Based Processing? . . . . . . . . . . . . . . . . . . . .
What Is Frame-Based Processing? . . . . . . . . . . . . . . . . . . . .
3-3
3-4
Inspect Sample and Frame Rates in Simulink . . . . . . . . . . . .
Sample Rate and Frame Rate Concepts . . . . . . . . . . . . . . . .
Inspect Sample-Based Signals Using Probe Block . . . . . . . . .
Inspect Frame-Based Signals Using Probe Block . . . . . . . . .
Inspect Sample-Based Signals Using Color Coding . . . . . . .
Inspect Frame-Based Signals Using Color Coding . . . . . . . .
3-8
3-8
3-9
3-11
3-13
3-15
Convert Sample and Frame Rates in Simulink . . . . . . . . . .
Rate Conversion Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Rate Conversion by Frame-Rate Adjustment . . . . . . . . . . . .
Rate Conversion by Frame-Size Adjustment . . . . . . . . . . . .
Avoid Unintended Rate Conversion . . . . . . . . . . . . . . . . . . .
Frame Rebuffering Blocks . . . . . . . . . . . . . . . . . . . . . . . . . .
Buffer Signals by Preserving the Sample Period . . . . . . . . .
Buffer Signals by Altering the Sample Period . . . . . . . . . . .
3-19
3-19
3-20
3-24
3-28
3-34
3-36
3-39
Buffering and Frame-Based Processing . . . . . . . . . . . . . . . .
Frame Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Buffer Sample-Based Signals into Frame-Based Signals . . .
Buffer Sample-Based Signals into Frame-Based Signals with
Overlap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Buffer Frame-Based Signals into Other Frame-Based
Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Buffer Delay and Initial Conditions . . . . . . . . . . . . . . . . . .
Unbuffer Frame-Based Signals into Sample-Based Signals .
3-43
3-43
3-43
Delay and Latency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Computational Delay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Algorithmic Delay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Zero Algorithmic Delay . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Basic Algorithmic Delay . . . . . . . . . . . . . . . . . . . . . . . . . . .
Excess Algorithmic Delay (Tasking Latency) . . . . . . . . . . . .
Predict Tasking Latency . . . . . . . . . . . . . . . . . . . . . . . . . . .
3-59
3-59
3-61
3-61
3-64
3-67
3-69
3-46
3-51
3-54
3-54
vii
viii
Contents
Filter Analysis, Design, and Implementation
Design a Filter in Fdesign Process Overview . . . . . . . . . .
Process Flow Diagram and Filter Design Methodology . . . . .
4-2
4-2
Design a Filter in the Filterbuilder GUI . . . . . . . . . . . . . . . .
The Graphical Interface to Fdesign . . . . . . . . . . . . . . . . . . .
4-10
4-10
Use FDATool with DSP System Toolbox Software . . . . . . . .
Design Advanced Filters in FDATool . . . . . . . . . . . . . . . . .
Access the Quantization Features of FDATool . . . . . . . . . . .
Quantize Filters in FDATool . . . . . . . . . . . . . . . . . . . . . . . .
Analyze Filters with a Noise-Based Method . . . . . . . . . . . .
Scale Second-Order Section Filters . . . . . . . . . . . . . . . . . . .
Reorder the Sections of Second-Order Section Filters . . . . .
View SOS Filter Sections . . . . . . . . . . . . . . . . . . . . . . . . . .
Import and Export Quantized Filters . . . . . . . . . . . . . . . . .
Generate MATLAB Code . . . . . . . . . . . . . . . . . . . . . . . . . . .
Import XILINX Coefficient (.COE) Files . . . . . . . . . . . . . . .
Transform Filters Using FDATool . . . . . . . . . . . . . . . . . . . .
Design Multirate Filters in FDATool . . . . . . . . . . . . . . . . . .
Realize Filters as Simulink Subsystem Blocks . . . . . . . . . . .
4-14
4-14
4-18
4-20
4-28
4-33
4-37
4-42
4-47
4-52
4-53
4-53
4-62
4-74
Digital Frequency Transformations . . . . . . . . . . . . . . . . . . .
Details and Methodology . . . . . . . . . . . . . . . . . . . . . . . . . . .
Frequency Transformations for Real Filters . . . . . . . . . . . .
Frequency Transformations for Complex Filters . . . . . . . . .
4-77
4-77
4-85
4-99
Digital Filter Design Block . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview of the Digital Filter Design Block . . . . . . . . . . . .
Select a Filter Design Block . . . . . . . . . . . . . . . . . . . . . . .
Create a Lowpass Filter in Simulink . . . . . . . . . . . . . . . . .
Create a Highpass Filter in Simulink . . . . . . . . . . . . . . . .
Filter High-Frequency Noise in Simulink . . . . . . . . . . . . .
4-111
4-111
4-112
4-113
4-114
4-116
Filter Realization Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview of the Filter Realization Wizard . . . . . . . . . . . . .
Design and Implement a Fixed-Point Filter in Simulink . .
Set the Filter Structure and Number of Filter Sections . . .
Optimize the Filter Structure . . . . . . . . . . . . . . . . . . . . . .
4-121
4-121
4-121
4-130
4-131
Digital Filter Implementations . . . . . . . . . . . . . . . . . . . . . .
Using Digital Filter Blocks . . . . . . . . . . . . . . . . . . . . . . . .
Implement a Lowpass Filter in Simulink . . . . . . . . . . . . .
Implement a Highpass Filter in Simulink . . . . . . . . . . . . .
Filter High-Frequency Noise in Simulink . . . . . . . . . . . . .
Specify Static Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Specify Time-Varying Filters . . . . . . . . . . . . . . . . . . . . . . .
Specify the SOS Matrix (Biquadratic Filter Coefficients) . .
4-133
4-133
4-133
4-134
4-135
4-140
4-140
4-141
Analog Filter Design Block . . . . . . . . . . . . . . . . . . . . . . . . . .
4-143
Removing High-Frequency Noise from an ECG Signal . . .
4-145
Adaptive Filters
Overview of Adaptive Filters and Applications . . . . . . . . . . .
Introduction to Adaptive Filtering . . . . . . . . . . . . . . . . . . . . .
Adaptive Filtering Methodology . . . . . . . . . . . . . . . . . . . . . .
Choosing an Adaptive Filter . . . . . . . . . . . . . . . . . . . . . . . . .
System Identification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Inverse System Identification . . . . . . . . . . . . . . . . . . . . . . . .
Noise or Interference Cancellation . . . . . . . . . . . . . . . . . . . .
Prediction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5-2
5-2
5-2
5-4
5-5
5-6
5-7
5-7
Adaptive Filters in DSP System Toolbox Software . . . . . . . .
Overview of Adaptive Filtering in DSP System Toolbox
Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Adaptive Filter Objects . . . . . . . . . . . . . . . . . . . . . . .
5-9
5-9
5-9
5-11
LMS Adaptive Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
LMS Filter Introductory Examples . . . . . . . . . . . . . . . . . . .
System Identification Using the LMS Algorithm . . . . . . . . .
System Identification Using the Normalized LMS Algorithm
Noise Cancellation Using the Sign-Data LMS Algorithm . . .
Noise Cancellation Using Sign-Error LMS Algorithm . . . . .
Noise Cancellation Using Sign-Sign LMS Algorithm . . . . . .
5-12
5-12
5-13
5-17
5-19
5-23
5-26
ix
Contents
RLS Adaptive Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Compare RLS and LMS Adaptive Filter Algorithms . . . . . .
Inverse System Identification Using dsp.RLSFilter . . . . . . .
5-30
5-30
5-31
Signal Enhancement Using LMS and Normalized LMS . . .
Create the Signals for Adaptation . . . . . . . . . . . . . . . . . . . .
Construct Two Adaptive Filters . . . . . . . . . . . . . . . . . . . . . .
Choose the Step Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Set the Adapting Filter Step Size . . . . . . . . . . . . . . . . . . . .
Filter with the Adaptive Filters . . . . . . . . . . . . . . . . . . . . .
Compute the Optimal Solution . . . . . . . . . . . . . . . . . . . . . .
Plot the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Compare the Final Coefficients . . . . . . . . . . . . . . . . . . . . . .
Reset the Filter Before Filtering . . . . . . . . . . . . . . . . . . . . .
Investigate Convergence Through Learning Curves . . . . . . .
Compute the Learning Curves . . . . . . . . . . . . . . . . . . . . . .
Compute the Theoretical Learning Curves . . . . . . . . . . . . .
5-36
5-36
5-37
5-38
5-38
5-39
5-39
5-39
5-41
5-42
5-42
5-42
5-43
Adaptive Filters in Simulink . . . . . . . . . . . . . . . . . . . . . . . . .
Create an Acoustic Environment in Simulink . . . . . . . . . . .
LMS Filter Configuration for Adaptive Noise Cancellation .
Modify Adaptive Filter Parameters During Model
Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adaptive Filtering Examples . . . . . . . . . . . . . . . . . . . . . . . .
5-45
5-45
5-47
Selected Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5-59
5-53
5-58
Multirate and Multistage Filters
Multirate Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Why Are Multirate Filters Needed? . . . . . . . . . . . . . . . . . . .
Overview of Multirate Filters . . . . . . . . . . . . . . . . . . . . . . . .
6-2
6-2
6-2
Multistage Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Why Are Multistage Filters Needed? . . . . . . . . . . . . . . . . . . .
Optimal Multistage Filters in DSP System Toolbox . . . . . . . .
6-6
6-6
6-6
Example Case for Multirate/Multistage Filters . . . . . . . . . . .
Example Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6-7
6-7
Single-Rate/Single-Stage Equiripple Design . . . . . . . . . . . . .
Reduce Computational Cost Using Mulitrate/Multistage
Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Compare the Responses . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Further Performance Comparison . . . . . . . . . . . . . . . . . . . . .
6-7
6-8
6-8
6-9
Filter Banks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Dyadic Analysis Filter Banks . . . . . . . . . . . . . . . . . . . . . . .
Dyadic Synthesis Filter Banks . . . . . . . . . . . . . . . . . . . . . .
6-11
6-11
6-15
Multirate Filtering in Simulink . . . . . . . . . . . . . . . . . . . . . . .
6-19
Transforms, Estimation, and Spectral Analysis
Transform Time-Domain Data into Frequency Domain . . . .
7-2
Transform Frequency-Domain Data into Time Domain . . . .
7-7
Linear and Bit-Reversed Output Order . . . . . . . . . . . . . . . .
FFT and IFFT Blocks Data Order . . . . . . . . . . . . . . . . . . . .
Find the Bit-Reversed Order of Your Frequency Indices . . .
7-12
7-12
7-12
Calculate Channel Latencies Required for Wavelet
Reconstruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Analyze Your Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Calculate the Group Delay of Your Filters . . . . . . . . . . . . .
Reconstruct the Filter Bank System . . . . . . . . . . . . . . . . . .
Equalize the Delay on Each Filter Path . . . . . . . . . . . . . . .
Update and Run the Model . . . . . . . . . . . . . . . . . . . . . . . . .
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7-14
7-14
7-16
7-18
7-18
7-21
7-22
Spectral Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7-23
Power Spectrum Estimates . . . . . . . . . . . . . . . . . . . . . . . . . .
Create the Block Diagram . . . . . . . . . . . . . . . . . . . . . . . . . .
Set the Model Parameters . . . . . . . . . . . . . . . . . . . . . . . . . .
View the Power Spectrum Estimates . . . . . . . . . . . . . . . . . .
7-24
7-24
7-25
7-32
xi
Spectrograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modify the Block Diagram . . . . . . . . . . . . . . . . . . . . . . . . .
Set the Model Parameters . . . . . . . . . . . . . . . . . . . . . . . . . .
View the Spectrogram of the Speech Signal . . . . . . . . . . . . .
xii
Contents
7-35
7-35
7-37
7-41
Mathematics
Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Statistics Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Basic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Running Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8-2
8-2
8-2
8-3
Linear Algebra and Least Squares . . . . . . . . . . . . . . . . . . . . .
Linear Algebra Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Linear System Solvers . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Matrix Factorizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Matrix Inverses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8-6
8-6
8-6
8-8
8-9
Fixed-Point Design
Fixed-Point Signal Processing . . . . . . . . . . . . . . . . . . . . . . . . .
Fixed-Point Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Benefits of Fixed-Point Hardware . . . . . . . . . . . . . . . . . . . . .
Benefits of Fixed-Point Design with System Toolboxes
Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9-2
9-2
9-2
Fixed-Point Concepts and Terminology . . . . . . . . . . . . . . . . .
Fixed-Point Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Scaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Precision and Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9-4
9-4
9-5
9-6
Arithmetic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modulo Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Two's Complement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Addition and Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . .
9-9
9-9
9-10
9-11
9-3
10
Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Casts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9-12
9-14
Fixed-Point Support for MATLAB System Objects . . . . . . .
Get Information About Fixed-Point System Objects . . . . . . .
Display Fixed-Point Properties for System Objects . . . . . . .
Set System Object Fixed-Point Properties . . . . . . . . . . . . . .
Full Precision for Fixed-Point System Objects . . . . . . . . . . .
9-19
9-19
9-22
9-23
9-24
Using the Fixed-Point Converter App with System Objects
9-25
Use the Fixed-Point Converter App with a System object .
Create DSP Filter Function and Test Bench . . . . . . . . . . . .
Convert the Function to Fixed-Point . . . . . . . . . . . . . . . . . .
9-26
9-26
9-27
Specify Fixed-Point Attributes for Blocks . . . . . . . . . . . . . .
Fixed-Point Block Parameters . . . . . . . . . . . . . . . . . . . . . . .
Specify System-Level Settings . . . . . . . . . . . . . . . . . . . . . . .
Inherit via Internal Rule . . . . . . . . . . . . . . . . . . . . . . . . . . .
Specify Data Types for Fixed-Point Blocks . . . . . . . . . . . . .
9-33
9-33
9-36
9-37
9-47
Quantizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Scalar Quantizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Vector Quantizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9-55
9-55
9-62
Fixed-Point Filter Design . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview of Fixed-Point Filters . . . . . . . . . . . . . . . . . . . . . .
Data Types for Filter Functions . . . . . . . . . . . . . . . . . . . . .
Floating-Point to Fixed-Point Filter Conversion . . . . . . . . .
Create an FIR Filter Using Integer Coefficients . . . . . . . . .
Fixed-Point Filtering in Simulink . . . . . . . . . . . . . . . . . . . .
9-69
9-69
9-69
9-70
9-79
9-95
C Code Generation
Understanding C Code Generation . . . . . . . . . . . . . . . . . . . .
C Code Generation with the Simulink Coder Product . . . . .
Highly Optimized Generated ANSI C Code . . . . . . . . . . . . .
10-2
10-2
10-3
xiii
Functions and System Objects Supported for C Code
Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
xiv
Contents
10-4
C Code Generation from MATLAB . . . . . . . . . . . . . . . . . . .
10-12
C Code Generation from Simulink . . . . . . . . . . . . . . . . . . .
Open and Run the Model . . . . . . . . . . . . . . . . . . . . . . . . .
Generate Code from the Model . . . . . . . . . . . . . . . . . . . . .
Build and Run the Generated Code . . . . . . . . . . . . . . . . . .
10-13
10-13
10-15
10-15
How to Run a Generated Executable Outside MATLAB . .
10-18
DSP System Toolbox Supported Hardware . . . . . . . . . . . .
10-19
Define New System Objects
Summary List of Methods for Defining New System
Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11-3
Define Basic System Objects . . . . . . . . . . . . . . . . . . . . . . . . .
11-5
Change Number of Step Inputs or Outputs . . . . . . . . . . . . .
11-7
Specify System Block Input and Output Names . . . . . . . .
11-11
Validate Property and Input Values . . . . . . . . . . . . . . . . . .
11-13
Initialize Properties and Setup One-Time Calculations . .
11-16
Set Property Values at Construction Time . . . . . . . . . . . . .
11-19
Reset Algorithm State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11-21
Define Property Attributes . . . . . . . . . . . . . . . . . . . . . . . . . .
11-23
Hide Inactive Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11-27
Limit Property Values to Finite String Set . . . . . . . . . . . .
11-29
Process Tuned Properties . . . . . . . . . . . . . . . . . . . . . . . . . . .
11-32
Release System Object Resources . . . . . . . . . . . . . . . . . . . .
11-34
Define Composite System Objects . . . . . . . . . . . . . . . . . . . .
11-36
Define Finite Source Objects . . . . . . . . . . . . . . . . . . . . . . . .
11-39
Save System Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11-41
Load System Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11-45
Clone System Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11-49
Define System Object Information . . . . . . . . . . . . . . . . . . .
11-50
Define System Block Icon . . . . . . . . . . . . . . . . . . . . . . . . . . .
11-52
Add Header to System Block Dialog . . . . . . . . . . . . . . . . . .
11-54
Add Property Groups to System Object and Block Dialog
11-56
Control Simulation Type in System Block Dialog . . . . . . .
11-61
Add Button to System Block Dialog Box . . . . . . . . . . . . . . .
11-63
Specify Locked Input Size . . . . . . . . . . . . . . . . . . . . . . . . . .
11-66
Set Output Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11-68
Set Output Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11-70
Set Output Complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11-72
Specify Whether Output Is Fixed- or Variable-Size . . . . . .
11-74
Specify Discrete State Output Specification . . . . . . . . . . .
11-76
Use Update and Output for Nondirect Feedthrough . . . . .
11-78
Enable For Each Subsystem Support . . . . . . . . . . . . . . . . .
11-81
xv
12
13
xvi
Contents
Methods Timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setup Method Call Sequence . . . . . . . . . . . . . . . . . . . . . . .
Step Method Call Sequence . . . . . . . . . . . . . . . . . . . . . . . .
Reset Method Call Sequence . . . . . . . . . . . . . . . . . . . . . . .
Release Method Call Sequence . . . . . . . . . . . . . . . . . . . . .
11-83
11-83
11-83
11-84
11-85
System Object Input Arguments and ~ in Code Examples
11-86
What Are Mixin Classes? . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11-87
Best Practices for Defining System Objects . . . . . . . . . . . .
11-88
HDL Code Generation
What is HDL Code Generation? . . . . . . . . . . . . . . . . . . . . . . .
12-2
HDL Code Generation Support for DSP System Toolbox . .
Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
System Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12-3
12-3
12-4
Find Blocks and System Objects Supporting HDL Code
Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
System Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12-6
12-6
12-6
Links to Category Pages
Signal Management Library . . . . . . . . . . . . . . . . . . . . . . . . . .
13-2
Sinks Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13-3
Math Functions Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13-4
Filtering Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13-5
14
15
Designing Lowpass FIR Filters
Lowpass FIR Filter Design . . . . . . . . . . . . . . . . . . . . . . . . . . .
14-2
Controlling Design Specifications in Lowpass FIR Design .
14-7
Designing Filters with Non-Equiripple Stopband . . . . . . .
14-13
Minimizing Lowpass FIR Filter Length . . . . . . . . . . . . . . .
14-18
FDATool: A Filter Design and Analysis GUI
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
FDATool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Filter Design Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Filter Design and Analysis Tool . . . . . . . . . . . . .
Analyzing Filter Responses . . . . . . . . . . . . . . . . . . . . . . . . .
Filter Design and Analysis Tool Panels . . . . . . . . . . . . . . . .
Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
15-2
15-2
15-2
15-3
15-4
15-4
15-5
Using FDATool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Choosing a Response Type . . . . . . . . . . . . . . . . . . . . . . . . .
Choosing a Filter Design Method . . . . . . . . . . . . . . . . . . . .
Setting the Filter Design Specifications . . . . . . . . . . . . . . . .
Computing the Filter Coefficients . . . . . . . . . . . . . . . . . . .
Analyzing the Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Editing the Filter Using the Pole/Zero Editor . . . . . . . . . .
Converting the Filter Structure . . . . . . . . . . . . . . . . . . . . .
Exporting a Filter Design . . . . . . . . . . . . . . . . . . . . . . . . .
Generating a C Header File . . . . . . . . . . . . . . . . . . . . . . .
Generating MATLAB Code . . . . . . . . . . . . . . . . . . . . . . . .
Managing Filters in the Current Session . . . . . . . . . . . . . .
Saving and Opening Filter Design Sessions . . . . . . . . . . . .
15-6
15-6
15-7
15-8
15-12
15-12
15-17
15-21
15-23
15-29
15-31
15-32
15-33
Importing a Filter Design . . . . . . . . . . . . . . . . . . . . . . . . . . .
Import Filter Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Filter Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
15-35
15-35
15-36
xvii
16
Designing a Filter in the Filterbuilder GUI
Filterbuilder Design Process . . . . . . . . . . . . . . . . . . . . . . . . .
Introduction to Filterbuilder . . . . . . . . . . . . . . . . . . . . . . . .
Design a Filter Using Filterbuilder . . . . . . . . . . . . . . . . . . .
Select a Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Select a Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Select an Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Customize the Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . .
Analyze the Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Realize or Apply the Filter to Input Data . . . . . . . . . . . . . .
Designing a FIR Filter Using filterbuilder . . . . . . . . . . . . .
FIR Filter Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
xviii
Contents
16-2
16-2
16-2
16-2
16-5
16-5
16-7
16-9
16-9
16-11
16-11
Bibliography
References Advanced Filters . . . . . . . . . . . . . . . . . . . . . . .
17-2
References Adaptive Filters . . . . . . . . . . . . . . . . . . . . . . . .
17-3
References Multirate Filters . . . . . . . . . . . . . . . . . . . . . . .
17-4
References Frequency Transformations . . . . . . . . . . . . .
17-5
References Fixed-Point Filters . . . . . . . . . . . . . . . . . . . . .
17-6
1
DSP Tutorials
Introduction to Streaming Signal Processing in MATLAB on page 1-2
Filter Frames of a Noisy Sine Wave Signal in MATLAB on page 1-10
Filter Frames of a Noisy Sine Wave Signal in Simulink on page 1-13
Lowpass Filter Design in MATLAB on page 1-20
Lowpass IIR Filter Design in Simulink on page 1-31
Create Moving Average System object on page 1-44
Tunable Lowpass Filtering of Audio Input in Simulink on page 1-52
Desktop Real-time Audio Acceleration with MATLAB Coder on page 1-57
Fixed-Point Filter Design in MATLAB on page 1-60
Sample Rate Conversion of Audio Signal in MATLAB and Simulink on page 1-69
Visualizing Multiple Signals Using Logic Analyzer on page 1-78
Signal Visualization and Measurements in MATLAB on page 1-92
Filter Frames of a Noisy Sine Wave Signal using Testbench Generator on page
1-107
Create Composite System object on page 1-116
Measure Audio Latency in MATLAB and Simulink on page 1-121
DSP Tutorials
Introduction to Streaming Signal Processing in MATLAB
This example shows how to do basic signal processing on an audio signal streamed
frame by frame from a file. The same framework applies to streaming live audio from a
microphone or audio line input.
Introduction
This example shows how to use components in the DSP System Toolbox called System
objects, which are optimized for streaming signal processing. The signals are read in and
processed frame by frame (or block by block). You control the frame size.
In this example, frames of 1024 samples are read from an audio file every time a while
loop executes. The audio in the file is sampled at 44.1 kHz and contains two channels
(stereo). Each frame of data is approximately 23.2 milliseconds.
The example filters the audio signal using a notch filter. Because filtering is done on each
frame, the state of the filter must be maintained from one frame to the next to ensure
smooth processing.
Initialization
Streaming components must be initialized before use. Initialize the audio file reader and
audio player to listen to the filtered audio.
FrameSize = 1024;
AR = dsp.AudioFileReader('Filename','guitar10min.ogg',...
'SamplesPerFrame',FrameSize);
Fs = AR.SampleRate;
AP = dsp.AudioPlayer('SampleRate',Fs,...
'OutputNumUnderrunSamples',true);
Notch Filter
This example uses a simple second order IIR notch filter with a biquad (second-order
sections) structure to filter the audio signal. The filter has a notch at 200 Hz and a Qfactor of 35. The higher the Q-factor, the narrower the 3 dB bandwidth of the notch. You
can change these values and listen to the resulting audio for different filter specifications
if desired. The notch filter retains state each time it is executed making it well-suited for
streaming data.
1-2
Introduction to Streaming Signal Processing in MATLAB
Wo = 200/(Fs/2);
Q = 35;
BW = Wo/Q;
[b,a] = iirnotch(Wo,BW);
NotchFilter = dsp.BiquadFilter('SOSMatrix',[b,a]);
Streaming
Next, stream in and process your signal. Construct a while loop to run for 20 seconds.
A counter keeps track of how many frames are processed during the 20-second period.
Because each frame is 23.2 ms of audio, at least 861 frames must be processed in the 20second period for the system to operate in real time or faster.
tic
Tstop = 20;
count = 0;
while toc < Tstop
audioIn = step(AR);
audioOut = step(NotchFilter,audioIn);
plot(audioOut)
drawnow
count = count + 1;
end
1-3
DSP Tutorials
On a reasonably fast computer, the value of count exceeds 861 frames meaning the
processing can be done in real time or faster.
Streaming Without DSP Components
Instead of using System objects, you can use the audioread and filter functions.
However, audioread is not well-suited for streaming, because the file name and position
must be provided at each call. As a result, the performance of audioread is considerably
slower than the performance of the System object file reader in a streaming application.
filter provides adequate performance, but state must be passed in and out of filter
for use in a streaming application. This can result in inefficient and complicated code if
there are many components with state that must be handled manually.
z = [];
1-4
Introduction to Streaming Signal Processing in MATLAB
tic
count = 0;
while toc < Tstop
audioIn = audioread('guitar10min.ogg',count*FrameSize+[1 FrameSize]);
[audioOut,z] = filter(b,a,audioIn,z);
plot(audioOut)
drawnow
count = count + 1;
end
The plot update gives a visual indication of how much slower the code is with audioread
and filter is compared to the System object implementation. In particular, count
is considerably less than 861 frames, and therefore processing real-time audio in this
fashion is not feasible.
1-5
DSP Tutorials
Listening to The Filtered Signal
On computers capable of processing the data in real time or better, you have to add only
a single line of code to listen to the filtered signal as it is being processed. A new variable,
dropped, keeps count of any dropped samples due to buffer underrun.
tic
dropped = 0;
count = 0;
while toc < Tstop
audioIn = step(AR);
audioOut = step(NotchFilter,audioIn);
plot(audioOut)
drawnow
count = count + 1;
dropped = dropped + step(AP,audioOut);
end
1-6
Introduction to Streaming Signal Processing in MATLAB
On most machines, there are no dropped samples.
Better Way of Plotting
Rather than using plot and drawnow to monitor the audio signal, use a System object
time scope. The time scope provides better performance along with many other features,
for example, measurements and panning. If you want to plot the spectrum of the signal
in real time, you can use a spectrum analyzer (dsp.SpectrumAnalyzer).
TS = dsp.TimeScope('YLimits',[-1,1],'SampleRate',Fs,...
'TimeSpan',FrameSize/Fs);
step(TS,audioOut);
tic
count = 0;
1-7
DSP Tutorials
while toc < Tstop % Run for 20 seconds
audioIn = step(AR);
audioOut = step(NotchFilter,audioIn);
step(TS,audioOut);
count = count + 1;
dropped = dropped + step(AP,audioOut);
end
1-8
Introduction to Streaming Signal Processing in MATLAB
However, for maximum performance, avoid plotting the signal in time or frequency.
Streaming Live Audio
To stream audio from a microphone or line input, use the audio recorder,
dsp.AudioRecorder. If you want to write the processed audio to disk, use the audio
file writer, dsp.AudioFileWriter. The audio file writer supports a number of the most
common audio formats such as WAV, OGG, FLAC, and M4A.
1-9
DSP Tutorials
Filter Frames of a Noisy Sine Wave Signal in MATLAB
This example shows how to lowpass filter a noisy signal and visualize the original and
filtered signals using a spectrum analyzer. There is a Simulink version of this example:
Filter Frames of a Noisy Signal in Simulink
Source
The input signal is the sum of two sine waves with frequencies of 1 kHz and 10 kHz. The
sampling frequency is 44.1 kHz.
Sine1 = dsp.SineWave('Frequency',1e3,'SampleRate',44.1e3);
Sine2 = dsp.SineWave('Frequency',10e3,'SampleRate',44.1e3);
Lowpass Filter
The lowpass FIR filter uses a constrained equiripple filter design (firceqrip). The filter's
cutoff frequency is 5 kHz and the linear ripples in the passband and stopband are equal
to 0.05 and 0.001.
FIRLowPass = dsp.FIRFilter('Numerator', ...
firceqrip(30,5e3*2/44.1e3,[0.05 1e-3]));
Spectrum Analyzer
Set up the spectrum analyzer to compare the power spectra of the original and filtered
signals. The spectrum units are dBm.
SpecAna = dsp.SpectrumAnalyzer('PlotAsTwoSidedSpectrum',false, ...
'SampleRate',Sine1.SampleRate, ...
'ReducePlotRate',false, ...
'ShowLegend',true, ...
'YLimits',[-145,45]);
SpecAna.ChannelNames = {'Original noisy signal','Low pass filtered signal'};
Frame-Based Processing
This example uses frame-based processing. In frame-based processing, the data is
processed one frame at a time. Each frame of data contains sequential samples from
an independent channel. Frame-based processing is advantageous for many signal
processing applications because you can process multiple samples at once. By buffering
1-10
Filter Frames of a Noisy Sine Wave Signal in MATLAB
your data into frames and processing multi-sample frames of data, you often improve the
computational time of your signal processing algorithms. In this example, the number
of samples per frame is 4000. The FIR filter uses frame-based processing if the input is
frame based.
Sine1.SamplesPerFrame = 4000;
Sine2.SamplesPerFrame = 4000;
Simulation Results
Add zero-mean white Gaussian noise with a standard deviation of 0.1 to the sum of sine
waves and filter the result with the FIR filter. While running the simulation, you see in
the spectrum analyzer that frequencies above 4 kHz in the source signal are attenuated.
The resulting signal maintains the peak at 1 kHz because it falls in the passband of the
lowpass filter.
for i = 1 : 1000
x = step(Sine1)+step(Sine2)+0.1.*randn(Sine1.SamplesPerFrame,1);
y = step(FIRLowPass,x);
step(SpecAna,[x,y]);
end
release(SpecAna)
1-11
DSP Tutorials
1-12
Filter Frames of a Noisy Sine Wave Signal in Simulink
Filter Frames of a Noisy Sine Wave Signal in Simulink
This example shows how to lowpass filter a noisy signal in Simulink and visualize the
original and filtered signals with a spectrum analyzer. For a MATLAB version of this
example, see Filter Frames of a Noisy Sine Wave Signal in MATLAB on page 1-10.
In this section...
Open Model on page 1-14
Inspect Model on page 1-15
Compare Original and Filtered Signal on page 1-18
1-13
DSP Tutorials
Open Model
1-14
Open the Library Browser by typing simulink at the MATLAB command prompt.
Click the New Model button arrow
In the Simulink Template Gallery, under DSP System Toolbox, double-click Basic
Model.
and select From Template.
Filter Frames of a Noisy Sine Wave Signal in Simulink
Inspect Model
Input Signal
Three source blocks comprise the input signal. The input signal consists of the sum of two
sine waves and white Gaussian noise with mean 0 and variance 0.05. The frequencies of
the sine waves are 1 kHz and 15 kHz. The sampling frequency is 44.1 kHz. The following
figure shows the block parameters for the 1 kHz sine wave. Open the other source blocks
to see their parameters.
1-15
DSP Tutorials
1-16
Filter Frames of a Noisy Sine Wave Signal in Simulink
Lowpass Filter
The lowpass filter is modeled using a Discrete FIR Filter block. The example uses a
constrained equiripple FIR filter design, firceqrip. The filter's cutoff frequency is 10
kHz. The linear ripples in the passband and stopband are equal to 0.05 and 1e-3.
The Lowpass FIR Filter block uses frame-based processing to process data one frame at
a time. Each frame of data contains sequential samples from an independent channel.
Frame-based processing is advantageous for many signal processing applications because
you can process multiple samples at once. By buffering your data into frames and
processing multisample frames of data, you can improve the computational time of
your signal processing algorithms. In this example, the number of samples per frame is
1-17
DSP Tutorials
256. Specify frame-based processing on the Lowpass FIR Filter block by setting Input
Processing to 'Columns as channels (frame based)'.
Compare Original and Filtered Signal
The power spectra of the original and filtered signals are compared using a Spectrum
Analyzer. The spectrum units are in dBm.
To run the simulation, click the Run button in the model. To stop the simulation, click
the Stop button in the Spectrum Analyzer. Alternatively, you can execute the following
code to run the simulation for 200 frames of data.
set_param(model,'StopTime','256/44100 * 200')
sim(model);
1-18
Filter Frames of a Noisy Sine Wave Signal in Simulink
Frequencies above 10 kHz in the source signal are attenuated. The resulting signal
maintains the peak at 1 kHz because it falls in the passband of the lowpass filter.
1-19
DSP Tutorials
Lowpass Filter Design in MATLAB
This example shows how to design lowpass filters. The example highlights some of the
most commonly used command-line tools in the DSP System Toolbox. Alternatively, you
can use the Filter Builder app to implement all the designs presented here. For more
design options, see Designing Low Pass FIR Filters.
Introduction
When designing a lowpass filter, the first choice you make is whether to design an
FIR or IIR filter. You generally choose FIR filters when a linear phase response is
important. FIR filters also tend to be preferred for fixed-point implementations because
they are typically more robust to quantization effects. FIR filters are also used in many
high-speed implementations such as FPGAs or ASICs because they are suitable for
pipelining. IIR filters (in particular biquad filters) are used in applications (such as
audio signal processing) where phase linearity is not a concern. IIR filters are generally
computationally more efficient in the sense that they can meet the design specifications
with fewer coefficients than FIR filters. IIR filters also tend to have a shorter transient
response and a smaller group delay. However, the use of minimum-phase and multirate
designs can result in FIR filters comparable to IIR filters in terms of group delay and
computational efficiency.
FIR Lowpass Designs - Specifying the Filter Order
There are many practical situations in which you must specify the filter order. One
such case is if you are targeting hardware which has constrained the filter order to a
specific number. Another common scenario is when you have computed the available
computational budget (MIPS) for your implementation and this affords you a limited
filter order. FIR design functions in the Signal Processing Toolbox (including fir1,
firpm, and firls) are all capable of designing lowpass filters with a specified order.
In the DSP System Toolbox, the preferred function for lowpass FIR filter design with a
specified order is firceqrip. This function designs optimal equiripple lowpass/highpass
FIR filters with specified passband/stopband ripple values and with a specified passbandedge frequency. The stopband-edge frequency is determined as a result of the design.
Design a lowpass FIR filter for data sampled at 48 kHz. The passband-edge frequency is
8 kHz. The passband ripple is 0.01 dB and the stopband attenuation is 80 dB. Constrain
the filter order to 120.
N
Fs
1-20
= 120;
= 48e3;
Lowpass Filter Design in MATLAB
Fp = 8e3;
Ap = 0.01;
Ast = 80;
Obtain the maximum deviation for the passband and stopband ripples in linear units.
Rp = (10^(Ap/20) - 1)/(10^(Ap/20) + 1);
Rst = 10^(-Ast/20);
Design the filter using firceqrip and view the magnitude frequency response.
NUM = firceqrip(N,Fp/(Fs/2),[Rp Rst],'passedge');
fvtool(NUM,'Fs',Fs,'Color','White')
The resulting stopband-edge frequency is about 9.64 kHz.
1-21
DSP Tutorials
Minimum-Order Designs
Another design function for optimal equiripple filters is firgr. firgr can design a
filter that meets passband/stopband ripple constraints as well as a specified transition
width with the smallest possible filter order. For example, if the stopband-edge frequency
is specified as 10 kHz, the resulting filter has an order of 100 rather than the 120thorder filter designed with firceqrip. The smaller filter order results from the larger
transition band.
Specify the stopband-edge frequency of 10 kHz. Obtain a minimum-order FIR filter with
a passband ripple of 0.01 dB and 80 dB of stopband attenuation.
Fst
= 10e3;
NumMin = firgr('minorder',[0 Fp/(Fs/2) Fst/(Fs/2) 1], [1 1 0 0],[Rp,Rst]);
Plot the magnitude frequency responses for the minimum-order FIR filter obtained with
firgr and the 120th-order filter designed with firceqrip. The minimum-order design
results in a filter with order 100. The transition region of the 120th-order filter is, as
expected, narrower than that of the filter with order 100.
fvtool(NUM,1,NumMin,1,'Fs',Fs,'Color','White');
legend('N = 120','N = 100');
1-22
Lowpass Filter Design in MATLAB
Filtering Data
To apply the filter to data, you can use the filter command or you can use
dsp.FIRFilter. dsp.FIRFilter has the advantage of managing state when executed
in a loop. dsp.FIRFilter also has fixed-point capabilities and supports C code
generation, HDL code generation, and optimized code generation for ARM Cortex M
and ARM Cortex A.
Filter 10 seconds of white noise with zero mean and unit standard deviation in frames
of 256 samples with the 120th-order FIR lowpass filter. View the result on a spectrum
analyzer.
LP_FIR = dsp.FIRFilter('Numerator',NUM);
SA
= dsp.SpectrumAnalyzer('SampleRate',Fs,'SpectralAverages',5);
tic
1-23
DSP Tutorials
while toc < 10
x = randn(256,1);
y = step(LP_FIR,x);
step(SA,y);
end
Using dsp.LowpassFilter
dsp.LowpassFilter is an alternative to using firceqrip and firgr in conjuction
with dsp.FIRFilter. Basically, dsp.LowpassFilter condenses the two step process
into one. dsp.LowpassFilter provides the same advantages that dsp.FIRFilter
provides in terms of fixed-point support, C code generation support, HDL code generation
support, and ARM Cortex code generation support.
1-24
Lowpass Filter Design in MATLAB
Design a lowpass FIR filter for data sampled at 48 kHz. The passband-edge frequency is
8 kHz. The passband ripple is 0.01 dB and the stopband attenuation is 80 dB. Constrain
the filter order to 120. Create a dsp.FIRFilter based on your specifications.
LP_FIR = dsp.LowpassFilter('SampleRate',Fs,...
'DesignForMinimumOrder',false,'FilterOrder',N,...
'PassbandFrequency',Fp,'PassbandRipple',Ap,'StopbandAttenuation',Ast);
The coefficients in LP_FIR are identical to the coefficients in NUM.
NUM_LP = tf(LP_FIR);
You can use LP_FIR to filter data directly, as shown in the preceding example. You can
also analyze the filter using FVTool or measure the response using measure.
fvtool(LP_FIR,'Fs',Fs,'Color','White');
measure(LP_FIR)
ans =
Sample Rate
Passband Edge
3-dB Point
6-dB Point
Stopband Edge
Passband Ripple
Stopband Atten.
Transition Width
:
:
:
:
:
:
:
:
48 kHz
8 kHz
8.5843 kHz
8.7553 kHz
9.64 kHz
0.01 dB
79.9981 dB
1.64 kHz
1-25
DSP Tutorials
Minimum-Order Designs with dsp.LowpassFilter
You can use dsp.LowpassFilter to design minimum-order filters and use measure to
verify that the design meets the prescribed specifications. The order of the filter is again
100.
LP_FIR_minOrd = dsp.LowpassFilter('SampleRate',Fs,...
'DesignForMinimumOrder',true,'PassbandFrequency',Fp,...
'StopbandFrequency',Fst,'PassbandRipple',Ap,'StopbandAttenuation',Ast);
measure(LP_FIR_minOrd)
Nlp = order(LP_FIR_minOrd)
ans =
Sample Rate
1-26
: 48 kHz
Lowpass Filter Design in MATLAB
Passband Edge
3-dB Point
6-dB Point
Stopband Edge
Passband Ripple
Stopband Atten.
Transition Width
:
:
:
:
:
:
:
8 kHz
8.7136 kHz
8.922 kHz
10 kHz
0.0098641 dB
80.122 dB
2 kHz
Nlp =
100
Designing IIR Filters
Elliptic filters are the IIR counterpart to optimal equiripple FIR filters. Accordingly, you
can use the same specifications to design elliptic filters. The filter order you obtain for an
IIR filter is much smaller than the order of the corresponding FIR filter.
Design an elliptic filter with the same sampling frequency, cutoff frequency, passbandripple constraint, and stopband attenuation as the 120th-order FIR filter. Reduce the
filter order for the elliptic filter to 10.
N = 10;
LP_IIR = dsp.LowpassFilter('SampleRate',Fs,'FilterType','IIR',...
'DesignForMinimumOrder',false,'FilterOrder',N,...
'PassbandFrequency',Fp,'PassbandRipple',Ap,'StopbandAttenuation',Ast);
Compare the FIR and IIR designs. Compute the cost of the two implementations.
hfvt = fvtool(LP_FIR,LP_IIR,'Fs',Fs,'Color','White');
legend(hfvt,'FIR Equiripple, N = 120', 'IIR Elliptic, N = 10');
cost_FIR = cost(LP_FIR)
cost_IIR = cost(LP_IIR)
cost_FIR =
NumCoefficients:
NumStates:
MultiplicationsPerInputSample:
AdditionsPerInputSample:
121
120
121
120
1-27
DSP Tutorials
cost_IIR =
NumCoefficients:
NumStates:
MultiplicationsPerInputSample:
AdditionsPerInputSample:
25
20
25
20
The FIR and IIR filters have similar magnitude responses. The cost of the IIR filter is
about 1/6 the cost of the FIR filter.
Running the IIR Filters
The IIR filter is designed as a biquad filter. To apply the filter to data, use the same
commands as in the FIR case.
1-28
Lowpass Filter Design in MATLAB
Filter 10 seconds of white Gaussian noise with zero mean and unit standard deviation
in frames of 256 samples with the 10th-order IIR lowpass filter. View the result on a
spectrum analyzer.
SA = dsp.SpectrumAnalyzer('SampleRate',Fs,'SpectralAverages',5);
tic
while toc < 10
x = randn(256,1);
y = step(LP_IIR,x);
step(SA,y);
end
1-29
DSP Tutorials
Variable Bandwidth FIR and IIR Filters
You can also design filters that allow you to change the cutoff frequency at run-time.
dsp.VariableBandwidthFIRFilter and dsp.VariableBandwidthIIRFilter can
be used for such cases.
1-30
Lowpass IIR Filter Design in Simulink
Lowpass IIR Filter Design in Simulink
In this section...
Model on page 1-31
Butterworth Filter on page 1-32
Chebyshev Type I Filter on page 1-34
Chebyshev Type II Filter on page 1-36
Elliptic Filter on page 1-38
Minimum-Order Designs on page 1-40
This example shows how to design classic lowpass IIR filters in Simulink. The initial
focus is on the situation in which the critical design parameter is the cutoff frequency
where the filter's power decays to half (-3 dB) the nominal passband value.
The example shows how to replace a Butterworth design with either a Chebyshev or
elliptic filter of the same order and obtain a steeper roll-off at the expense of some ripple
in the passband and/or stopband of the filter. The example also explores minimum-order
designs.
Model
This example is based on the Simulink model ex_iir_design. The model includes one
lowpass filter for each of the four designs: Butterworth, Chebyshev type 1, Chebyshev
type 2, and elliptic. You can use the link under Select Filter to switch between designs
at any point during the simulation.
A stereo audio signal sampled at 44.1 kHz is passed through the filter. The audio input is
corrupted by N(0,10-5) Gaussian noise passed through a highpass filter. The original and
filtered signals are displayed in a spectrum analyzer. You can listen to either the original
or filtered signal by toggling the Manual Switch at the input of the To Audio Device
block.
The filters in this example all use direct-form II second-order sections (SOS).
model = 'ex_iir_design';
open_system(model);
set_param(model,'StopTime','1024/44100 * 100')
sim(model);
1-31
DSP Tutorials
Butterworth Filter
The first filter block is an 8-th order Butterworth lowpass filter with a cutoff frequency
of 5 kHz. This filter is maximally flat (no ripple in the passband or in the stopband).
Double click on the block to open the block dialog. You can visualize the filter's frequency
response by clicking the View Filter Response button on the block's dialog.
1-32
Lowpass IIR Filter Design in Simulink
1-33
DSP Tutorials
Chebyshev Type I Filter
A Chebyshev type I design allows you to control the passband. There are still no ripples
in the stopband. Larger ripples enable a steeper roll-off. In this model, the peak-to-peak
ripple is specified to be 0.5 dB.
1-34
Lowpass IIR Filter Design in Simulink
1-35
DSP Tutorials
Zooming in on the passband, you can see that the ripples are contained in the range [-0.5,
0] dB.
Chebyshev Type II Filter
A Chebyshev type II design allows you to control the stopband attenuation. There are no
ripples in the passband. A smaller stopband attenuation enables a steeper roll-off. In this
example, the stopband attenuation is 80 dB.
1-36
Lowpass IIR Filter Design in Simulink
1-37
DSP Tutorials
Elliptic Filter
An elliptic filter can provide steeper roll-off compared to previous designs by allowing
ripples in both the stopband and passband. To illustrate this, use the same passband and
stopband characteristics specified in the Chebyshev designs.
1-38
Lowpass IIR Filter Design in Simulink
1-39
DSP Tutorials
Minimum-Order Designs
In cases where you specify the passband and stopband in terms of frequencies and the
amount of tolerable ripple, you can use a minimum-order design. As an example, the
Order mode of the Butterworth filter is set to Minimum. The normalized passband and
stopband frequencies are set to 0.1*22050 Hz and 0.3*22050 Hz. The passband ripple
is 1 dB and the stopband attenuation is 60 dB. A 7-th order filter is necessary to meet the
specifications with a Butterworth design. By following the same approach on the other
filter blocks, you can verify that a 5-th order filter is required for Chebyshev type I and
type II designs. A 4-th order filter is sufficient for the elliptic design.
1-40
Lowpass IIR Filter Design in Simulink
1-41
DSP Tutorials
The following figure shows the magnitude response for the 7-th order Butterworth
design.
The pole-zero plot for the 7-th order Butterworth design shows the expected clustering of
7 poles around an angle of zero radians on the unit circle and the corresponding 7 zeros
at angle of radians.
1-42
Lowpass IIR Filter Design in Simulink
Close the model to finish the tutorial.
close_system(model,0);
1-43
DSP Tutorials
Create Moving Average System object
In this section...
Introduction on page 1-44
Create the Class Definition on page 1-45
Moving Average Filter Properties on page 1-45
Moving Average Filter Constructor on page 1-46
Moving Average Filter Setup on page 1-47
Moving Average Filter Step on page 1-47
Moving Average Filter Reset on page 1-48
Input Validation on page 1-48
Object Saving and Loading on page 1-48
System object Usage in MATLAB on page 1-49
Simulink Customization Methods on page 1-50
System object Usage in Simulink on page 1-50
Introduction
This example shows how to create a System object that implements a moving average
filter. The example shows how to use the System object in MATLAB and Simulink
through the MATLAB System block. MovingAverageFilter is a simple moving
average System object filter, which computes the unweighted mean of the previous
WindowLength input samples, where WindowLength is the length of the moving
average window.
The System object accepts single-precision and double-precision 2-D input matrices.
Each column of the input matrix is treated as an independent (1-D) channel. The first
dimension of the input defines the length of the channel (or the input frame size).
MovingAverageFilter independently computes the moving average of each input
channel over time.
System object Usage in MATLAB on page 1-49 and System object Usage in
Simulink on page 1-50 show how to use your System object with data.
1-44
Create Moving Average System object
Create the Class Definition
In the MATLAB Home tab select New -> System Object -> Simulink Extension to
open a System object template. This template includes customizations of the System
object for use in the MATLAB System block. You can edit the template file, using it as
guideline, to create your own System object.
Replace all occurrences of Untitled in the file with MovingAverageFilter and save
the file as MovingAverageFilter.m in a folder where you have write permission. You
need to add this folder to the MATLAB path to use the System object. For convenience,
the entire System object is provided in the file dspdemo.MovingAverageFilter.m. To
view this file enter
edit dspdemo.MovingAverageFilter
at the MATLAB command prompt. The prefix dspdemo on
dspdemo.MovingAverageFilter is a package name. Packages are special folders
that can contain class folders, function and class definition files, and other packages.
Package folders always begin with the + character such as +dspdemo. Packages define
the scope of the contents of the package folder (that is, a namespace in which names
must be unique). This means function and class names need to be unique only within
the package. Using a package provides a means to organize classes and functions and
to select names for these components that other packages can reuse. You do not have
to use packages when creating your System object. For more information on packages
in MATLAB, see Packages Create Namespaces. The remainder of this example shows
you how to create the MovingAverageFilter object from the System object template
without using a package. However, you can also review and use the completed version,
dspdemo.MovingAverageFilter.
Moving Average Filter Properties
The MovingAverageFilter object has one public property that controls the length of
the moving average. Because the algorithm depends on this value being constant once
data processing begins, the property is defined as nontunable. Additionally, the property
only accepts real, positive integers. To ensure correct input, add the PositiveInteger
attribute to the property. The default value of this property is 5.
properties (PositiveInteger, Nontunable)
% Win