Digital Signal Processing Matlab
Digital Signal Processing Matlab
Markus Kuhn
Computer Laboratory
Michaelmas 2009
What is MATLAB
high-level language (garbage collecting, var-len structures) BASIC-like syntax, with elements from C, GUI IDE basic data type: 2- or 3-dimensional oating-point matrix most operators and functions work on entire matrices hardly ever necessary to write out loops uses internally highly optimized numerics libraries (BLAS, LAPACK, FFTW)
comprehensive toolboxes for easy access to standard algorithms from many elds: statistics, image processing, signal processing, neural networks, wavelets, communications systems very simple I/O for many data/multimedia le formats popular for experimental/rapid-prototype number crunching widely used as a visualization and teaching tool
2
Some of these limitations have been reduced in recent releases, e.g. release 13 replaced slow interpreter with a JIT compiler (JVM).
Free alternatives: GNU Octave (http://www.octave.org/) reimplements a MATLAB subset. SciLab (http://www.scilab.org/) is another MATLAB-like package. R specializes on statistics and plotting (http://www.r-project.org/). Python, a full-featured programming language, has with its numpy and matplotlib packages (http://matplotlib.sourceforge.net/) also evolved into a serious competitor and MATLAB-lookalike. 3
not a high-performance language (but fast matrix operators) not freely available
Computer Laboratory Windows and Linux PCs Start matlab, then type helpdesk
http://www.mathworks.com/access/helpdesk/help/helpdesk.html
Read Getting Started section of the MATLAB manual Use the command help function-name
PWF MATLAB may be a year behind the latest release. If you spot problems with the PWF MATLAB installation, please do let the lecturer know ( [email protected]). 4
6 7 2
Assignments and subroutine calls normally end with a semicolon. Without, MATLAB will print each result. Useful for debugging! Results from functions not called inside an expression are assigned to the default variable ans. Type help magic for the manual page of this library function.
13
14
10
2.0000
Single matrix cell: a(2,3) == 7. Vectors as indices select several rows and columns. When used inside a matrix index, the variable end provides the highest index value: a(end, end-1) == 9. Using just : is equivalent to 1:end and can be used to select an entire row or column.
6
5]' * [1 7 11] 14 21 35 22 33 55
The imaginary unit vector 1 is available as both i and j, and matrices can be complex. Related functions: real, imag, conj, exp, abs, angle
Exercise 1 Find a short MATLAB expression to build the matrix 1 2 3 4 5 6 7 B = 9 7 5 3 1 1 3 4 8 16 32 64 128 256 Exercise 2 Give a MATLAB expression that uses only a single matrix multiplication with B to obtain (a) the sum of columns 5 and 7 of B (b) the last row of B (c) a version of B with rows 2 and 3 swapped Exercise 3 Give a MATLAB expression that multiplies obtain 0 1 1 2 3 4 5 1 2 3 4 5 (a) the matrix (b) the matrix 2 3 1 2 3 4 5 4 two vectors to 0 1 2 3 4 0 1 2 3 4
10
Plotting
20point raised cosine 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0
0.4 0.2 0 0.2 0.6 0.4 1 0.8 real imaginary
10
15
20
10
Plotting functions plot, semilogx, semilogy, loglog all expect a pair of vectors for each curve, with x and y coordinates, respectively. Use saveas(gcf, 'plot2.eps') to save current gure as graphics le.
11
2D plotting
20
1
15 10 5 0
20 0 20 10 20 10 0
0.5
0.5 20
5 10
xl = -20:0.3:20; yl = -20:0.3:20; [x,y] = meshgrid(xl, yl); r = sqrt(x.^2 + y.^2); s = sin(r) ./ r; s(find(r==0)) = 1; plot3(x, y, s); grid on;
15 20 20
10
10
20
12
cross/auto-correlation sequence
find
list non-zero indices
csvread, csvwrite
comma-separated-value I/O
figure, saveas
open new gure, save gure
13
0.5
1.5 Time
2.5
16
Example solution:
t % % % l f p % p s % a w = 0:1/fs:d-1/fs; % timestamps for each sample point normalized logarithm of frequency of each tone (row) for each sample point (column), all rising linearly from 0 to 1, then wrap around back to 0 = mod(([0:n-1]/n)' * ones(1, fs*d) + ones(n,1) * (t/(d*n)), 1); % freq. for each tone and sample = fmin * (fmax/fmin) .^ l; = 2*pi * cumsum(f, 2) / fs; % phase for each tone and sample make last column a multiple of 2*pi for phase continuity = diag((2*pi*floor(p(:,end)/(2*pi))) ./ p(:,end)) * p; = sin(p); % sine value for each tone and sample mixing amplitudes from raised-cosine curve over frequency = 0.5 - 0.5 * cos(2*pi * l); = sum(s .* a)/n; % mix tones together, normalize to [-1, +1]
w = repmat(w, 1, 3); % repeat waveform 3x specgram(w, 2048, fs, 2048, 1800); ylim([0 4000]) % plot w = repmat(w, 1, 20); % repeat waveform 20x wavwrite(w, fs, 16, 'ladder.wav'); % make audio file
17
Exercise 4 Modify slide 17 to produce tones of falling frequency instead. Exercise 5 (a) Write down the function g (t) that has the shape of a sine wave that increases linearly in frequency from 0 Hz at t = 0 s to 5 Hz at t = 10 s. (b) Plot the graph of this function using MATLABs plot command. (c) Add to the same gure (this can be achieved using the hold command) in a dierent colour a graph of the same function sampled at 5 Hz, using the stem command. (d) Plot the graph from (c) separately. Try to explain its symmetry (hint: sampling theorem, aliasing). Exercise 6 Use MATLAB to write an audio waveform (8 kHz sampling frequency) that contains a sequence of nine tones with frequencies 659, 622, 659, 622, 659, 494, 587, 523, and 440 Hz. Then add to this waveform a copy of itself in which every other sample has been multiplied by 1. Play the waveform, write it to a WAV le, and use the specgram command to plot its spectrogram with correctly labelled time and frequency axis.
18