Académique Documents
Professionnel Documents
Culture Documents
Up: MUMT 307: Week #1 Previous: Signal Spectra Matlab Signals A Couple Examples Matlab Audio I/O Computing Audio Spectra in Matlab Other Useful Functions Writing Matlab Functions
Matlab Signals
In Matlab, one manipulates vectors or matrices of raw numbers. Elements of a matrix should be separated by spaces and/or commas and specified within brackets ([ ]). Rows are separated by semicolons.
>> [ 1 2 3; 4 5 6 ] ans = 1 4 2 5 3 6
www.music.mcgill.ca//matlab.html
1/5
17-Jun-13
The previously defined row vector can be transposed to a column vector using either the transpose() function or the .' operator:
>> x.' ans = 1 2 3 4 5 6
The colon (:) operator is especially useful in Matlab. It can be used to create vectors of regularly spaced values as follows:
>> 2 : 0.2 : 4 ans = 2.00 2.20 2.40 2.60 2.80 3.00 3.20 3.40 3.60 3.80 4.00
As well, the colon operator can be used to select an entire row or column of a matrix:
>> x = [ 1 2 3; 4 5 6 ] x = 1 4 >> x(:, 1) ans = 1 4 2 5 3 6
The multiplication operator (*) implies matrix multiplication, which is only possible when the matrices being multiplied have compatible dimensions (i.e., the number of columns of the first matrix is equal to the number of rows of the second):
>> [1 2 3] * [4 5 6].' ans = 32
Pointwise operations on arrays, such as squaring each element of a matrix, are accomplished by proceeding the desired operator with a period (.):
>> [1 2 3; 4 5 6].^2 ans = 1 16 4 25 9 36
www.music.mcgill.ca//matlab.html
2/5
17-Jun-13
Matlab functions can be combined in a single statement. For example, the mathematical operation represented by
The clear function can be used to partially or completely erase any previously defined variables in your workspace. The reshape function can be used to resize an existing vector or matrix.
A Couple Examples
To create a simple sinusoidal signal:
fs = 44100; T = 1/fs; t = [0:T:0.25]; f1 = 50; omega1 = 2*pi*f1; phi = 2*pi*0.75; x1 = cos(omega1*t + phi); plot(t, x1); xlabel('Time (seconds)'); ylabel('x1'); title('Simple Sinusoid'); sound(0.9*x1, fs); % sampling rate % sampling period % time vector % frequency in Hertz % angular frequency in radians % arbitrary phase offset = 3/4 cycle % sinusoidal signal, amplitude = 1 % plot the signal
xcomplex = x1 + x2 + x3 + x4 + x5; plot(t, xcomplex); xlabel('Time (seconds)'); ylabel('xcomplex'); title('More Complex Signal'); sound(0.9*xcomplex, fs); % play the signal
17-Jun-13
Audio files formatted with the NeXT/SUN audio file format can be read and written to/from Matlab using the built-in auread and auwrite functions. Signal can be played out the computer audio hardware in most versions of Matlab via the sound (unnormalized) or soundsc (normalized) functions. Example Matlab script and soundfile: wavinout.m, guitar.wav
Well designed Matlab functions will check argument values and sizes to avoid undefined conditions. Example Matlab function: dumbfun.m
www.music.mcgill.ca//matlab.html
4/5
17-Jun-13
Up: MUMT 307: Week #1 Previous: Signal Spectra 2004-2013 McGill University. All Rights Reserved. Maintained by Gary P. Scavone.
www.music.mcgill.ca//matlab.html
5/5