You are on page 1of 5

17-Jun-13

Audio Processing in Matlab

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

Audio Processing in Matlab


Matlab is widely used environment for signal processing and analysis. In this section, we introduce some key Matlab concepts and functions that are useful for music and audio. 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).

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

We will often work with row or column vectors:


>> x = [ 1 2 3 4 5 6 ] X = 1 2 3 4 5 6

www.music.mcgill.ca//matlab.html

1/5

17-Jun-13

Audio Processing in Matlab

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

Audio Processing in Matlab

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.

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

% play the signal

To create a more complex signal composed of many sinusoids:


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

Matlab Audio I/O


Matlab provides a few built-in functions that allow one to import and export audio files. Audio files formatted with the Microsoft WAV format can be read and written to/from Matlab using the built-in wavread and wavwrite functions.
www.music.mcgill.ca//matlab.html 3/5

17-Jun-13

Audio Processing in Matlab

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

Computing Audio Spectra in Matlab


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

Other Useful Functions


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.

Writing Matlab Functions


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

www.music.mcgill.ca//matlab.html

4/5

17-Jun-13

Audio Processing in Matlab

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

You might also like