


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Matlab Excercises with Solutions Notes Prepared By Markus Kuhn.
Typology: Exercises
1 / 4
This page cannot be seen from the preview
Don't miss anything!
Exercise 1: Find a short MATLAB expression to build the matrix
Answer: 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
Answer: b * [0 0 0 0 1 0 1]'
(b) the last row of B
Answer: [0 0 1] * b
(c) a version of B with rows 2 and 3 swapped
Answer: [1 0 0; 0 0 1; 0 1 0] * b
Exercise 3: Give a MATLAB expression that multiplies two vectors to obtain
(a) the matrix
Answer: [1 1 1]' * (1:5)
(b) the matrix
Answer: (0:4)' * [1 1 1]
Exercise 4: Modify slide 30 to produce tones of falling frequency instead.
Answer: Replace
f = fmin * (fmax/fmin) .^ l;
with
f = fmax * (fmin/fmax) .^ l;
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.
Answer: The instantaneous frequency of function g(t) at time t is
f (t) = t · 5 Hz 10 s = t 2 s^2 and since the phase of a sine wave is 2π times the integrated frequency so far, we get
g(t) = sin
( 2 π
∫ (^) t
0
f (t′) dt′
) = sin
( 2 π t
2 4 s^2
) = sin
( πt^2 2 s^2
)
(b) Plot the graph of this function using MATLAB’s plot command. (c) Add to the same figure (this can be achieved using the hold command) in a different colour a graph of the same function sampled at 5 Hz, using the stem command.
Answer: for (b) and (c)
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)]; audiowrite('matlab_answer-2.wav', w, fs); specgram(w, [], fs);
Time
1 2 3 4 5 6 7 8
Frequency
0
500
1000
1500
2000
2500
3000
3500
4000