Matlab DSP
Matlab DSP
Matlab DSP
Introduction to MATLAB
and Simulink
Appendix A
This appendix provides a quick reference for using MATLAB with its toolboxes
and Simulink with its blocksets for DSP applications. These tools are used exten-
sively in the experiments and examples in this book. This appendix covers useful
topics related to DSP in MATLAB, the Signal Processing Toolbox, the Filter Design
Toolbox, Simulink, the DSP Blockset, and the Fixed-Point Blockset. More detailed
descriptions are documented in the MATLAB, toolbox, and Simulink users guides
listed in references at the end of this appendix.
A.1 USING MATLAB
MATLAB stands for MATrix LABoratory and is a technical-computing language
that allows the user to perform numerical computation, simulation, acquisition, and
visualization of data, algorithm design, analysis, development, and implementation.
Unlike other high-level programming languages such as C, MATLAB provides a com-
prehensive suite of mathematical, statistical, and engineering functions. The functional-
ity is extended with interactive graphical capabilities for creating plots. Furthermore,
extensive toolboxes are available for working under the MATLAB environment. Tool-
boxes are collections of algorithms, written by experts in their fields, that provide appli-
cation-specific capabilities. These toolboxes enhance MATLABs functionality in
KuoAppAv3.qxd 2/16/04 3:07 PM Page 1
2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
2
G
2
G
2
2 * 2
vector = [0 2 4 6 8 10] r = 0:2:10
G * g
g+g
g. * g
g * g'
3 * 3 G = [1 2 3; 4 5 6; 7 8 9]
13 * 12 p = [1 2 3]'
11 * 32 g = [1 2 3]
can represent and manipulate scalars, vectors, and matrices in the MATLAB
workspace, as shown by the examples summarized in Table A.1.
A.1.3 Plots
MATLAB is known for its outstanding graphics capabilities. Both 2-D and 3-D
graphics can be plotted and manipulated using some commonly used commands
summarized in Table A.2.
MATLAB version 6 (or higher) provides a set of powerful editing tools for
editing graphics. It supports a point-and-click editing mode to modify the plots.
KuoAppAv3.qxd 2/16/04 3:07 PM Page 4
2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
2 - Y.
22;
[X,Y] = meshgrid 1[-2:0.1:2]2;
plot31sin1t2, cos1t2,t2
t = 0:pi/50:5 * pi
axis1[0 10 -1 1]2
y2 = cos12 * pi * x/82;
y1 = sin12 * pi * x/82;
step = 1 x = 0:1:50;
1x,y,z2 = 1number
x = exp1-10. * t2;
t = 0:0.001:1;
Graphical objects can be selected by clicking on the object, and multiple objects can
be selected by shift-clicking. Several editing features are highlighted in Fig. A.2.
Besides selecting, moving, resizing, changing color, cutting, copying, and past-
ing graphic objects, MATLAB also provides tools for zoom-in, zoom-out, camera
KuoAppAv3.qxd 2/16/04 3:07 PM Page 5
2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
Section A.2 Using Digital Signal Processing Toolboxes and Interactive Tools A11
TABLE A.6 Signal-Processing Functions (adapted from [3])
Filter analysis Description
filternorm Two-norm or inf-norm of a digital filter
freqs Frequency response of an analog filter
freqspace Frequency spacing for a frequency response
freqz Frequency response of a digital filter
freqzplot Plots frequency-response data
grpdelay Group delay of a filter
impz Impulse response of a digital filter
unwrap Unwraps a phase angle
zplane Zero-pole plot
Filter
implementation Description
conv Convolution and polynomial multiplication
conv2 2-D convolution
deconv Deconvolution and polynomial division
fftfilt FFT-based FIR filtering using overlap-add
filter FIR or IIR filtering
filter2 2-D digital filtering
filtfilt Zero-phase digital filtering
filtic Initial condition of a transposed form-II IIR filter
latcfilt Lattice and lattice-ladder filtering
medfilt1 1-D median filtering
sgolayfilt SavitzkyGolay filtering
sosfilt Second-order (biquad) IIR filtering
upfirdn Upsample, FIR filtering, and downsample
FIR filter design Description
convmtx Convolution matrix
cremez Complex and nonlinear-phase equiripple FIR
fir1 Window-based FIR-filter design
fir2 Frequency sampling-based FIR-filter design
fircls Constrained least-square FIR-filter design (multiband),
fircls1 (lowpass and highpass)
firls Least-square linear-phase FIR-filter design
firrcos Raised-cosine FIR-filter design
intfilt Interpolation FIR-filter design
kaiserord FIR-filter design using a Kaiser window
remez Computes a ParksMcClellan optimal
remezord FIR-filter design and filter-order estimation
sgolay SavitzkyGolay filter design
IIR filter design Description
bilinear Bilinear transformation
butter Butterworth filter design
buttord Order and cutoff frequency for a Butterworth filter
(Continued)
KuoAppAv3.qxd 2/16/04 3:07 PM Page 11
2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
Section A.2 Using Digital Signal Processing Toolboxes and Interactive Tools A13
Windows Description
triang Triangular window
tukeywin Tukey window
window Window-function gateway
Transforms Description
bitrevorder Permutes input into a bit-reversed order
czt Chirp z-transform
dct Discrete cosine transform
dftmtx DFT matrix
fft 1-D FFT
fft2 2-D FFT
fftshift Moves zero-th lag to the center of the spectrum
goertzel Second-order Goertzel algorithm
hilbert Computes an analytic signal using a Hilbert transform
idct Inverse-discrete cosine transform
ifft 1-D IFFT
ifft2 2-D IFFT
Cepstral analysis Description
cceps Complex cepstral analysis
icceps Inverse-complex cepstrum
rceps Real cepstrum, minimum phase reconstruction
Statistical and
spectrum analysis Description
cohere Estimates the magnitude-square coherence function
corrcoef Correlation coefficients
corrmtx Autocorrelation matrix
cov Covariance matrix
csd Cross-spectral density
pburg Power-spectrum density (PSD) estimate using Burgs method
pcov PSD estimate using a covariance method
peig PSD estimate using an eigenvector method
periodogram PSD estimate using a periodogram method
pmcov PSD estimate using a modified covariance method
pmtm PSD estimate using a Thomson multitaper method
pmusic PSD estimate using the MUSIC method
psdplot Plots PSD data
pwelch PSD estimate using Welchs method
pyulear PSD estimate using the YuleWalker autoregressive method
rooteig Frequency and power estimation using the eigenvector algorithm
rootmusic Frequency and power estimation using the MUSIC algorithm
tfe Transfer function estimate
xcorr Crosscorrelation function
xcorr2 2-D crosscorrelation
xcov Covariance function
(Continued)
TABLE A.6 (Continued)
KuoAppAv3.qxd 2/16/04 3:07 PM Page 13
2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
Section A.2 Using Digital Signal Processing Toolboxes and Interactive Tools A15
Figure A.3 SPTool window
Figure A.4 Signal Browser window
2. The Filter Designer is used for designing digital FIR and IIR filters. The user
simply clicks on the New icon for a new filter or the Edit icon for an existing fil-
ter under the Filter column in SPTool to open the Filter Designer window, as
KuoAppAv3.qxd 2/16/04 3:07 PM Page 15
2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
Section A.2 Using Digital Signal Processing Toolboxes and Interactive Tools A17
addition, the size of the FFT, window functions, and overlapping samples can
be selected to complete the power-spectrum density (PSD) estimation.
SPTool also provides a useful tool for exporting signals, filter parameters, and
spectra to the MATLAB workspace or files. These saved parameters are represent-
ed in MATLAB as a structure of signals, filters, and spectra. More information can
be found in the Signal Processing Toolbox Users Guide [3]. A step-by-step example
of using SPTool in designing an IIR filter is given in Chapter 2.
A.2.3 Filter Design Toolbox
The Filter Design Toolbox is a collection of tools that provides advanced techniques
for designing, simulating, and analyzing digital filters. It extends the capabilities of
Figure A.6 Filter Viewer window
Figure A.7 Apply Filter window
KuoAppAv3.qxd 2/16/04 3:07 PM Page 17
2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
Section A.2 Using Digital Signal Processing Toolboxes and Interactive Tools A19
allows the user to compute quantized FFTs and IFFTs. These functions ease the task
of determining the effects of quantization on real-world designs. Quantization tools
allow the user to model the behavior of fixed-point filters and FFT algorithms pre-
cisely. The quantized algorithms in the toolbox exactly match the output of the algo-
rithms implemented on a fixed-point processor because the simulation is bit-true.
The Filter Design Toolbox includes a new GUI tool called FDATool. This tool
allows the user to design optimal FIR and IIR filters from scratch, import previous-
ly designed filters, quantize floating-point filters, and analyze quantization effects.
This tool is introduced in the next section.
A.2.4 Filter Design and Analysis Tool
This interactive tool provides several advanced techniques that support designing,
simulating, and analyzing fixed-point and floating-point filters for a wide range of
precision. This tool performs the following functions:
1. Designs filters
2. Converts filters between different structures
3. Quantizes filters
4. Quantizes data
5. Quantizes FFT and IFFT
6. Designs adaptive filters
In addition to the existing functions listed in Table A.6, FDATool contains the addi-
tional filter-design functions listed in Table A.7.
FDATool can be activated by typing
fdatool
in the MATLAB command window. The Filter Design & Analysis Tool window is
shown in Fig. A.9. This window includes tools similar to those shown in the Filter
TABLE A.7 Additional Filter Design Functions in FDATool
Filter design function Description
firlpnorm Designs minimax FIR filters using the
least-Pth algorithm
gremez Designs optimal FIR filters (Remez
exchange)
iirgrpdelay Designs IIR filters (specifies group delay
in the passband)
iirlpnorm Designs minimax IIR filters using the
least-Pth algorithm
iirlpnormc Designs minimax IIR filters using the
least-Pth algorithm, which restricts filter
poles and zeros within a fixed radius
KuoAppAv3.qxd 2/16/04 3:07 PM Page 19
2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
Section A.2 Using Digital Signal Processing Toolboxes and Interactive Tools A21
Figure A.10 Convert Structure window
Optimization button (the meanings of these options are explained in Chapter 7). Fur-
thermore, the direct-form IIR filter can also be converted to a cascade of second-
order sections for a more stable implementation. This option can be activated by
clicking on to Second-Order sections. This option is only applicable
to IIR filters and is explained in more detail in Chapter 7.
In addition to these features, the new version of FDATool also provides an
option for frequency transformation, which can be accessed by clicking on the
Transform Filter icon . This option transforms the original designed filter to
another filter with different characteristics. It also contains a Filter Realization Wiz-
ard (Realize Model) that allows the user to create a fixed-point or floating-point
filter blockset, which can be used in the Simulink environment. Furthermore, filters
can also be imported from variables or discrete-time filter objects in the workspace
by clicking on the Import Filter icon .
Besides using FDATool in specifying the quantized filter, we can also con-
struct a quantized filter object using the function qfilt in MATLAB. For example,
Hq=qfilt('quantizer',{'float',[32 8],'round'})
Edit :Convert
Figure A.11 Setting quantization parameters in FDATool
KuoAppAv3.qxd 2/16/04 3:07 PM Page 21
2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4
For exclusive distribution with the book Digital Signal Processors: Architectures, Implementations, and
Applications by Sen M. Kuo and Woon-Seng Gan, ISBN 0-13-035214-4