Introduction To MATLAB: Markus Kuhn
Introduction To MATLAB: Markus Kuhn
Markus Kuhn
Computer Laboratory
Michaelmas 2010
What is MATLAB
Installed on
Intel Lab PWF Windows
http://www.mathworks.com/access/helpdesk/help/helpdesk.html
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
13
14
>> 3:0
ans =
Empty matrix: 1-by-0
10
>> 4:-1:1
ans =
4
3
>> 3:-0.5:2
ans =
3.0000 2.5000
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
>> a(:,1)
ans =
8
3
4
>> a(2:3,1:2)
ans =
3
5
4
9
>> a(6:end)
ans =
9
6
7
>> b = a(1:4:9)
ans =
8
5
2
>> size(b)
ans =
1
3
7
5] * [1 7 11]'
5]' * [1 7 11]
14
21
35
22
33
55
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
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
1
0.9
real
imaginary
0.8
0.8
0.7
0.6
0.6
0.4
0.5
0.2
0.4
0.3
0.2
0.2
0.1
0
0.4
10
15
x = 0:20;
y = 0.5 - 0.5*cos(2*pi * x/20);
stem(x, y);
title('20-point raised cosine');
20
10
t = 0:0.1:10;
x = exp(t * (j - 1/3));
plot(t, real(x), t, imag(x));
grid; legend('real', 'imaginary')
2D plotting
20
1
15
0.5
10
0.5
20
0
20
10
0
20
10
20
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
length, size
size of vectors and matrices
csvread, csvwrite
comma-separated-value I/O
cross/auto-correlation sequence
find
list non-zero indices
figure, saveas
open new figure, save figure
13
=
=
=
=
=
16000;
1;
300;
3400;
12;
%
%
%
%
%
Frequency
3000
2500
2000
1500
1000
500
0
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);
= fmin * (fmax/fmin) .^ l;
% freq. for each tone and sample
= 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