Audio Processing With MatLab
Audio Processing With MatLab
An Introduction
This lab is an introduction to audio processing with MatLab. This lab will help to familiarize
you with some of the main functions to read in and play music files in MatLab.
To understand how each of these functions is used in MatLab, type help followed by one of the
above commands into the command prompt. Note that in order to use the wavread() function
you must use a .wav file. It is possible to convert .wma and .mp3 files into .wav files. You can
Google search to find programs to do this. If you can’t find one that works, send me an email
and I can forward you some links.
To read and store an audio file, you can use one of two different command lines. The following
stores the file into variable y.
y = wavread(‘filename');
The command line below stores the audio file into variable y and the sampling frequency in
variable Fs.
[y,Fs] = wavread(‘filename');
Section 2: Play the Audio File
To play an audio file in MatLab you use the sound() function. The following function plays the
sound. If the Fs variable is not defined or included in the command, it will assume the default
sample rate of 8192 Hz.
sound(y,Fs);
To scale an audio file the soundsc() command is used. This allows for the modification of an
audio signal’s amplitude or frequency.
soundsc(y,Fs);
To increase the volume of the audio track you can multiple the variable it is stored in by a scalar.
To slow down or speed up the track played you can adjust the sampling rate. Comment on your
observations using different values.
Now experiment with different bit values (1,2,..,16) in the following command:
soundsc(y,Fs,bits);
The command to reverse the order of the samples in a matrix is flipud(). Experiment with this
command.
Now for this part of the lab have fun with MatLab. Take your favorite music files and convert
them to .wav files if they are not already converted. I’m sure you all have audio files from some
source or another!!! If you’re not sure how to convert the files, Google search it.
Once you have a couple of your favorite audio files, you may need to crop the file. Most audio
files are fairly large which can cause MatLab to lock up.
Read and store the cropped audio file. Now use some of the above commands to modify the
audio signal. Play your favorite song backwards or make your favorite singer sound like a
chipmunk. Note that the above commands are just a basic look at MatLab’s audio processing
capabilities.
Comment on your findings!!!
For your help here is a file forward and backward that has been done with the same process
This section of the lab will teach you how to create songs using different tones created in
MatLab. First we are going to code a sine wave of amplitude A = 1, with at an audio frequency
of 523.25 Hz (corresponds to C).
cnote = sin(2*pi*523.25*(0:0.000125:0.5));
This vector cnote now contains simples of the sine wave from t = 0s to t = 0.5s, in samples that
are spaced 0.000125s apart (this is the sampling interval Ts). Note that this sampling interval
corresponds to a sampling frequency of 8 kHz (1/Ts = fs). This is standard for voice grade audio
channels.
Now to write this sound to a wave file we have the following command.
wavwrite(cnote, ‘c.wav’);
Now to play the sound, same as before use the sound() function. That is the single note that you
just created. Now that you’ve got one down… only several more to go!!
I’ve done some of the easy work here for you. The following webpage gives the frequencies of
different notes which are also shown on the table below.
http://www.dolmetsch.com/musictheory27.htm
Note that there are different octaves as you go through the different keys on a piano. To get you
started here are a few:
f = sin(2*pi*174.61*(0:0.000125:0.5));
g = sin(2*pi*195.99*(0:0.000125:0.5));
a = sin(2*pi*220*(0:0.000125:0.5));
b = sin(2*pi*246.94*(0:0.000125:0.5));
line1 = [a,b,c,d,e,f];
line2 = [a,b,c,d,e,f];
The letters should represent the notes that you have created in MatLab. Put the notes in the order
you want them to play.
Add as many lines as you like. Now write your song, read and store it into a new variable, and
listen to it. Comment on your musical abilities. If you’re musically challenged, check out
Google for some songs that you can create. Feel free to create your own song or attempt to copy
some Beethoven. Enjoy!!!
If you need some more help with this lab, check out the following link:
http://users.rowan.edu/~shreek/networks1/music.html
For another example, at the end of this lab I have attached the song that I created which is a few
lines of “Heart and Soul”. Get creative with this assignment and comment on the problems you
run in to.
>> v = sin(2*pi*466.16*[0:0.000125:1.0]);
The vector, v, now contains samples of the sine wave, starting at t=0 s, to
t=1.0 s, in samples spaced 0.000125 s apart (the sampling interval Ts).
This corresponds to a sampling frequency of 8 KHz, which is standard for
voice grade audio channel.
Now, you can either plot this sine wave; or you can hear it!!!
To plot, simply do
>> plot(v);
Now, you can "play" this wav file called asharp.wav using any multimedia
player. For example, you could use the Sound Recorder program. Start this
by pointing to Start - Programs - Accessories - Multimedia - Sound
Recorder. Open the wav file using the File Menu and press Play. If you
have installed a sound card and a pair of speakers on your PC, you should
be able to hear a short note.
Now that you can make a single note, you can put notes together and
make music!!!
Assuming each note lasts for 0.5 seconds, the MATLAB m-file for
creating a wav file for this piece of music would be:
clear;
a=sin(2*pi*440*(0:0.000125:0.5));
b=sin(2*pi*493.88*(0:0.000125:0.5));
cs=sin(2*pi*554.37*(0:0.000125:0.5));
d=sin(2*pi*587.33*(0:0.000125:0.5));
e=sin(2*pi*659.26*(0:0.000125:0.5));
fs=sin(2*pi*739.99*(0:0.000125:0.5));
line1=[a,a,e,e,fs,fs,e,e,];
line2=[d,d,cs,cs,b,b,a,a,];
line3=[e,e,d,d,cs,cs,b,b];
song=[line1,line2,line3,line3,line1,line2];
wavwrite(song,'song.wav');
HINT: Before you code the entire song, just code the first line, create a
wav file, play it and make sure everything works.
Do you recognize this piece? (If not, then we have a lot of work ahead of
us..............) Enjoy!!!!!