Signal Gs
Signal Gs
R2023b
How to Contact MathWorks
Phone: 508-647-7000
Overview
1
Signal Processing Toolbox Product Description . . . . . . . . . . . . . . . . . . . . . 1-2
v
Filter Design with the Filter Designer App
4
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
vi Contents
1
Overview
1 Overview
Signal Processing Toolbox provides functions and apps to manage, analyze, preprocess, and extract
features from uniformly and nonuniformly sampled signals. The toolbox includes tools for filter design
and analysis, resampling, smoothing, detrending, and power spectrum estimation. You can use the
Signal Analyzer app for visualizing and processing signals simultaneously in time, frequency, and
time-frequency domains. With the Filter Designer app you can design and analyze FIR and IIR digital
filters. Both apps generate MATLAB® scripts to reproduce or automate your work.
Using toolbox functions, you can prepare signal datasets for AI model training by engineering
features that reduce dimensionality and improve the quality of signals. You can access and process
collections of files and large datasets using signal datastores. With the Signal Labeler app, you can
annotate signal attributes, regions, and points of interest to create labeled signal sets. The toolbox
supports GPU acceleration in addition to C/C++ and CUDA® code generation for desktop prototyping
and embedded system deployment.
1-2
2
Representing Signals
In this section...
“Numeric Arrays” on page 2-2
“Vector Representation” on page 2-2
Numeric Arrays
The central data construct in the MATLAB environment is the numeric array, an ordered collection of
real or complex numeric data with two or more dimensions. The basic data objects of signal
processing (one-dimensional signals or sequences, multichannel signals, and two-dimensional signals)
are all naturally suited to array representation.
Vector Representation
MATLAB represents ordinary one-dimensional sampled data signals, or sequences, as vectors. Vectors
are 1-by-n or n-by-1 arrays, where n is the number of samples in the sequence. One way to introduce
a sequence is to enter it as a list of elements at the command prompt. The statement
x = [4 3 7 -9 1];
creates a simple five-element real sequence in a row vector. Transposition turns the sequence into a
column vector
x = x';
x =
4
3
7
-9
1
Column orientation is preferable for single channel signals because it extends naturally to the
multichannel case. For multichannel data, each column of a matrix represents one channel. Each row
of such a matrix then corresponds to a sample point. A three-channel signal that consists of x, 2x,
and x/π is
y = [x 2*x x/pi]
y =
4.0000 8.0000 1.2732
3.0000 6.0000 0.9549
7.0000 14.0000 2.2282
-9.0000 -18.0000 -2.8648
1.0000 2.0000 0.3183
If the sequence has complex-valued elements, the transpose operator takes the conjugate of the
sequence elements. To transform a complex-valued row vector into a column vector without taking
conjugates, use the .' or non-conjugate transpose:
2-2
Waveform Generation: Time Vectors and Sinusoids
Most toolbox functions require you to begin with a vector representing a time base. Consider
generating data with a 1000 Hz sample frequency, for example. An appropriate time vector is
t = (0:0.001:1)';
where the MATLAB® colon operator (:) creates a 1001-element row vector that represents time
running from 0 to 1 seconds in steps of 1 ms. The transpose operator (') changes the row vector into
a column; the semicolon (;) tells MATLAB to compute, but not display, the result.
Given t, you can create a sample signal y consisting of two sinusoids, one at 50 Hz and one at 120 Hz
with twice the amplitude.
y = sin(2*pi*50*t) + 2*sin(2*pi*120*t);
The new variable y, formed from vector t, is also 1001 elements long. You can add normally
distributed white noise to the signal and plot the first 50 points:
yn = y + 0.5*randn(size(t));
plot(t(1:50),yn(1:50))
2-3
2 Basic Signal Processing Concepts
Since MATLAB® is a programming language, an endless variety of different signals is possible. Here
are some statements that generate a unit impulse, a unit step, a unit ramp, and a unit parabola.
t = (-1:0.01:1)';
impulse = t==0;
unitstep = t>=0;
ramp = t.*unitstep;
quad = t.^2.*unitstep;
All of these sequences are column vectors that inherit their shapes from t. Plot the sequences.
Generate and plot a square wave with period 0.5 and amplitude 0.81.
sqwave = 0.81*square(4*pi*t);
plot(t,sqwave)
2-4
Impulse, Step, and Ramp Functions
2-5
2 Basic Signal Processing Concepts
Signal Processing Toolbox™ provides functions for generating widely used periodic waveforms.
• sawtooth generates a sawtooth wave with peaks at ±1 and a period of 2π. An optional width
parameter specifies a fractional multiple of 2π at which the signal maximum occurs.
• square generates a square wave with a period of 2π. An optional parameter specifies the duty
cycle, the percent of the period for which the signal is positive.
Generate 1.5 seconds of a 50 Hz sawtooth wave with a sample rate of 10 kHz. Plot 0.2 seconds of the
generated waveform.
fs = 10e3;
t = 0:1/fs:1.5;
x = sawtooth(2*pi*50*t);
plot(t,x)
axis([0 0.2 -1 1])
Generate 1.5 seconds of a 50 Hz square wave with a sample rate of 10 kHz. Specify a duty cycle of
25%. Plot 0.2 seconds of the generated waveform.
fs = 10e3;
t = 0:1/fs:1.5;
x = square(2*pi*50*t,25);
2-6
Common Periodic Waveforms
plot(t,x)
axis([0 0.2 -1 1])
Use the dutycycle function to verify that the duty cycle of the square wave is the specified value.
Use the function with no output arguments to plot the waveform, the location of the mid-reference
level instants, the associated reference levels, the state levels, and the associated lower and upper
state boundaries.
dc = dutycycle(x,fs);
dc = dc(1)
dc = 0.2500
dutycycle(x,fs);
xlim([0 0.2])
2-7
2 Basic Signal Processing Concepts
See Also
dutycycle | sawtooth | square
2-8
Common Aperiodic Waveforms
Signal Processing Toolbox™ provides functions for generating several widely used aperiodic
waveforms.
Compute 2 seconds of a linear chirp signal with a sample rate of 1 kHz that starts at DC and crosses
150 Hz at 1 second.
t = 0:1/1000:2;
y = chirp(t,0,1,150);
Plot the spectrogram of the chirp. Specify 90% of overlap between adjoining windowed segments.
pspectrum(y,t,'spectrogram','OverlapPercent',90)
Use gauspuls to plot a 50 kHz Gaussian RF pulse with 60% bandwidth, sampled at a rate of 1 MHz.
Truncate the pulse where the envelope falls 40 dB below the peak.
2-9
2 Basic Signal Processing Concepts
tc = gauspuls('cutoff',50e3,0.6,[],-40);
t = -tc : 1e-6 : tc;
yi = gauspuls(t,50e3,0.6);
plot(t,yi)
See Also
chirp | gauspuls | pspectrum
2-10
The pulstran Function
The pulstran function generates pulse trains from either continuous or sampled prototype pulses.
This example generates a pulse train consisting of the sum of multiple delayed interpolations of a
Gaussian pulse.
The pulse train is defined to have a sample rate of 50 kHz, a pulse train length of 10 ms, and a pulse
repetition rate of 1 kHz. T specifies the time instants at which the pulse train is sampled. D specifies
the delay to each pulse repetition in the first column and an optional attenuation for each repetition
in the second column. To construct the pulse train, pass the name of the gauspuls function to
pulstran, along with additional parameters that specify a 10 kHz Gaussian pulse with 50%
bandwidth.
T = 0:1/50e3:10e-3;
D = [0:1/1e3:10e-3;0.8.^(0:10)]';
Y = pulstran(T,D,'gauspuls',10e3,0.5);
plot(T,Y)
See “Compute Envelope Spectrum of Vibration Signal” for an example that uses the pulstran
function to generate vibration data for bearing analysis.
2-11
2 Basic Signal Processing Concepts
See Also
pulstran
2-12
The Sinc Function
The sinc function computes the mathematical sinc function for an input vector or matrix x. Viewed
as a function of time, or space, the sinc function is the inverse Fourier transform of the rectangular
pulse in frequency centered at zero, with width 2π and unit height:
sin πx
, x ≠ 0,
∫
1 π
sinc x = e jωx dω = πx
2π −π
1, x = 0.
To plot the sinc function for a linearly spaced vector with values ranging from −5 to 5, use these
commands:
x = linspace(-5,5);
y = sinc(x);
plot(x,y)
grid
See Also
diric | sinc
2-13
2 Basic Signal Processing Concepts
The function diric computes the Dirichlet function, sometimes called the periodic sinc or aliased
sinc function, for an input vector or matrix x. The Dirichlet function is defined by
sin(Nx/2)
, x ≠ 2πk,
D(x) = Nsin(x/2) k = 0, ± 1, ± 2, ± 3, …
k(N − 1)
( − 1) , x = 2πk,
where N is a user-specified positive integer. For N odd, the Dirichlet function has a period of 2π; for
N even, its period is 4π. The magnitude of this function is 1/N times the magnitude of the discrete-
time Fourier transform of the N-point rectangular window.
subplot(2,1,1)
plot(x/pi,diric(x,7))
title('N = 7')
subplot(2,1,2)
plot(x/pi,diric(x,8))
title('N = 8')
xlabel('x / \pi')
2-14
The Dirichlet Function
See Also
diric | sinc
2-15
2 Basic Signal Processing Concepts
Data Precision
All Signal Processing Toolbox functions accept double-precision inputs. If you input single-precision
floating-point or integer data types, you should not expect to receive correct results and in many
cases, an error will occur. DSP System Toolbox™ and Fixed-Point Designer™ products enable single-
precision floating-point and fixed-point support for most dfilt structures.
2-16
Selected Bibliography
Selected Bibliography
Algorithm development for Signal Processing Toolbox functions has drawn heavily upon the
references listed below. All are recommended to the interested reader who needs to know more about
signal processing than is covered in this manual.
References
[1] Crochiere, R. E., and Lawrence R. Rabiner. Multi-Rate Signal Processing. Englewood Cliffs, NJ:
Prentice Hall, 1983. pp. 88–91.
[2] IEEE. Programs for Digital Signal Processing. IEEE Press. New York: John Wiley & Sons, 1979.
[3] Jackson, L. B. Digital Filters and Signal Processing. Third Ed. Boston: Kluwer Academic
Publishers, 1989.
[4] Kay, Steven M. Modern Spectral Estimation. Englewood Cliffs, NJ: Prentice Hall, 1988.
[5] Oppenheim, Alan V., and Ronald W. Schafer. Discrete-Time Signal Processing. Englewood Cliffs,
NJ: Prentice Hall, 1989.
[6] Parks, Thomas W., and C. Sidney Burrus. Digital Filter Design. New York: John Wiley & Sons,
1987.
[7] Percival, D. B., and A. T. Walden. Spectral Analysis for Physical Applications: Multitaper and
Conventional Univariate Techniques. Cambridge: Cambridge University Press, 1993.
[8] Pratt, W. K. Digital Image Processing. New York: John Wiley & Sons, 1991.
[9] Proakis, John G., and Dimitris G. Manolakis. Digital Signal Processing: Principles, Algorithms, and
Applications. Upper Saddle River, NJ: Prentice Hall, 1996.
[10] Rabiner, Lawrence R., and Bernard Gold. Theory and Application of Digital Signal Processing.
Englewood Cliffs, NJ: Prentice Hall, 1975.
[11] Welch, P. D. “The Use of Fast Fourier Transform for the Estimation of Power Spectra: A Method
Based on Time Averaging Over Short, Modified Periodograms.” IEEE® Transactions on Audio
and Electroacoustics. Vol. AU-15, 1967. pp. 70–73.
2-17
3
Note You must have the Signal Processing Toolbox installed to use fdesign and filterBuilder.
Advanced capabilities are available if your installation additionally includes the DSP System Toolbox
license. You can verify the presence of both toolboxes by typing ver at the command prompt.
Filter design through user-defined specifications is the core of the fdesign approach. This
specification-centric approach places less emphasis on the choice of specific filter algorithms, and
more emphasis on performance during the design a good working filter. For example, you can take a
given set of design parameters for the filter, such as a stopband frequency, a passband frequency, and
a stopband attenuation, and— using these parameters— design a specification object for the filter.
You can then implement the filter using this specification object. Using this approach, it is also
possible to compare different algorithms as applied to a set of specifications.
The distinction between these two objects is at the core of the filter design methodology. The basic
attributes of each of these objects are outlined in the following table.
You can run the code in the following examples from the Help browser (select the code, right-click the
selection, and choose Evaluate Selection from the context menu), or you can enter the code on the
MATLAB command line. Before you begin this example, start MATLAB and verify that you have
installed the Signal Processing Toolbox software. If you wish to access the full functionality of
fdesign and filterBuilder, you should additionally obtain the DSP System Toolbox software. You
can verify the presence of these products by typing ver at the command prompt.
3-2
Design a Filter Using fdesign
Assume that you want to design a bandpass filter. Typically a bandpass filter is defined as shown in
the following figure.
In this example, a sampling frequency of Fs = 48 kHz is used. This bandpass filter has the following
specifications, specified here using MATLAB code:
In the following two steps, these specifications are passed to the fdesign.bandpass method as
parameters.
Step 1
To create a filter specification object, evaluate the following code at the MATLAB prompt:
d = fdesign.bandpass
Now, pass the filter specifications that correspond to the default Specification —
fst1,fp1,fp2,fst2,ast1,ap,ast2. This example adds fs as the final input argument to specify
the sampling frequency of 48 kHz.
3-3
3 Design a Filter with fdesign and Filter Builder
Note The order of the filter is not specified, allowing a degree of freedom for the algorithm
design in order to achieve the specification. The design will be a minimum order design.
The specification parameters, such as Fstop1, are all given default values when none are
provided. You can change the values of the specification parameters after the filter specification
object has been created. For example, if there are two values that need to be changed, Fpass2
and Fstop2, use the set command, which takes the object first, and then the parameter value
pairs. Evaluate the following code at the MATLAB prompt:
>> set(BandPassSpecObj, 'Fpass2', 15800, 'Fstop2', 18400)
BandPassSpecObj is the new filter specification object which contains all the required design
parameters, including the filter type.
You may also change parameter values in filter specification objects by accessing them as if they
were elements in a struct array.
>> BandPassSpecObj.Fpass2=15800;
Step 2
Design the filter by using the design command. You can access the design methods available for
you specification object by calling the designmethods function. For example, in this case, you
can execute the command
>> designmethods(BandPassSpecObj)
butter
cheby1
cheby2
ellip
equiripple
kaiserwin
After choosing a design method use, you can evaluate the following at the MATLAB prompt (this
example assumes you've chosen 'equiripple'):
>> BandPassFilt = design(BandPassSpecObj, 'equiripple')
BandPassFilt =
If you have the DSP System Toolbox installed, you can also design your filter with a filter System
object™. To create a filter System object with the same specification object BandPassSpecObj,
you can execute the commands
>> designmethods(BandPassSpecObj,...
'SystemObject',true)
3-4
Design a Filter Using fdesign
butter
cheby1
cheby2
ellip
equiripple
kaiserwin
System: dsp.FIRFilter
Properties:
Structure: 'Direct form'
NumeratorSource: 'Property'
Numerator: [1x44 double]
InitialConditions: 0
FrameBasedProcessing: true
Available design methods and design options for filter System objects are not necessarily the
same as those for filter objects.
Note If you do not specify a design method, a default method will be used. For example, you can
execute the command
BandPassFilt =
To check your work, you can plot the filter magnitude response using the Filter Visualization tool.
Verify that all the design parameters are met:
If you have the DSP System Toolbox installed, the Filter Visualization tool produces the following
figure with the dashed red lines indicating the transition bands and unity gain (0 in dB) over the
passband.
3-5
3 Design a Filter with fdesign and Filter Builder
3-6
Design a Filter Using Filter Builder
filterBuilder
2 Select Bandpass filter response from the list in the dialog box, and hit the OK button.
3 Enter the correct frequencies for Fpass2 and Fstop2, then click OK. Here the specification uses
normalized frequency, so that the passband and stopband edges are expressed as a fraction of
the Nyquist frequency (in this case, 48/2 kHz). The following message appears at the MATLAB
prompt:
If you display the Workspace tab, you see the object Hbp has been placed on your workspace.
4 To check your work, plot the filter magnitude response using the Filter Visualization tool. Verify
that all the design parameters are met:
3-7
3 Design a Filter with fdesign and Filter Builder
Note that the dashed red lines on the preceding figure will only appear if you are using the DSP
System Toolbox software.
3-8
4
Introduction
This section describes how to graphically design and implement digital filters using Signal Processing
Toolbox. Filter design is the process of creating the filter coefficients to meet specific frequency
specifications. Filter implementation involves choosing and applying a particular filter structure to
those coefficients. Only after both design and implementation have been performed can your data be
filtered.
This section includes a brief discussion of applying the completed filter design and filter
implementation using MATLAB command line functions, such as filter.
4-2
Designing the Filter
filterDesigner
The app opens with a default filter. Its filter information is summarized in the upper left (Current
Filter Information) and its filter specifications are depicted in the upper right. In addition to
displaying filter specification, this upper right pane displays filter responses and filter
coefficients.
The bottom half of the app shows the Filter Design panel, where you specify the filter
parameters. Other panels, such as Import filter from workspace and Pole/Zero Editor, which you
access with the buttons on the lower left, are also displayed in this area. If you have other
products installed, you may see additional buttons.
Note that when you open the app, Design Filter is not enabled. You must make a change to the
default filter design in order to enable Design Filter. This is true each time you want to change
the filter design. Changes to radio button items or drop down menu items such as those under
Response Type or Filter Order enable Design Filter immediately. Changes to specifications in
text boxes such as Fs, Fpass, and Fstop require you to click outside the text box to enable
Design Filter.
2 In the Response Type pane, select Bandpass.
3 In the Design Method pane, select IIR, and then select Butterworth from the selection list.
4-3
4 Filter Design with the Filter Designer App
4 For the Filter Order, select Specify order, and then enter 6.
6 After specifying the filter design parameters, click the Design Filter button at the bottom of the
design panel to compute the filter coefficients. The display updates to show the magnitude
response of the designed filter.
4-4
Designing the Filter
Notice that the Design Filter button is disabled after you compute the coefficients for your filter
design. This button is enabled again if you make any changes to the filter specifications.
7 Click the Store Filter button.
8 In the Store Filter dialog, change the filter name to Bandpass Butterworth-1 and click OK to
save the filter in the Filter Manager.
4-5
4 Filter Design with the Filter Designer App
Magnitude response
Phase response
Group delay
Phase delay
Impulse response
Step response
Pole-zero plot
Filter coefficients
Filter information
Note Other analyses are available if you have the DSP System Toolbox product installed.
4-6
Analyzing the Filter
4-7
4 Filter Design with the Filter Designer App
1 Using the parameters listed in the table above, for each table row, set the appropriate the Fc1
and Fc2 values.
2 Design the filter by clicking the Design Filter button.
3 Click Store Filter to save the filter.
4 Change the name to the appropriate filter name shown in the table above.
5 Repeat these steps until all 10 filters are designed and stored.
4-8
Viewing and Annotating the Filter
1 Click the Filter Manager button to display the Filter Manager, which lists your saved filters.
2 Press Ctrl+click on each filter name to select all the filters, and then click FVTool. FVTool opens
with the filter responses overlaid for easy comparison. (If you want to view a single filter in
FVTool, click the Full View Analysis button when that filter is shown in the app’s display panel
or select View > Filter Visualization Tool).
3 Change the x-axis scale to logarithmic by selecting Analysis > Analysis Parameters to display
the Analysis Parameters dialog.
4 Change the Frequency Scale to Log.
4-9
4 Filter Design with the Filter Designer App
5 Click OK.
6
Click the Legend button to turn on the legend, which you can drag to the desired location.
4-10
Viewing and Annotating the Filter
Use the Zoom button and drag a rectangle around the first few passbands to zoom in.
4-11
4 Filter Design with the Filter Designer App
8
Click the Restore Default View button to return to the full view.
9 Display other responses, as desired. (The FVTool Analysis toolbar buttons and Analysis menu are
the same as in Filter Designer. See “Analyzing the Filter” on page 4-6 for descriptions of the
buttons.
Note Do not close Filter Designer at this time. You will use it in future sections.
1 Use the toolbar buttons to annotate your response plot. Add a line by clicking one of the line
buttons, and then use your mouse to draw the line on your plot.
2 Add a data tip by clicking on a plot at the desired point. The data tip shows the frequency and
magnitude at that point.
4-12
Exporting Filters from Filter Designer
1 In Filter Designer, click Filter Manager and highlight only the Bandpass Butterworth-1
filter.
2 Select File > Export.
3 Set Export to to Workspace. Set Export as to Objects. In Discrete Filter type Hd1. Click
Export to export the first filter in your filter bank to an Hd1 dfilt object in the workspace.
4 Repeat steps 1 through 3 for each of the remaining nine filters. Highlight each filter individually
to make it the active filter and change the Discrete Filter name to match the filter number.
When you finish you will have 10 dfilt objects in the workspace.
5 Close the app by selecting File > Close.
6 On the MATLAB command line, verify that your objects were exported by using the whos
command.
whos
Name Size Bytes Class Attributes
4-13
4 Filter Design with the Filter Designer App
Hd = [Hd1 Hd2 Hd3 Hd4 Hd5 Hd6 Hd7 Hd8 Hd9 Hd10];
2 To view the first filter, type Hd(1).
Hd(1)
ans =
FilterStructure: 'Direct-Form II, Second-Order Sections'
sosMatrix: [3x6 double]
ScaleValues: [3.40097054256801e-009;1;1;1]
PersistentMemory: false
3 A number of methods can be used to view and manipulate the Hd1 dfilt object. Try the info
command:
This produces the same display as step 5 of “Viewing the Filter in FVTool” on page 4-9.
5 Now using the MATLAB command line, create some discrete white Gaussian noise data, which
you can then filter using the filter bank.
plot(yw)
4-14
Exporting Filters from Filter Designer
4-15