Sunteți pe pagina 1din 8

Experiment 16

Fourier Analysis of Musical Notes


Apparatus:
(i) Matlab software, (ii) Recorded sound les of various musical notes
Purpose of experiment:
To study the structure of dierent musical notes played on various instruments using
Fourier analysis of the recorded signal. After carrying out this experiment you are sup-
posed to gain insight into: (1) Mathematical technique called the Fourier series repre-
sentation (2) How the pitch and timbre of the musical instruments come about and (3)
Working familiarity with the scientic programming language, MATLAB.
Basic Methodology:
The data in the sound le imported to MATLAB is Fourier analyzed using a MATLAB
program. Same notes played on dierent instruments and dierent notes played on the
same instrument are compared to see how the qualities of pitch and timbre come about.
I Theory
At the heart of Fourier series analysis is the Fouriers Theorem which states that any
reasonably well behaved periodic function can be represented as a sum of cosine and sine
functions. Consider a function f(t) with a period T. That is, f(t + T) = f(t). The
following then is the Fourier series representation of that function,
f(t) =
a
0
2
+

n=1
a
n
cos(
2n
T
t) +

n=1
b
n
sin(
2n
T
t) (1)
The coecients in the expansion are given by
a
n
=
2
T

T
0
f(t) cos(
2n
T
t)dt (2)
b
n
=
2
T

