- Matlab Signals
- A Couple Examples
- Matlab Audio I/O
- Computing Audio Spectra in Matlab
- Other Useful Functions
- Writing Matlab Functions

- Matlab can be used to create and manipulate discrete-time signals.
- Individual expressions can be typed directly inside the Matlab interpreter. Collections of commands can be saved in text-files or scripts (with .m extensions) and then run from the command-line. Users can also write Matlab functions.
- Matlab operations are optimized for matrix algebra. Loops tend to execute more slowly.
- Matlab functions can be compiled as C executables to speed up performance (though you must purchase the compiler).
- Matlab is not free and its pricing structure is very complex.
- An open-source alternative to Matlab called Octave is available.
- Useful functions: size, abs, sum, plot, axis, stem, fft, ifft, grid, ...
- One can get help for any function by typing
`help`and a function name at the command-line prompt (ex.`help plot`).

- 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 2 3 4 5 6

- We will often work with row or column vectors:
>> x = [ 1 2 3 4 5 6 ] X = 1 2 3 4 5 6

- 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

- The increment value can be omitted, in which case it defaults to 1:
>> 1:10 ans = 1 2 3 4 5 6 7 8 9 10

- 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 2 3 4 5 6 >> x(:, 1) ans = 1 4

- 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 4 9 16 25 36

- Matlab functions can be combined in a single statement. For example, the mathematical operation represented by

can be implemented in Matlab as:>> sum( log( abs(x) ) )

- 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.

- To create a simple sinusoidal signal:
fs = 44100; % sampling rate T = 1/fs; % sampling period t = [0:T:0.25]; % time vector f1 = 50; % frequency in Hertz omega1 = 2*pi*f1; % angular frequency in radians phi = 2*pi*0.75; % arbitrary phase offset = 3/4 cycle x1 = cos(omega1*t + phi); % sinusoidal signal, amplitude = 1 plot(t, x1); % plot the signal xlabel('Time (seconds)'); ylabel('x1'); title('Simple Sinusoid'); sound(0.9*x1, fs); % play the signal

- To create a more complex signal composed of many sinusoids:
phi = 2 * pi * 0.25; % 1/4 cycle phase offset x1 = cos(omega1*t + phi); % sinusoidal signal, amplitude = 1 x2 = cos(2*pi*150*t + phi)/3; % sinusoidal signal, amplitude = 1/3 x3 = cos(2*pi*250*t + phi)/5; % sinusoidal signal, amplitude = 1/5 x4 = cos(2*pi*350*t + phi)/7; % sinusoidal signal, amplitude = 1/7 x5 = cos(2*pi*450*t + phi)/9; % sinusoidal signal, amplitude = 1/9 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

- In newer versions of Matlab, the functions
`audioread`and`audiowrite`can be used to read and write data to/from various types of audio files. Unfortunately, the Matlab documentation does not specify exactly which typies of audio files are supported. - Audio files using the Microsoft WAV format can be read and written to/from Matlab using the built-in
`wavread`and`wavwrite`functions (these functions will be removed in a future release). - Audio files using the NeXT/SUN audio file format can be read and written to/from Matlab using the built-in
`auread`and`auwrite`functions (these functions will be removed in a future release). - 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`

- The
`fft`function computes the FFT of a specified signal. - In general, we will want to view either the magnitude or phase values of the FFT coefficients, which in Matlab can be determined using the
`abs`and`angle`functions. - A variety of windows can be applied to a signal before the computation of the FFT using the functions
`hann`,`hamming`,`blackman`. For a complete list, see the`window`function help. Time-domain windows can help minimize spectral artifacts related to signal truncation. - The
`spectrogram`function computes a time-frequency plot of a signal where color represents spectral magnitude amplitude. - Example Matlab script and soundfile:
`wavfft.m`,`triangle.wav`

- The
`clear`function clears all Matlab variables. Individual variables can be cleared by specifying them as arguments to the`clear`function. - Matlab provides a ``C-like''
`fprintf`function to format output data to a file or the terminal.

- It is relatively easy to create your own Matlab functions. An example is included below:
function y = dumbfun(x, z) % DUMBFUN An example Matlab function. % % Y = DUMBFUN(X,Z) doesn't do much. The Z parameter is optional % and should either be a scalar or equal in size to X. % % By Gary P. Scavone, McGill University, 2004. if nargin>1 & z>0, if size(z) == [1 1] | size(z) == size(x), y = 0.5.*x + z; else error('Parameter Z size error.'); return end else y = 0.4.*x; end

- Well designed Matlab functions will check argument values and sizes to avoid undefined conditions.
- Example Matlab function:
`dumbfun.m`

©2004-2015 McGill University. All Rights Reserved. Maintained by Gary P. Scavone. |