Upload
aditish-dede-etniz
View
215
Download
0
Embed Size (px)
Citation preview
7/27/2019 Audio_Processing_in_Matlab.pdf
1/5
Up:MUMT 307: Week #1Previous:Signal Spectra
Matlab Signals
A Couple Examples
Matlab Audio I/O
Computing Audio Spectra in Matlab
Other Useful FunctionsWriting 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 calledOctave is available.
Useful functions: size, abs, sum, plot, axis, stem, fft, ifft, grid, ...
One can get help for any function by typing hel p and a function name at the command-line prompt
(ex. hel p pl ot ).
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 2 34 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
17-Jun-13 Audio Processing in Matlab
www.music.mcgill.ca//matlab.html 1/5
7/27/2019 Audio_Processing_in_Matlab.pdf
2/5
The previously defined row vector can be transposed to a column vector using either the
t r anspose( ) function or the . ' operator:
>> x. '
ans =
123
456
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 34 5 6
>> x( : , 1)
ans =
14
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 tothe 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 916 25 36
17-Jun-13 Audio Processing in Matlab
www.music.mcgill.ca//matlab.html 2/5
7/27/2019 Audio_Processing_in_Matlab.pdf
3/5
Matlab functions can be combined in a single statement. For example, the mathematical operation
represented by
can be implemented in Matlab as:
>> sum( l og( abs( x) ) )
The cl ear function can be used to partially or completely erase any previously defined variables in
your workspace.
The r eshape function can be used to resize an existing vector or matrix.
A Couple Examples
To create a simple sinusoidal signal:
f s = 44100; % sampl i ng r ateT = 1/ f s; % sampl i ng per i odt = [ 0: T: 0. 25] ; % t i me vect or
f 1 = 50; % f r equency i n Hert zomega1 = 2*pi *f 1; % angul ar f r equency i n r adi ans
phi = 2*pi *0. 75; % arbi t r ary phase of f set = 3/ 4 cycl ex1 = cos(omega1*t + phi ) ; % si nusoi dal si gnal , ampl i t ude = 1
pl ot ( t , x1) ; % pl ot t he s i gnal
xl abel ( ' Ti me ( seconds) ' ) ;yl abel ( ' x1' ) ;t i t l e( ' Si mpl e Si nusoi d' ) ;
sound( 0. 9*x1, f s) ; % pl ay t he si gnal
To create a more complex signal composed of many sinusoids:
phi = 2 * pi * 0. 25; % 1/ 4 cycl e phase of f setx1 = cos(omega1*t + phi ) ; % si nusoi dal si gnal , ampl i t ude = 1x2 = cos( 2*pi *150*t + phi ) / 3; % si nusoi dal si gnal , ampl i t ude = 1/ 3x3 = cos( 2*pi *250*t + phi ) / 5; % si nusoi dal si gnal , ampl i t ude = 1/ 5x4 = cos( 2*pi *350*t + phi ) / 7; % si nusoi dal si gnal , ampl i t ude = 1/ 7
x5 = cos( 2*pi *450*t + phi ) / 9; % si nusoi dal si gnal , ampl i t ude = 1/ 9
xcompl ex = x1 + x2 + x3 + x4 + x5;
pl ot ( t , xcompl ex);xl abel ( ' Ti me ( seconds) ' ) ;yl abel ( ' xcompl ex' ) ;t i t l e( ' Mor e Compl ex Si gnal ' ) ;
sound( 0. 9*xcompl ex, f s) ; % pl ay t he si gnal
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 wavr ead andwavwr i t e functions.
17-Jun-13 Audio Processing in Matlab
www.music.mcgill.ca//matlab.html 3/5
7/27/2019 Audio_Processing_in_Matlab.pdf
4/5
Audio files formatted with the NeXT/SUN audio file format can be read and written to/from Matlab
using the built-in auread andauwr i t e functions.
Signal can be played out the computer audio hardware in most versions of Matlab via the sound
(unnormalized) orsoundsc (normalized) functions.
Example Matlab script and soundfile: wavi nout . m, gui t ar. wav
Computing Audio Spectra in Matlab
The f f t 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 andangl e functions.
A variety of windows can be applied to a signal before the computation of the FFT using the functions
hann, hammi ng, bl ackman. For a complete list, see the wi ndowfunction help. Time-domain windows
can help minimize spectral artifacts related to signal truncation.
The spect r ogr amfunction computes a time-frequency plot of a signal where color represents spectralmagnitude amplitude.
Example Matlab script and soundfile: wavf f t . m, t r i angl e. wav
Other Useful Functions
The cl ear function clears all Matlab variables. Individual variables can be cleared by specifying them
as arguments to the cl ear function.
Matlab provides a ``C-like'' f pr i nt f 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:
f unct i on y = dumbfun( x, z)% DUMBFUN An exampl e Mat l ab f unct i on.%% Y = DUMBFUN( X, Z) doesn' t do much. The Z par amet er i s opt i onal% and shoul d ei t her be a scal ar or equal i n si ze t o X.%
% By Gar y P. Scavone, McGi l l Uni ver si t y, 2004.
i f nar gi n>1 & z>0,i f si ze( z) == [ 1 1] | si ze( z) == si ze( x),
y = 0. 5. *x + z;el se
er r or( ' Par ameter Z s i ze er r or . ' ) ;return
endel se
y = 0. 4. *x;end
Well designed Matlab functions will check argument values and sizes to avoid undefined conditions.
Example Matlab function: dumbf un. m
17-Jun-13 Audio Processing in Matlab
www.music.mcgill.ca//matlab.html 4/5
7/27/2019 Audio_Processing_in_Matlab.pdf
5/5
Up:MUMT 307: Week #1Previous:Signal Spectra
2004-2013 McGill University. All Rights Reserved.
Maintained by Gary P. Scavone.
17-Jun-13 Audio Processing in Matlab
www.music.mcgill.ca//matlab.html 5/5