T
0
f(t) sin(
2n
T
t)dt (3)
where the integration is over one time period of the function. The Fourier representation
of a function lets one look at the function in terms of the various frequencies present in the
function. Refer any standard book on Engineering Mathematics or Mathematical Physics
to get a more detailed picture of Fouriers Theorem and the conditions on functions that
can be expressed as a Fourier series. Some of the references are given at the end of the
article.
2 PHYSICS LABORATORY MANUAL
Since we are going to do the Fourier analysis of digitized data corresponding to
recorded sound, let us briey look at the phenomenon of sound. Sound wave is a distur-
bance of the medium initiated by a vibrating source. In a gaseous medium like air, it can
be thought of as a propagation of either the pressure or density variation induced by the
vibrating source. When these pressure variations reaches the ear, it sets the membrane
of the ear drum to motion which is then converted to a nerve signal. This signal when
processed by the brain is perceived as sound by us. One can broadly categorize the sound
we listen to as noise and music.
Figure 1: The plot on top shows the variation of amplitude with time of a sound generated
by hitting on the desk. The bottom is the variation of amplitude corresponding to a sitar
note.
Let the pressure variations in the medium be represented by P(x, t). We are consid-
ering a one dimensional space. Generalizations to three dimensions is straight forward.
The fact that sound is a wave means that the functional dependence of x and t in P
should be of the from P(x vt). In general the pressure variation corresponding to a
sound, P(x vt) may not have any particular feature. That is, if one looks at the graph
of P() Vs , no specic pattern will be observed. Such a sound would be categorized
as noise. The sound wave corresponding to a musical note, on the other hand, will show
a repetitive pattern. In Fig. 1 the variation of amplitudes (original pressure variations
converted to a voltage signal by a microphone and then digitized) with time for both
noise (thumping on a desk) and music (a sitar note) is shown. The repeating pattern
in a musical note implies that it is a combination of pure sinusoidal vibrations of some
fundamental frequency and its harmonics. This follows from the Fouriers Theorem we
discussed above. The relative magnitude of the fundamental and the various harmonics
is what renders the tonal quality (timbre) to the particular note played. Thus for a given
note played on violin and a sitar, the dierence in quality of the sound arises from the
fact that they have dierent relative weights for the harmonics present.
2
Experiment 1. Fourier Analysis 3
A few more remarks on sounds. A musical piece is a regular arrangement of notes or
a combination of them (harmony), that is esthetically pleasing. There are other elements
as well in music, such as rhythm and modulation. Speech on the other hand is achieved
by amplitude modulation using a limited band of frequencies centered about some value.
These frequencies may not have any simple relation between them. Another important
point to note is that even though in most of the cases a musical note is combination of the
fundamental and its harmonics, there are instances when the brain perceives a fundamen-
tal frequency even when the spectrum does not contain that frequency or it is very weak.
The right combination of harmonics can fool the brain into perceiving the fundamental
even in its absence! This phenomenon is known as the missing fundamental. Lower notes
played on violin has this property.
II Procedure
In the current experiment you are given a set of audio les corresponding to pure notes
played on various instruments. You have to analyze the spectrum, that is nd the funda-
mental frequency and the strength of the fundamental and the harmonics in each of the
notes. A very basic MATLAB program for nding the Fourier coecients will be pro-
vided (Though there are far better and ecient ways of nding the Fourier coecients,
we have implemented a straight forward numerical integration routine to nd the Fourier
coecients, making use of the integrals given above for nding the coecients). Below we
list sequentially how you can carry out the analysis. MATLAB commands and variable
names have been given in bold letters.
1. Download the MATLAB program (phy1wav.m) and the data les (data1.wav, data2.wav
and data3.wav) from the Fourier Analysis topic in Physics Laboratory 1 page on pho-
ton. Save these les in a directory named Sec-xyGrp-pq on the Desktop, where
xy is your section number and pq your group number. Note that the MATLAB
program le (phy1wav.m) has some junk characters at the beginning and end of the
le when downloaded and saved in the computer. Open the le and delete these
characters.
2. Opening MATLAB: Double click on the MATLAB icon on the desktop
3. Change the working directory using the command cd followed by the path to the
directory Sec-xyGrp-pq where the data and the program le resides.
4. Importing the sound data les: Type [note1, rate1] = wavread(data1); at
the MATLAB command prompt. This will import the data contained in the le
data1.wav into MATLAB and gets stored in the variable named note1. The variable
rate1 will contain the sample rate (that is number of data points acquired in a
second) at which the sound was recorded. The semicolon at the end of the command
prevents the variables being printed on the screen.
3
4 PHYSICS LABORATORY MANUAL
(a) Type whos at the command prompt to see the current variables in the MAT-
LAB work space and their size. Note the size of the variables note1 and rate1.
note1 is a large array which contains the voltage readings corresponding to
the sound recorded. rate1 is a single number.
(b) Type rate1 to read o the sample rate value.
(c) Type note1 to see the data corresponding to the digitized signal corresponding
to the original sound wave picked up by the microphone. This is a long array.
The length of the array depending on how long the sound was recorded and
at what sample rate. You can use the command more on to pause the data
from scrolling across the screen. Tap space bar to move on to the next part of
the data.
5. Playing the sound wave data le: You can play the sound by typing sound(note1,
rate1); at the command prompt. If you increase the rate value you will generate a
sound at higher frequency. Try out the command sound(note1,2* rate1);. You
should hear the sound now at exactly twice the earlier frequency.
6. Plotting the data and extracting the relevant quantities: Type plot(note1) to see
the plot of the data. In the plot the vertical axes corresponds to the signal strength
and the horizontal axes is the index in the array (which is like time up to a scale
factor). See Fig. 2 to see how the plot would look.
Figure 2: The out put when the data is plotted would look like this. The horizontal axes
is the index of the array that contains the data and the vertical axes is the amplitude.
The horizontal axes scaled appropriately (by dividing by the rate value) will give the time.
(a) Use the zoom switch on the gure window to zoom in the see the pattern in
the note. Fig. 3 shows the plot at various scales.
4
Experiment 1. Fourier Analysis 5
Figure 3: The original plot zoomed in at various scales. The bottom right shows the
waveform corresponding to a single period.
(b) Finding the time period of the repeating pattern: First nd out, after how
many data points does the pattern repeat itself. This can be done by zooming
into consecutive identical points on the wave pattern and nding out at what
values of indices they occur. If required, use the Data Cursor option in the
Tools menu on the top of the gure window to keep tab on the zoom location.
If the index values of the two consecutive identical points are Ni and Nf, the
number of data points in one time period of oscillation is given by Nf - Ni. Since
the sample rate is known, this should enable you to determine the time period
of the repeating pattern (time period = (Nf - Ni)/rate1). The fundamental
frequency will be given by the inverse of this time period.
(c) Extracting the signal corresponding to one period: Suppose one of the funda-
mental repeating pattern in the data variable, note1, lies between the array po-
sitions Ni and Nf (say 34980 and 35171). Type note1s = note1(34980:35171);
to extract the data corresponding to a single period and store it in the variable
named note1s. You are now all set to do the spectral analysis. Plot the array
note1s and make sure you have one single period in the variable note1s.
7. Using the MATLAB program phy1wav to nd the Fourier coecients: phy1wav
is a MATLAB function that has been created to analyses the Fourier coecients of
a periodic function. A function is nothing but a collection of MATLAB commands
which has been put in a le. The le containing the function has been saved by
you (thephy1wav.m you downloaded from the photon). Call the function from the
command prompt by typing [a1 b1] = phy1wav(note1s,rate1,Nf-Ni,M,m);.
The function takes in as input arguments note1s, rate1, the number of data points
in a single period (Nf - Ni) and two integers M and m. The values of M and m (say
M = 50 and m = 5) specify the number of Fourier coecients to be computed and
5
6 PHYSICS LABORATORY MANUAL
the number of coecients used to reconstruct the signal respectively. The program
returns two arrays a1 and b1 containing the Fourier coecients, one corresponding
to the cosine terms and the other to the sine terms in the expansion . The program
returns a gure(see Fig. 4), which contains the original data for one time period
(red curve) and the reconstructed data using the rst m Fourier coecients (black
curve). For example if m is 5, then the black curve in the plot corresponds to the
sum
a
0
2
+
5

