Matlab Answers
Matlab Answers
Markus Kuhn
Michaelmas 2006
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
Example solution: b = [1:7; 9:-2:-3; 2.^(2:8)]
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
Example solution: (a) b * [0 0 0 0 1 0 1] (b) [0 0 1] * b (c) [1 0 0; 0 0 1; 0 1 0] * b
Exercise 3 Give a MATLAB expression that multiplies two 0 0 1 1 1 2 3 4 5 1 2 3 4 5 (b) the matrix 2 2 (a) the matrix 3 3 1 2 3 4 5 4 4
Example solution: (a) [1 1 1] * (1:5) (b) (0:4) * [1 1 1]
vectors to obtain 0 1 2 3 4
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).
Example solution: (a) The instantaneous frequency of function g(t) at time t is f (t) = t 5 Hz t = 10 s 2 s2
and since the phase of a sine wave is 2 times the integrated frequency so far, we get
t
g(t) = sin 2
0
f (t ) dt
= sin 2
t2 4 s2
= sin
t2 2 s2
(b+c)
0.8
0.6
0.4
0.2
0.2
0.4
0.6
0.8
10
t = 0:0.01:10; f = sin(pi*t.^2/2); plot(t,f); hold; t2 = 0:1/5:10; stem(t2, sin(pi*t2.^2/2), r); (d) A sine wave with a frequency f larger than half the sampling frequency fs cannot be distinguished based on the sample values from a sine wave of frequency fs f . In other words, the sample values would have looked the same had we replaced the instantaneous frequency f (t) with fs /2 |fs /2 f (t)|, and the latter is symmetric around fs /2, which is in this graph 2.5 Hz and occurs at t = 5 s.
The above is of course just a hand-waving argument, but shall be sucient for this exercise. There are actually a few more conditions fullled here that lead to the exact symmetry of the plot. Firstly, since we started sampling at t = 0 s with fs = 5 Hz, the positions of the sample values end up being symmetric around t = 5 s. Secondly, at the symmetry point t = 5 s, the sine wave was at a symmetric peak from where increasing or decreasing the phase has the same result.
0.8
0.6
0.4
0.2
0.2
0.4
0.6
0.8
10
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.
Example solution:
4000
f = [659 622 659 622 659 494 587 523 440]; fs = 8000; % sampling frequency d = 0.5; % duration per tone t = 0:1/fs:d-1/fs; w = sin(2 * pi * f * t)/2; w = w; w = w(:); w = [w, w .* (mod((1:length(w)), 2) * 2 - 1)]; wavwrite(w, fs, 16, matlab_answer-2.wav); specgram(w, [], fs);
3500
3000
2500 Frequency
2000
1500
1000
500
0 1 2 3 4 Time 5 6 7 8