Matlab-Slides Cambridge
Matlab-Slides Cambridge
Introduction to MATLAB
Markus Kuhn
https://www.cl.cam.ac.uk/teaching/1920/TeX+MATLAB/
2 / 21
What MATLAB is not
3 / 21
Open-source MATLAB alternatives
Similar to MATLAB, subset compatible: GNU Octave, SciLab, FreeMat
Other high-level languages for technical computing:
I R – focus on statistics and plotting
https://www.r-project.org/
I Python – a full-featured programming language. Modules:
• numpy – numerical arrays, fast linear algebra
• matplotlib – MATLAB-like plotting functions
https://matplotlib.org/
• SciPy – scientific computing, Pandas – data analysis, etc.
I Julia – modern, fast, full-featured, compiled, interactive language
https://julialang.org/
• MATLAB-inspired syntax (especially for arrays)
• combines dynamic and static type systems, multiple dispatch
• LLVM backend, can also call C, C++, Python, R, Fortran functions
• LISP-like metaprogramming, rich flexible type system
I others: LuaJIT (SciLua), Perl Data Language (PDL), OCaml (Owl)
Jupyter is a popular browser-based notebook environment for recording
and presenting experiments in Julia, Python, R, . . .
4 / 21
Local availability
MATLAB is installed and ready to use on
I Intel Lab, etc.: MCS Windows
I Intel Lab, etc.: MCS Linux (/usr/bin/matlab)
I CL MCS Linux server: ssh -X linux.cl.ds.cam.ac.uk
I MCS Linux server: ssh -X linux.ds.cam.ac.uk
I Computer Laboratory managed Linux PCs
cl-matlab -> /usr/groups/matlab/current/bin/matlab
Locally installed MATLAB may be half a year behind the latest release. If you spot problems with
the MCS MATLAB installation, please do let the lecturer know (→ [email protected]).
6 / 21
MATLAB matrices (1)
7 / 21
MATLAB matrices (2)
Colon generates number sequence: Specify step size with second colon:
>> 11:14 >> 1:3:12
ans = ans =
11 12 13 14 1 4 7 10
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.
8 / 21
MATLAB matrices (3)
9 / 21
MATLAB matrices (4)
10 / 21
Review: matrix multiplication
• • • • • • •
• • •
• • • •
• • • •
• • •
· • • • • =
• • • •
• • • • • • • • • • •
• • • • • • •
11 / 21
Review: matrix multiplication
• • • •
• • • •
• • • •
·
• • • • • • •
• • •
• • • •
• • •
=
• • • •
• • • • • • •
• • • • • • •
11 / 21
Review: matrix multiplication
• • • •
• • • •
• • • •
· k
• • • • • • •
• • •
• • • •
• • •
• • • •
• • • • • • •
• • • • • • •
11 / 21
Review: matrix multiplication
• • • •
• • • •
• • • •
· k
• • • • • • •
• • •
• • • •
• • •
• • • •
• • • • • • •
• • • • • • •
11 / 21
Review: matrix multiplication
• • • •
• • • •
• • • •
· k
• • • • • • •
• • •
• • • •
• • •
• • • •
• • • • • • •
• • • • • • •
11 / 21
Review: matrix multiplication
• • • •
• • • •
• • • •
· k
• • • • • • •
• • •
• • • •
• • •
• • • •
• • • • • • •
• • • • • • •
11 / 21
Review: inner and outer product of vectors
•
•
• • • • ·
• =
12 / 21
Review: inner and outer product of vectors
•
•
• • • • ·
• =
• = •
•
12 / 21
Review: inner and outer product of vectors
•
•
• • • • ·
• =
• = •
•
12 / 21
Review: inner and outer product of vectors
•
•
• • • • ·
• =
• = •
•
12 / 21
Review: inner and outer product of vectors
•
•
• • • • ·
• =
• = •
•
12 / 21
Review: inner and outer product of vectors
•
•
• • • • ·
• =
• = •
•
12 / 21
MATLAB matrices (5)
√
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
13 / 21
Plotting
0.8
0.6
0.6 0.4
0.2
0.4
0
0.2 -0.2
-0.4
0 0 2 4 6 8 10
0 5 10 15 20
x = 0:20; t = 0:0.1:10;
y = 0.5 - 0.5*cos(2*pi * x/20); x = exp(t * (j - 1/3));
stem(x, y); plot(t, real(x), t, imag(x));
title('20-point raised cosine'); grid; legend('real', 'imaginary')
14 / 21
2D plotting
-20
1
-15
0.5
-10
0 -5
-0.5 0
20
20
0 10 5
0
-10
-20 -20
10
15
xl = -20:0.3:20;
yl = -20:0.3:20;
[x,y] = meshgrid(xl, yl); 20
-20 -10 0 10 20
r = sqrt(x.^2 + y.^2);
s = sin(r) ./ r; s(find(r==0)) = 1; imagesc(xl, yl, s, [-1 1]);
plot3(x, y, s); colormap(gray);
grid on; set(gca, 'DataAspectRatio', [1 1 1]);
15 / 21
Some common functions and operators
16 / 21
Functions and m-files
17 / 21
Example: generating an audio illusion
18 / 21
Spectrogram of the first 6 s:
-40
3.5
3 -60
Power/frequency (dB/Hz)
2.5
Frequency (kHz)
-80
2
-100
1.5
-120
1
0.5 -140
0
1 2 3 4 5
Time (secs)
19 / 21
Example solution:
t = 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
l = mod(((0:n-1)/n)' * ones(1, fs*d) + ones(n,1) * (t/(d*n)), 1);
f = fmin * (fmax/fmin) .^ l; % freq. for each tone and sample
p = 2*pi * cumsum(f, 2) / fs; % phase for each tone and sample
% make last column a multiple of 2*pi for phase continuity
p = diag((2*pi*floor(p(:,end)/(2*pi))) ./ p(:,end)) * p;
s = sin(p); % sine value for each tone and sample
% mixing amplitudes from raised-cosine curve over frequency
a = 0.5 - 0.5 * cos(2*pi * l);
w = sum(s .* a)/n; % mix tones together, normalize to [-1, +1]
A variant of this audio effect, where each tone is exactly one octave (factor 2 in frequency) from
the next, is known as the Shepard–Risset glissando.
What changes to the parameters would produce that?
20 / 21
MATLAB, Julia, NumPy: comparative cheat sheet
MATLAB Julia NumPy