n=1
a
n
cos(
2n
T
t) +
5

n=1
b
n
sin(
2n
T
t)
The program also plays an audio corresponding to the sound wave having only
the rst m Fourier components. Note how, for low values of m, the pitch sounds
the same though the timbre is dierent. (Go through the program thoroughly and
understand the logic of the program, which has been commented to help you make
sense of the code. Try also to pick up the MATLAB commands and programming
structure while you are at it.)
Figure 4: The original data over a single time period (red curve) and the reconstructed
data using nite number of terms in the Fourier expansion. Clockwise from top left gives
the reconstructed signal by using m = 1, 3, 10 and 30.
8. Run the above program many times over for various values of m = 2, 5, 8, 10, 20, 50
and see how the pattern in the plot and the sound generated gets more and more
close to the original data and sound. Save the gure (as eps) for m = 2, by using
the Save As option in the File drop down menu in the gure. Name the gure
note1meq2.eps
9. Plotting the Fourier coecients: The amplitude corresponding to the fundamen-
tal can be obtained by nding sqrt(a1(2)2 + b1(2)2); The amplitude corre-
sponding to the (n 2) th harmonic can be obtained by nding sqrt(a1(n)2 +
6
Experiment 1. Fourier Analysis 7
b1(n)2); . The amplitudes for the fundamental and harmonics can be obtained as
an array using the command amps1 = sqrt(a1.2 + b1. 2);. In this command
a1 and b1 are arrays and the operation a1.2, with a dot in front the , returns
an array the same size as a1 where each element in a1 is squared. The plot would
look like the one shown in Fig. 5.
Figure 5: The value of amplitudes for the fundamental and the rst 49 harmonics. The
second bar gives the amplitude of the fundamental (the rst one corresponds to the term
involving a
0
).
10. A bar plot of amplitude for various frequencies involved can be obtained by using the
command bar(amps1). This will plot a bar diagram which will give the amplitude
for n = 0, 1 (fundamental), 2 (1st harmonic), 3(2nd harmonic) etc. Save this gure
as mentioned above. Name it note1FC.eps
11. Repeat the above procedure for data2.wav and data3.wav les. As mentioned above
nd the fundamental frequency in each case and store them in variable f2 and f3.
For the data corresponding to note2.wav, save the gure obtained while running
the program (keeping the fundamental and rst 8 harmonics, that is m = 9) by the
name note2meq9.eps. Similarly for data corresponding to note3.wav, save the plot
obtained(for the case of m = 30) naming it note3meq30.eps. For each case, save the
gure of the spectrum as note2FC.eps and note3FC.eps respectively.
12. Fill up the EXCEL work sheet based on your analysis. You have to import the
gures at the appropriate locations on the EXCEL le. To insert the eps gures
into the EXCEL le, use the insert picture option in the EXCEL sheet.
13. Upload the EXCEL le to the Photon server.
7
8 PHYSICS LABORATORY MANUAL
14. If you have time left in the lab, explore more. The audacity recorder is made
available in the computer. You can use it to record your speech and do a spectral
analysis of it.
15. If you are musically inclined, you can try checking out how well you can reproduce
the notes. Sing notes on a scale you are familiar with and check out how accurate
you are.
Questions you should address
1. What are the similarities and dierences between any pair of notes you have ana-
lyzed?
2. Determine the ratio of amplitude of the harmonics to the fundamental for two notes
of the same instrument. Is there a pattern here?
3. Can you see the missing fundamental phenomenon in action in the spectrum of
violin note (data3.wav)?
4. Could you make out that data1.wav and data2.wav both has notes from the same
instrument, harmonium? Play note1 at twice the rate and compare it with the
sound generated by note2.
5. Where does the peak in your speech spectrum lie?
6. What is the typical band of frequencies that you are using for your speech?
References
1. Waves and Oscillations by A. P. French
2. Advanced Engineering Mathematics by Ervin Kryzig
3. Wikipedia article on Missing Fundamental.
4. MATLAB reference: Use the MATLAB demo available under the Help icon in the
main window to learn more about MATLAB.
8

S-ar putea să vă placă și