What MATLAB is not → not a computer algebra system Introduction to MATLAB → not a strong general purpose programming language • limited support for other data structures Markus Kuhn • few software-engineering features; typical MATLAB programs are only a few lines long • not suited for teaching OOP • limited GUI features → not a high-performance language (but fast matrix operators) Computer Laboratory → not freely available 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 Michaelmas 2010 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 What is MATLAB Availability and documentation → high-level language (garbage collecting, var-len structures) → Installed on → BASIC-like syntax, with elements from C, GUI IDE • Intel Lab PWF Windows → basic data type: 2- or 3-dimensional floating-point matrix • Intel Lab PWF Linux ( /usr/bin/matlab ) → most operators and functions work on entire matrices • PWF servers linux{2,3}.pwf.cl.cam.ac.uk ⇒ hardly ever necessary to write out loops • Computer Laboratory Windows and Linux PCs → uses internally highly optimized numerics libraries → Full documentation available online in HTML and PDF (BLAS, LAPACK, FFTW) → comprehensive toolboxes for easy access to standard algorithms • Start matlab , then type helpdesk from many fields: statistics, image processing, signal process- • http://www.mathworks.com/access/helpdesk/help/helpdesk.html ing, neural networks, wavelets, communications systems → Read “Getting Started” section of the MATLAB manual → very simple I/O for many data/multimedia file formats → Use the command help function-name → popular for experimental/rapid-prototype number crunching → widely used as a visualization and teaching tool 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 ( → mgk25@cl.cam.ac.uk ). 2 4
MATLAB matrices (1) MATLAB matrices (3) Select rows, columns and Matrices can also be accessed as a Generate a “magic square” with equal row/column/diagonal sums and submatrices of a : 1-dimensional vector: assign the resulting 3 × 3 matrix to variable a : >> a(1:5) >> a(1,:) >> a = magic(3) ans = ans = a = 8 3 4 1 5 8 1 6 8 1 6 3 5 7 >> a(6:end) >> a(:,1) ans = 4 9 2 ans = 9 6 7 2 8 Assignments and subroutine calls normally end with a semicolon. 3 Without, MATLAB will print each result. Useful for debugging! >> b = a(1:4:9) 4 ans = Results from functions not called inside an expression are assigned to 8 5 2 the default variable ans . >> a(2:3,1:2) ans = Type help magic for the manual page of this library function. >> size(b) 3 5 ans = 4 9 1 3 5 7 MATLAB matrices (2) MATLAB matrices (4) Use [ ] to build new matrices, where , or space as a delimiter joins sub- Colon generates number sequence: Specify step size with second colon: matrices horizontally and ; joins them vertically. >> 11:14 >> 1:3:12 ans = Mask matrix elements: ans = >> c = [2 7; 3 1] 11 12 13 14 1 4 7 10 c = 2 7 >> find(a > 5) >> -1:1 >> 4:-1:1 3 1 ans = ans = ans = >> d = [a(:,end) a(1,:)'] 1 -1 0 1 4 3 2 1 d = 6 6 8 7 >> 3:0 >> 3:-0.5:2 7 1 8 ans = ans = 2 6 >> a(find(a > 5)) = 0 Empty matrix: 1-by-0 3.0000 2.5000 2.0000 >> e = [zeros(1,3); a(2,:)] a = e = 0 1 0 Single matrix cell: a(2,3) == 7 . Vectors as indices select several rows and 0 0 0 3 5 0 columns. When used inside a matrix index, the variable end provides the 3 5 7 4 0 2 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 8
MATLAB matrices (5) Plotting 20−point raised cosine 1 1 real Operators on scalars and matrices: Inner and outer vector product: 0.9 imaginary 0.8 0.8 0.6 >> [2 3 5] * [1 7 11]' 0.7 >> [1 1; 1 0] * [2 3]' 0.6 ans = 0.4 ans = 0.5 78 0.2 0.4 5 >> [2 3 5]' * [1 7 11] 0.3 0 2 ans = 0.2 −0.2 >> [1 2 3] .* [10 10 15] 0.1 2 14 22 ans = −0.4 0 0 2 4 6 8 10 3 21 33 0 5 10 15 20 10 20 45 5 35 55 x = 0:20; t = 0:0.1:10; y = 0.5 - 0.5*cos(2*pi * x/20); x = exp(t * (j - 1/3)); The imaginary unit vector √− 1 is available as both i and j , and stem(x, y); plot(t, real(x), t, imag(x)); title('20-point raised cosine'); grid; legend('real', 'imaginary') matrices can be complex. Plotting functions plot , semilogx , semilogy , loglog all expect a pair of Related functions: real, imag, conj, exp, abs, angle vectors for each curve, with x and y coordinates, respectively. Use saveas(gcf, 'plot2.eps') to save current figure as graphics file. 9 11 2D plotting Exercise 1 Find a short MATLAB expression to build the matrix 1 2 3 4 5 6 7 −20 B = 9 7 5 3 1 − 1 − 3 1 4 8 16 32 64 128 256 −15 0.5 −10 Exercise 2 Give a MATLAB expression that uses only a single matrix multiplication with B to obtain 0 −5 (a) the sum of columns 5 and 7 of B −0.5 0 20 20 (b) the last row of B 10 0 5 0 −10 (c) a version of B with rows 2 and 3 swapped −20 −20 10 Exercise 3 Give a MATLAB expression that multiplies two vectors to 15 xl = -20:0.3:20; obtain yl = -20:0.3:20; 20 [x,y] = meshgrid(xl, yl); −20 −10 0 10 20 0 0 0 r = sqrt(x.^2 + y.^2); 1 2 3 4 5 1 1 1 s = sin(r) ./ r; s(find(r==0)) = 1; imagesc(xl, yl, s, [-1 1]); plot3(x, y, s); colormap(gray); (a) the matrix 1 2 3 4 5 (b) the matrix 2 2 2 grid on; set(gca, 'DataAspectRatio', [1 1 1]); 1 2 3 4 5 3 3 3 4 4 4 10 12
Some common functions and operators Example: generating an audio illusion Generate an audio file with 12 sine tones of apparently continuously *, ^ imread, imwrite, image, exponentially increasing frequency, which never leave the frequency matrix multiplication, exponentiation imagesc, colormap /, \, inv bitmap image I/O range 300–3400 Hz. Do this by letting them wrap around the frequency A/B = AB − 1 , A \ B = A − 1 B, A − 1 plot, semilog { x,y } , loglog interval and reduce their volume near the interval boundaries based on +, -, .*, ./, .^ 2D curve plotting a raised-cosine curve applied to the logarithm of the frequency. element-wise add/sub/mul/div/exp conv, conv2, xcorr ==, ~=, <, >, <=, >= First produce a 1 s long waveform in which each tone raises 1/12 of 1D/2D convolution, relations result in element-wise 0/1 cross/auto-correlation sequence the frequency range, then concatenate that to a 60 s long 16-bit WAV length, size fft, ifft, fft2 file, mono, with 16 kHz sampling rate. Avoid phase jumps. size of vectors and matrices discrete Fourier transform zeros, ones, eye, diag Parameters: sum, prod, min, max all-0, all-1, identity, diag. matrices sum up rows or columns xlim, ylim, zlim fs = 16000; % sampling frequency [Hz] cumsum, cumprod, diff set plot axes ranges d = 1; % time after which waveform repeats [s] cumulative sum or product, xlabel, ylabel, zlabel differentiate row/column fmin = 300; % lowest frequency label plot axes find wavread, wavwrite, sound fmax = 3400; % highest frequency list non-zero indices audio I/O n = 12; % number of tones figure, saveas csvread, csvwrite open new figure, save figure comma-separated-value I/O 13 15 Functions and m-files Spectrogram of the first 3 s: To define a new function, for example decibel ( x ) = 10 x/ 20 , write into 4000 a file decibel.m the lines 3500 function f = decibel(x) 3000 % DECIBEL(X) converts a decibel figure X into a factor f = 10 .^ (x ./ 20); 2500 Frequency Only the function that has the same name as the m-file in which it is defined can be called from outside the file; all other functions are only 2000 visible inside the file. The function keyword sets the variable whose 1500 value will be returned and lists the parameter variables. The m-file must be in the current directory ( cd ) or MATLAB’s search 1000 path ( path ) to become accessible. 500 Use edit db to edit the m-file, help db to show the first comment lines and type db to show its source text. 0 M-files can also contain just sequences of statements instead of a func- 0.5 1 1.5 2 2.5 Time tion definition. These are called simply by typing their name. 14 16
Recommend
More recommend