Lab 2: More MATLAB and Working With Sounds: Objective
Lab 2: More MATLAB and Working With Sounds: Objective
k
A
k
cos(2f
k
t +
k
) =
k
A
k
cos(2kf
0
t +
k
)
where f
0
is the fundamental frequency and f
k
= kf
0
are the harmonics. (We use the k subscript instead of
the argument f, as in A
k
vs. A(f
k
), for brevity.) Note that we will not be worrying about the phase terms
in this lab. Next, well create a synthetic horn note, progressively making it sound more like the original by
adding in more harmonics.
For this part of the lab, you will need to download the soundle horn11short.wav and the M-les
getfreqs.m, cosgen.m and cosgen.g and move them to your working directory. The cosgen tool will allow
you to look at the time and frequency representations side by side (time is on the left, frequency is on the
right). The time window shows only a short duration of the actual signal, so that the changes are more
visible as you add more cosines. The getfreqs command will allow you to measure the frequency and
amplitude of the cosines in a sound (phase is not shown).
Create a synthetic horn note by:
1. Use cosgen to look at a natural horn sound (load horn11short.wav) and play the signal.
2. Use getfreqs to measure the amplitude and frequency of the cosines in that sound.
3. Use cosgen a second time to create a synthetic horn sound by adding up cosines with the amplitudes
and frequencies measured. Save the synthetic sound at each step of adding a cosine, using the save
button in cosgen and specifying dierent names like 1cos, 2cos, etc.
4. Play the natural and synthetic examples to determine how many cosines you need before it starts
sounding like the natural horn.
To play the notes back and forth, read them into MATLAB and play them using the wavread and sound
commands, as follows:
>> [note1, fs]=wavread(1cos);
>> [note2, fs]=wavread(2cos);
>> ...
>> [natural, fsn]=wavread(horn11short);
>> sound(note1,fs)
>> sound(natural,fsn)
>> sound(note2,fs)
>> sound(natural,fsn)
>> ...
The fs and fsn are the sampling frequencies of the sounds. All the synthetic notes will have the same
sampling frequency, but the natural one is dierent. You need to be careful to use the right sampling
frequency in playing the sound, or it will have the wrong pitch.
3 Time-Varying Frequency Content
Many interesting sounds have frequency content that changes as a function of time. You can visualize this
using a spectrogram. MATLAB has a built-in command that gives you a 3-D plot with color for indicating
the amount of each frequency, as in the examples in class:
spectrogram(x,nwindow,noverlap,nt,fs,yaxis)
It has several parameters, some of which control aspects that are beyond what we will go into in this class,
but the basic principles are:
5
x: the signal that you want to create a spectrogram of
nwindow: the number of time samples in each time interval used in frequency analysis (smaller windows
give you better time resolution for seeing things like clicks, longer windows give you better frequency
resolution for seeing harmonics when the fundamental frequency is low)
noverlap: the amount of overlap of each time window used in frequency analysis (0 < noverlap <
nwindow)
nt: number of points in the t (higher numbers give more frequency resolution but take longer, should
be at least as big as nwindow, power of 2 is often used for eciency, e.g. 512 or 1024)
fs: sampling frequency of signal x
yaxis indicates that frequency goes on the y-axis, which gives pictures like what weve seen in class
A reasonable conguration for the bluenose3.wav sound (McNeill book reading snippet) is given in the
example below. Try downloading some sound les from the course source les page and computing
spectrograms. You can read them in using the wavread command, play and generate spectrograms, as in:
>> [blue, fs]=wavread(bluenose3);
>> sound(blue,fs)
>> spectrogram(blue,256,128,256,yaxis)
Get a microphone from the lab and try recording a short sound, ideally something with time-varying
characteristics. Save it with a meaningful lename and move it to your Lab 2 directory. Read the le and
its sampling frequency using the wavread command, and then compute the spectrogram.
Show the spectrogram to your TA and point out specic regions where you can connect
what you hear with what you see, e.g. where the high vs. low frequency sounds are, where
any sudden onsets are, or for speech where the vowels are.
IMPORTANT REMINDER:
Any les saved on the SCC Lab computers WILL be deleted when the computer is rebooted!!! Make sure
that you copy any les that you want to keep to your EE (or other) account. As a courtesy to others, it is
also a good idea to delete your les and working directory when you are done.
6