Use the Fourier Series to Approximate Discontinuous Functions and to Interpret Music

Introduction

OBJECTIVE:  Use Mathematica to calculate Fourier series and to build even and odd Fourier representations of selected function.

In signal processing and communications, it is necessary to construct periodic functions, some with discontinuities. The Fourier series provides us with a tool to analyze such functions. One very important type of signal that you probably receive every day is music. We can use the Fourier series to build mathematical models of musical tones, to look at their graphs, and even to play back the signal to hear how close our model is to the real thing. We usually call a device that can do this a synthesizer.

As you have probably noticed, the computations involved in arriving at a Fourier series approximation to a function can be tedious. Fortunately, the computer can perform such computations for us, enabling you to not only get the results but to visualize the results 

It should be noted that Mathematica has a package that allows you to merely call upon the Fourier Series. The package is called by executing  <<Calculus`FourierTransform` The specific commands for getting the Fourier Series expansions are different in older versions. You can get details on this from the Help window, by looking under Add-Ons and then Calculus packages and then the FourierTransform. This module calls upon the FourierTransform package at times, but it also uses the Integrate commands to compute the Fourier coefficients, so that it is less of a "black box." If you wish to use this package for a discontinuous function, that function must defined in a special way, for example, using the Mod function or the UnitStep function.

Technology Guidelines

NOTE:  If you have just finished a module, restart Mathematica or Quit Kernel under the Evaluation pull-down menu before executing a new module.
TO OPEN CELLS, click on the arrow to the left of the cell or put your cursor on the right cell bracket and double click. To open all cells, highlight the bracket on the far right and select Grouping under the Cell pull-down menu and select Open All Subgroups.
TO STOP AN EXECUTION
Select the Evaluation pull-down menu and click on Abort Evaluation.
ORDER OF EXECUTION
Execute cells in the order given. Do not skip any Input cells within a given notebook.
SAVING NOTEBOOKS
You can save anytime to any directory you choose, and it is wise to save often. However, before you do your final save to reduce the size of your file, it is a good idea to delete all your output by selecting the Delete All Output selection under the Cell pull-down menu.
EXPERIENCING MAJOR PROBLEMS
Save if appropriate, then shut down Mathematica and start it up again.

Part I - Fourier Series Approximations for Non-Periodic Functions

The Fourier Series

The following is an example of how you can use a Fourier series to approximate a continuous function. We define the coefficients for the sine and cosine functions as they are defined in your text. Because of the way that a[n] and b[n] are stored, if you decide to enter a new function, you must be sure to clear the a and b values first.

The function we begim looking at is f(x) = (1 - x)(x+1)(x-2) and we will find its Fourier series approximation over the interval from x = -2 to +2. We will begin by defining the function and looking at its plot.

Clear[a,b,f,x,length,foursine,fourcos,all]
f[x_]:=(1-x) (x+1) (x-2)
length=2;
Plot[f[x],{x,-length,length}]

Now we compute the Fourier coefficients using the formulations in your text.

\[MathematicaIcon] About Mathematica

b[n_]:=b[n]=1/length Integrate[Sin[n  π/2  x]f[x],{x,-length,length}]	
a[n_]:=a[n]=1/length Integrate[Cos[n π/2   x]f[x],{x,-length,length}]

Next, we will put these coefficients into the series formulas and go out to the n = 10 term in both the sine and cosine components.

foursine[x_,10]=Sum[b[j] Sin[j π/2  x],{j,1,10}];
fourcos[x_,10]=a[0]/2+Sum[a[j ]Cos[j π/2  x],{j,1,10}];
all[x_,10]=foursine[x,10]+fourcos[x,10]

I expect that you are glad that you did not have to calculate that by hand! Now let's look at a plot of the function together with its Fourier series approximation.

Plot[{f[x],Evaluate[all[x,10]]},{x,-length,length},PlotStyle->{RGBColor[0,0,1],RGBColor[1,0,0]},AxesLabel->{"x","function"}]
Print["The function is plotted in blue and its Fourier series approximation over the interval from x = -",length," to x = ",length," is plotted in red."]
Print["The Fourier series approximation out to n = 10 is:  ",ComplexExpand[N[all[x,10]]]]

The ComplexExpand command in the last input expression shows the series in real form. 

What happens if you extend the plot out further? You will notice that the Fourier Series repeats the pattern of the function over the interval [-2, 2], whereas the polynomial does its own thing.

Plot[{f[x],Evaluate[all[x,10]]},{x,-4 length,4 length},PlotRange->{-20,20},PlotStyle->{RGBColor[0,0,1],RGBColor[1,0,0]},AxesLabel->{"x","function"}]

The Fourier series gives a reasonable approximation to the function over the interval [-2,2], but outside that interval, all bets are off. The extended Fourier series is called the periodic extension of the function.

Using the Fourier Transform Package

We could have arrived at the Fourier trigonometric expansion for our function by loading a special calculus package.

<<"FourierSeries`"

The default periodic length in this package is 1, so, when we call upon the trig series function, we will need to set our period at something other than 1. To do this, we change the second FourierParameter to 1/period. In this case, the width of the period is (length/2). The "10" specifies how many terms we want.

fastfs[x_]=FourierTrigSeries[f[x],x,10,FourierParameters->{1,1/( length/2)}]

To verify that this agrees with what we had previously found, we will look at the numerical approximation and graph this along with our original function.

Simplify[N[fastfs[x]]]
Plot[{f[x],fastfs[x]},{x,- 2length, 2length},PlotRange->{-40,40},PlotStyle->{RGBColor[0,0,1],RGBColor[1,0,0]},AxesLabel->{"x","function"}]

You Try It - Part I

Find the Fourier series for the function ((x^(2 ))/(4 ))  over the interval from -π to π. The following will get you started.

Clear[a,b,f,x,length,foursine,fourcos,all]
f[x_]:=((x^2)/4)
length=π;
b[n_]:=b[n]=(((∫_(-length)^length)Sin[n x] f[x]\[DifferentialD]x)/length)
a[n_]:=a[n]=(((∫_(-length)^length)Cos[n x] f[x]\[DifferentialD]x)/length)
foursine[x_,10]=(∑_(j=1)^10)b[j] Sin[j x];
fourcos[x_,10]=((a[0])/2)+(∑_(j=1)^10)a[j] Cos[j x];
all[x_,n_]=foursine[x,10]+fourcos[x,10];
Plot[{f[x],Evaluate[all[x,10]]},{x,-length,length},PlotStyle->{RGBColor[0,0,1],RGBColor[1,0,0]}]
Print["The function is plotted in blue and its Fourier series approximation over the interval from x = -",length," to x = ",length," is plotted in red."]
Print["The Fourier series approximation out to n = 10 is:  ",all[x,10]]

Use this result to verify that the series that converges to(( (π^2))/6) is 1 + (1/4) + (1/9) + (1/16) + (1/25) + ... .
What should you let x equal in your f[x] and in your Fourier series to get the desired result?

Can you come up with the sum of any other infinite series by this method?

Part II - Fourier Coefficients for the Sawtooth Function

Consider the Sawtooth Function that is essentially the line y = x over the interval from 0 to 1, and then that segment just gets repeated over and over. We can draw this by defining it using the Mod [x, 1] function. This maps every x onto the interval between 0 and 1. For example, if x is between 0 and 1, the function will be x. If x is between 1 and two, the function will be x-1, etc. Let's look at a graph of this function

Clear[a,b,x,n,f,g]
g[x_]:=Mod[x,1]
Plot[g[x],{x,-3,3},PlotStyle->RGBColor[0,0,1],AxesLabel->{"x","function"}]

In order to compute Fourier coefficients for this function, we need to focus on the function over the interval from -1 to +1. The function is (x+1) when x goes from -1 to 0 and then becomes x when x goes from 0 to +1. Here we will put all our commands together and show the results at the end.

Clear[a,b,x,length,foursine,fourcos,all]
length=1;
b[n_]:=b[n]=((∫_(-length)^0)Sin[((n π x)/length)] (x+1)\[DifferentialD]x+(∫_0^length)Sin[((n π x)/length)] x\[DifferentialD]x)/length
a[n_]:=a[n]=((∫_(-length)^0)Cos[((n π x)/length)] (x+1)\[DifferentialD]x+(∫_0^length)Cos[((n π x)/length)] x\[DifferentialD]x)/length
foursine[x_,20]=(∑_(j=1)^20)b[j] Sin[((j π x)/length)];
fourcos[x_,20]=((a[0])/2)+(∑_(j=1)^20)a[j] Cos[((j π x)/length)];
all[x_,20]=foursine[x,20]+fourcos[x,20];
Plot[{g[x],Evaluate[all[x,20]]},{x,-2 length,2 length},PlotStyle->{RGBColor[0,0,1],RGBColor[1,0,0]},AxesLabel->{x,function}]
Print["The function is plotted in blue and its Fourier series approximation over the interval from x = -",length," to x = ",length," is plotted in red."]
Print["The Fourier series approximation out to n = 20 is:  ",all[x,20]]

Since this is neither an even nor an odd function, we need both the sine and cosine series in order to fit the function. However, you should note that there is only one nonzero term in the Fourier cosine series.

You should notice what is referred to as Gibb's phenomena. That refers to the poor (wiggly) fit at the points of discontinuity. There are far fewer wiggles over the continuous sections. This phenomena would occur no matter how many terms we extended our series.

You Try It - Part II

Suppose you have a step function so that it takes on the value 0 from -3 to 0 and then jumps up to 5 when x is between 0 and 3. First we will define the function and look at its graph.

\[MathematicaIcon] About Mathematica

Clear[g,x]
length=3;
g[x_]:=0/;-length≤x≤0
g[x_]:=5/;0<x<length
Plot[g[x],{x,-length,length},PlotStyle->RGBColor[0,0,1]]

For more efficient computation, we define the series out to twenty terms. If you want it out to more or fewer terms, you can change the term in red.

Clear[a,b,x,length,foursine,fourcos,all]
length=3;
b[n_]:=b[n]=((∫_(-length)^0)Sin[((n π x)/length)] 0\[DifferentialD]x+(∫_0^length)Sin[((n π x)/length)] 5\[DifferentialD]x)/length
a[n_]:=a[n]=((∫_(-length)^0)Cos[((n π x)/length)] 0\[DifferentialD]x+(∫_0^length)Cos[((n π x)/length)] 5\[DifferentialD]x)/length
terms=20;
foursine[x_,terms]=(∑_(j=1)^terms)b[j] Sin[((j π x)/length)];

fourcos[x_,terms]=((a[0])/2)+(∑_(j=1)^terms)a[j] Cos[((j π x)/length)];
all[x_,terms]=foursine[x,terms]+fourcos[x,terms];
Plot[{g[x],Evaluate[all[x,terms]]},{x,-length,length},PlotStyle->{RGBColor[0,0,1],RGBColor[1,0,0]},AxesLabel->{x,function}]
Print["The function is plotted in blue and its Fourier series approximation over the interval from x = -",length," to x = ",length," is plotted in red."]
Print["The Fourier series approximation out to n =",terms," is:  ",all[x,terms]]

Part III - Define a Function to be Either Even or Odd

Sometimes, all we want is a Fourier series approximation for a function over an interval and we don't care about how the periodic extension of the function of the function behaves. Consequently, we have the choice of constructing a Fourier series that is either even (cosine terms only) or odd (sine terms only). There may be a reason why you would choose to have only cosine terms or only sine terms to approximate the function. The following is an example of how you can do either. Suppose that the function for which we want the Fourier series takes on the value x for (0 ≤ x < (1/2)) and the value (1/2) for ((1/2) ≤ x < 1).
We will begin by defining g[x] over the interval from 0 to 1 and then we will extend the definition to an odd function and to an even function.

Clear[g,x,geven,godd]
g[x_]:=x/;0≤x≤0.5`;
g[x_]:=0.5`/;0.5`<x<1;
Plot[g[x],{x,0,1},PlotStyle->RGBColor[0,0,1],AxesLabel->{x,function}]
godd[x_]:=-0.5`/;-1≤x≤-0.5`;
godd[x_]:=x/;-0.5`≤x≤0;
godd[x_]:=x/;0≤x≤0.5`;
godd[x_]:=0.5`/;0.5`≤x≤1;
Plot[godd[x],{x,-1,1},PlotStyle->RGBColor[0,1,0.5`],AxesLabel->{x,odd function}]
geven[x_]:=0.5`/;-1≤x≤-0.5`;
geven[x_]:=-x/;-0.5`≤x≤0;
geven[x_]:=x/;0≤x≤0.5`;
geven[x_]:=0.5`/;0.5`≤x≤1;
Plot[geven[x],{x,-1,1},PlotStyle->RGBColor[1,0,1],AxesLabel->{x,even function}]

Here we will call upon the simplified formulas used when you are looking only for a sine or only for a cosine series. We double the integrals, but integrate over only half the interval and multiply the integral by 2. We will go to n = 20 in both the sine and cosine series for these evaluations.

Clear[a,b,x,foursine,fourcos]
length=1;
b[n_]:=b[n]=(2 ((∫_0^(length/2))Sin[((n π x)/length)] x\[DifferentialD]x+(∫_(length/2)^length)Sin[((n π x)/length)] 0.5`\[DifferentialD]x))/length
a[n_]:=a[n]=(2 ((∫_0^(length/2))Cos[((n π x)/length)] x\[DifferentialD]x+(∫_(length/2)^length)Cos[((n π x)/length)] 0.5`\[DifferentialD]x))/length
terms=20;
foursine[x_,terms]=(∑_(j=1)^20)b[j] Sin[((j π x)/length)];
fourcos[x_,terms]=((a[0])/2)+(∑_(j=1)^20)a[j] Cos[((j π x)/length)];
Plot[{godd[x],Evaluate[foursine[x,terms]]},{x,-length,length},PlotStyle->{RGBColor[0,0.5`,1],RGBColor[1,0,0]},AxesLabel->{x,"odd function"}]
Print["The Fourier sine series is ",N[foursine[x,terms]]]
Plot[{geven[x],Evaluate[fourcos[x,terms]]},{x,-length,length},PlotStyle->{RGBColor[0.8`,0,1],RGBColor[1,0,0]},AxesLabel->{x,"even function"},PlotRange->All]
Print["The Fourier cosine series is ",N[fourcos[x,terms]]]

Note that both the sine and the cosine expansions fit the function over the interval 0 to 1, but, over a broader range, one is even and one is odd. The cosine series seems to be a better fit.

You Try It - Part III

Find both a Fourier sine series and cosine series expansion for the function  |2x - π| over the interval from 0 to π. We could simply define the function as given, but, for clarity in plotting, we will define both its even counterpart and its odd counterpart over the interval from -π to π. You should note that this can be done by shifting the function to the left a distance of π units.

Clear[x,geven,godd]
geven[x_]:=Abs[2 (x+π)-π]/;-π≤x<0;
geven[x_]:=Abs[2 x-π]/;0≤x<π;
Plot[geven[x],{x,-π,π},PlotStyle->RGBColor[1,0,1],AxesLabel->{x,even function}]
godd[x_]:=-Abs[2 (x+π)-π]/;-π≤x<0;
godd[x_]:=Abs[2 x-π]/;0≤x<π;
Plot[godd[x],{x,-π,π},PlotStyle->RGBColor[0,1,0.5`],AxesLabel->{x,odd function}]

Note that this function is -(2x - π) between 0 and π/2, but +(2x - π) from π/2 to π. You can change the number of terms by changing the appropriate expression in red.

Clear[a,b,x,n,foursine,fourcos,terms]
length=π;
b[n_]:=b[n]=(1/length)2 ((∫_0^(length/2))Sin[((n π x)/length)] (-(2 x-π))\[DifferentialD]x+(∫_(length/2)^length)Sin[((n π x)/length)] (2 x-π)\[DifferentialD]x)
a[n_]:=a[n]=(1/length)2 ((∫_0^(length/2))Cos[((n π x)/length)] (-(2 x-π))\[DifferentialD]x+(∫_(length/2)^length)Cos[((n π x)/length)] (2 x-π)\[DifferentialD]x)
terms=20;
foursine[x_,terms]=N[(∑_(j=1)^20)b[j] Sin[((j π x)/length)]];
Plot[{godd[x],Evaluate[foursine[x,terms]]},{x,-length,length},PlotStyle->{RGBColor[0,0.5`,1],RGBColor[1,0,0]},AxesLabel->{x,"odd function"},PlotRange->All]
Print["The Fourier sine series is: ",foursine[x,terms]]
fourcos[x_,terms]=N[((a[0])/2)+(∑_(j=1)^20)a[j] Cos[((j π x)/length)]];
Plot[{geven[x],Evaluate[fourcos[x,terms]]},{x,-length,length},PlotStyle->{RGBColor[0.8`,0,1],RGBColor[1,0,0]},AxesLabel->{x,"even function"},PlotRange->All]
Print["The Fourier cosine series is: ",fourcos[x,terms]]

Is the cosine function a better fit again? Try this out with other functions.

Part IV - Analyze Musical Tones of a Clarinet

The harmonies in mathematics are heard in musical instruments such as the piano and clarinet and are seen in the Fourier Series of the form: a[0]+a[1] Cos[((2 π 1 t)/λ)]+b[1] Sin[((2 π 1 t)/λ)]+a[2] Cos[((2 π 2 t)/λ)]+b[2] Sin[((2 π 2 t)/λ)]+...+a[n] Cos[((2 π n t)/λ)]+b[n] Sin[((2 π n t)/λ)]
A group of students from Carroll College in Montana recorded musical tones from a clarinet and from a piano using a sound sensor that interfaces with a graphing calculator. The recorded signals represent a measure of the loudness of the tones as a function of time. After sampling the tones and graphically observing the periodic pattern, they selected what appeared to be approximately one period of the signal and then used Riemann sums to approximate the integrals for the Fourier coefficients. The following data sets are those that were collected by the students. Lambda (λ) represents the length of the interval selected for what appears to be one period in the recorded signal. Increasing the number of Fourier terms gives better approximations of the signal.

First we look at all the sample points collected when the note A is played on a clarinet. You can hear the recording of the sound itself with the Mathematica ListPlay command, provided your speakers are turned on. It is a very short tone.

clarinet={2.27817,2.40131,2.57483,2.642,2.63081,2.59722,2.53005,2.51886,2.58603,2.53005,2.49087,2.47408,2.37892,2.34534,2.36213,2.33974,2.33414,2.37892,2.4293,2.44609,2.38452,2.36213,2.29496,2.27817,2.27817,2.25578,2.31175,2.54684,2.78753,2.82112,2.83231,2.82112,2.68678,2.6476,2.58043,2.49087,2.43489,2.39012,2.40691,2.4125,2.45169,2.51326,2.57483,2.69238,2.74835,2.69238,2.66999,2.65879,2.62521,2.69238,2.74276,2.69798,2.68118,2.69238,2.81552,2.80992,2.59722,2.54125,2.39571,2.25578,2.25578,2.21659,2.26137,2.36773,2.53565,2.642,2.63081,2.60282,2.54125,2.50766,2.56923,2.55244,2.49647,2.48527,2.40131,2.33414,2.36213,2.33974,2.33414,2.36773,2.4237,2.45169,2.40131,2.35653,2.30615,2.27257,2.28376,2.26137,2.29496,2.47408,2.74835,2.82672,2.81552,2.83791,2.70917,2.6532,2.60282,2.50766,2.44049,2.39012,2.39012,2.4125,2.44049,2.50206,2.56364,2.66439,2.74835,2.70917,2.66439,2.66999,2.63081,2.67559,2.74276,2.70357,2.68118,2.67559,2.77074,2.8547,2.6364,2.55244,2.44609,2.27817,2.25578,2.22219,2.23898,2.34534,2.49647,2.6364,2.6364,2.60842,2.56364,2.50206,2.54684,2.58043,2.50206,2.49087,2.4293,2.33974,2.35093,2.35093,2.32854,2.35653,2.4125,2.45169,2.4125,2.36213,2.32854,2.27257,2.28376,2.26697,2.27257,2.4293,2.69798,2.82112,2.80433,2.83791,2.73716,2.65879,2.61961,2.53005,2.45169,2.39571,2.39012,2.40691,2.4293,2.49087,2.54125,2.642,2.73156,2.72037,2.65879,2.67559,2.6364,2.65879,2.73156,2.71477,2.67559,2.67559,2.73716,2.8603,2.68118,2.56364,2.48527,2.30615,2.26137,2.23339,2.22219,2.31735,2.45169,2.61401,2.642,2.61401,2.57483,2.50206,2.53565,2.58603,2.50766,2.49087,2.44609,2.35093,2.34534,2.35093,2.32854,2.33974,2.39571,2.44049,2.4237,2.36773,2.33414,2.27817,2.27817,2.27257,2.26697,2.37332,2.61961,2.81552,2.80433,2.83231,2.76514,2.66439,2.63081,2.55244,2.46848,2.4125,2.39012,2.40691,2.4181,2.46848,2.53005,2.60842,2.70917,2.73716,2.66439,2.66999,2.6476,2.6476,2.71477,2.72596,2.68118,2.66999,2.70917,2.84911,2.74835,2.56364,2.51326,2.33974,2.25018,2.23898,2.21659,2.29496,2.4125,2.58603,2.642,2.62521,2.58603,2.51326,2.51326,2.58603,2.53005,2.49087,2.46848,2.37332,2.33414,2.35093,2.33974,2.33974,2.38452,2.43489,2.44049,2.37332,2.34534,2.28936,2.27257,2.27257,2.26137,2.33974,2.56364,2.79873,2.79873,2.80992,2.79873,2.68118,2.6476,2.58043,2.48527,2.4181,2.37892,2.39571,2.4125,2.45728,2.51886,2.59162,2.69798,2.74276,2.67559,2.65879,2.6532,2.6364,2.70357,2.73716,2.68678,2.66999,2.68678,2.81552,2.79873,2.58603,2.53565,2.37892,2.25578,2.24458,2.211,2.26697,2.38452,2.54684,2.6476,2.62521,2.59162,2.53565,2.50766,2.56923,2.55244,2.48527,2.47408,2.39571,2.33414,2.35093,2.33974,2.33414,2.36773,2.4237,2.44609,2.39012,2.34534,2.30615,2.26697,2.27817,2.25578,2.30615,2.49647,2.76514,2.81552,2.80433,2.82112,2.69798,2.6476,2.59722,2.49647,2.43489,2.38452,2.39012,2.40691,2.44049,2.50766,2.56923,2.66999,2.74276,2.69238,2.6532,2.66439,2.62521,2.68118,2.73716,2.69238,2.66999,2.67559,2.78753,2.83231,2.60282,2.54684,2.4237,2.26137,2.25578,2.21659,2.24458,2.35653,2.50766,2.6364,2.62521,2.59722,2.54684,2.50206,2.55804,2.56364,2.49087,2.48527,2.4125,2.32854,2.35093,2.33974,2.32854,2.35653,2.4125,2.45169,2.40131,2.35093,2.31175,2.26697,2.27817,2.26697,2.28936,2.44609,2.71477,2.81552,2.79313,2.82672,2.72037,2.6476,2.61401,2.51886,2.45169,2.39571,2.38452,2.40131,2.4237,2.49087,2.55244,2.642,2.73156,2.70917,2.6532,2.66999,2.63081,2.65879,2.73156,2.70917,2.66999,2.66999,2.75395,2.84351,2.642,2.56364,2.46288,2.28376,2.25018,2.22219,2.23339,2.32854,2.47408,2.62521,2.63081,2.61401,2.56364,2.50206,2.53565,2.56923,2.50766,2.49647,2.43489,2.34534,2.35093,2.34534,2.32854,2.34534,2.40131,2.44049,2.4181,2.36213,2.32854,2.27257,2.27817,2.26137,2.27817,2.40131,2.6532,2.81552,2.79313,2.82672,2.74276,2.6532,2.62521,2.53565,2.45728,2.40691,2.37892,2.40131,2.4181,2.47408,2.54125,2.61961,2.72037,2.72596,2.6532,2.66439,2.63081,2.642,2.72037,2.71477,2.67559,2.66999,2.70917,2.8547,2.69798,2.55244,2.49647,2.32295,2.25578,2.22779,2.21659,2.31175,2.4237,2.59722,2.6364,2.61961,2.58043,2.50206,2.51886,2.58043,2.50766,2.48527,2.45169,2.35653,2.33974};
ListPlay[clarinet]

To get a clearer understanding of the signal you are hearing, let's look at a graph of the signal with the points connected. Note the periodic behavior of the musical tone. The units on the vertical axis represent the sound levels in the units recorded by the particular sound sensor.

ListPlot[clarinet,Joined->True]

The students chose the points from 129 through 177 to represent one period. Does that look about right? That means that the interval will be of length  λ = 177 - 129.

Clear[period]
start=129;
stop=177;
λ=stop-start;
period=Table[clarinet[[c]],{c,start,stop}];
clarinetplot=ListPlot[period,PlotRange->{{1,λ+1},{2,3}},Joined->True,AxesLabel->{count,"sound sensor values"},AxesOrigin->{0,2.2`},PlotStyle->RGBColor[0,0,1]]

We want to get a Fourier series approximation for the set of points you see in the graph. In what is to follow, note the use of Riemann sums rather than integrals to approximate the Fourier coefficients. Because you have a set of discrete values (assigned to the symbol period) instead of a mathematical function to approximate, you can estimate the areas of each small rectangle to be the width of the rectangle (1/λ) times the appropriate height (sound sensor values as in the above graph). Adding up these areas gives approximations to the integrals over the interval of length λ.

Clear[a,b,n,t,terms,clarinetfour]
terms=20;
a[0]=(((∑_(t=1)^λ)period[[t]])/λ);a[n_]:=a[n]=((2(∑_(t=1)^λ)period[[t]] N[Cos[((2 π n t)/λ)]])/λ)
b[n_]:=b[n]=((2 (∑_(t=1)^λ)period[[t]] N[Sin[((2 π n t)/λ)]])/λ)
clarinetfour[t_]=N[a[0]+(∑_(n=1)^terms)(a[n] Cos[((2 π n t)/λ)]+b[n] Sin[((2 π n t)/λ)])];

Now we can see what our simulated clarinet plot looks like.

clarinetfourplot=Plot[Evaluate[clarinetfour[t]],{t,1,λ+1},PlotStyle->{RGBColor[1,0,0]},AxesLabel->{count,"sound sensor values"},AxesOrigin->{0,2.2`},DisplayFunction->Identity];
Show[clarinetplot,clarinetfourplot,DisplayFunction->$DisplayFunction]
Print["The original plot is in blue and the Fourier series plot is in red."]

Based upon what we have done, do you think that the Fourier series we found can be used to reproduce the sound of a note? To learn more about this, you might check into the way in which music synthesizers work and the technique used to replicate a phone number dialed on a touch-tone phone. The important point that you should gather from all this is that periodic phenomena, discrete or continuous, can be approximated by Fourier series representation.

You Try It - Part IV - The Piano

As with the clarinet, we first look at and listen to all the sample points collected when the note middle-C is played on a piano.

piano={2.50766`,2.51326`,2.51326`,2.51886`,2.52445`,2.52445`,2.52445`,2.53005`,2.54125`,2.55244`,2.56364`,2.56364`,2.55244`,2.54684`,2.53565`,2.53005`,2.51886`,2.51326`,2.50206`,2.49647`,2.48527`,2.47967`,2.46848`,2.45169`,2.43489`,2.4237`,2.4237`,2.4293`,2.44049`,2.45169`,2.46288`,2.47967`,2.49647`,2.51326`,2.53565`,2.55244`,2.56923`,2.58043`,2.58603`,2.59722`,2.59722`,2.58603`,2.58043`,2.56364`,2.55244`,2.53565`,2.53005`,2.51886`,2.50766`,2.50206`,2.49647`,2.50206`,2.51326`,2.51886`,2.52445`,2.52445`,2.51326`,2.50766`,2.50766`,2.50766`,2.50206`,2.50206`,2.50766`,2.50766`,2.51326`,2.51886`,2.52445`,2.52445`,2.52445`,2.53005`,2.54125`,2.55244`,2.56364`,2.55804`,2.55244`,2.54125`,2.53565`,2.52445`,2.51886`,2.51326`,2.50206`,2.49647`,2.48527`,2.47967`,2.46848`,2.45169`,2.43489`,2.4237`,2.4237`,2.4293`,2.44049`,2.45169`,2.46288`,2.47408`,2.49087`,2.51326`,2.53005`,2.55244`,2.56364`,2.57483`,2.58603`,2.59162`,2.59162`,2.58603`,2.57483`,2.56364`,2.55244`,2.53565`,2.52445`,2.51886`,2.50766`,2.49647`,2.49647`,2.50206`,2.51326`,2.51886`,2.52445`,2.51886`,2.51326`,2.50766`,2.50766`,2.50766`,2.50206`,2.50206`,2.50206`,2.50766`,2.51326`,2.51886`,2.51886`,2.51886`,2.52445`,2.53005`,2.54125`,2.55244`,2.56364`,2.55804`,2.55244`,2.54125`,2.53565`,2.52445`,2.51886`,2.51326`,2.50206`,2.49647`,2.48527`,2.47967`,2.46848`,2.45169`,2.44049`,2.4293`,2.4237`,2.4293`,2.44049`,2.45169`,2.46288`,2.47408`,2.49087`,2.51326`,2.53005`,2.54684`,2.56364`,2.57483`,2.58603`,2.59162`,2.59162`,2.58603`,2.57483`,2.56364`,2.54684`,2.53565`,2.52445`,2.51326`,2.50766`,2.49647`,2.49647`,2.50206`,2.50766`,2.51886`,2.52445`,2.51886`,2.51326`,2.50766`,2.50766`,2.50206`,2.50206`,2.50206`,2.50206`,2.50766`,2.50766`,2.51886`,2.51886`,2.51886`,2.51886`,2.52445`,2.53565`,2.55244`,2.55804`,2.55804`,2.55244`,2.54125`,2.53005`,2.52445`,2.51886`,2.51326`,2.50206`,2.49087`,2.48527`,2.47408`,2.46288`,2.45169`,2.43489`,2.4237`,2.4181`,2.4237`,2.43489`,2.45169`,2.46288`,2.47408`,2.49087`,2.50766`,2.53005`,2.54684`,2.55804`,2.56923`,2.58603`,2.58603`,2.58603`,2.58603`,2.57483`,2.55804`,2.54684`,2.53565`,2.52445`,2.51326`,2.50206`,2.49647`,2.49647`,2.50206`,2.50766`,2.51886`,2.52445`,2.51886`,2.51326`,2.50766`,2.50766`,2.50206`,2.50206`,2.50206`,2.50206`,2.50766`,2.51326`,2.51326`,2.51886`,2.51886`,2.51886`,2.53005`,2.53005`,2.55244`,2.55804`,2.55804`,2.55244`,2.54125`,2.53565`,2.52445`,2.51886`,2.50766`,2.50206`,2.49087`,2.48527`,2.47967`,2.46288`,2.45169`,2.43489`,2.4237`,2.4237`,2.4293`,2.44049`,2.45169`,2.46288`,2.47408`,2.49087`,2.51326`,2.53005`,2.54684`,2.55804`,2.56923`,2.58043`,2.58603`,2.58603`,2.58043`,2.56923`,2.55804`,2.54684`,2.53565`,2.52445`,2.51326`,2.50206`,2.49647`,2.49647`,2.49647`,2.50766`,2.51886`,2.51886`,2.51886`,2.51326`,2.50766`,2.50766`,2.50206`,2.49647`,2.49647`,2.50206`,2.50766`,2.50766`,2.51326`,2.51326`,2.51326`,2.51886`,2.52445`,2.53565`,2.54684`,2.55244`,2.55244`,2.54684`,2.54125`,2.53005`,2.52445`,2.51886`,2.51326`,2.50206`,2.49647`,2.48527`,2.47408`,2.46288`,2.45169`,2.43489`,2.4237`,2.4181`,2.4293`,2.43489`,2.45169`,2.45728`,2.47408`,2.48527`,2.50766`,2.52445`,2.54125`,2.55804`,2.56923`,2.58043`,2.58603`,2.58603`,2.58043`,2.56923`,2.55244`,2.54125`,2.53565`,2.52445`,2.51326`,2.50206`,2.49647`,2.49647`,2.50206`,2.50766`,2.51886`,2.51886`,2.51886`,2.51326`,2.50766`,2.50766`,2.50206`,2.49647`,2.49647`,2.50206`,2.50206`,2.50766`,2.51326`,2.51326`,2.51326`,2.51886`,2.52445`,2.53565`,2.54684`,2.55244`,2.55244`,2.54684`,2.53565`,2.53005`,2.52445`,2.51326`,2.50766`,2.50206`,2.49087`,2.48527`,2.47408`,2.46288`,2.44609`,2.43489`,2.4237`,2.4181`,2.4237`,2.43489`,2.44609`,2.45728`,2.46848`,2.48527`,2.50766`,2.52445`,2.54125`,2.55244`,2.56923`,2.57483`,2.58043`,2.58603`,2.58043`,2.56923`,2.55804`,2.54125`,2.53565`,2.52445`,2.51326`,2.50206`,2.49647`,2.49647`,2.50206`,2.51326`,2.51886`,2.51886`,2.51326`,2.51326`,2.50766`,2.50206`,2.50206`,2.49647`,2.49647`,2.49647`,2.50206`,2.50766`,2.51326`,2.51326`,2.51326`,2.51326`,2.52445`,2.53565`,2.54684`,2.55244`,2.54684`,2.54125`,2.53565`,2.53005`,2.52445`,2.51886`,2.50766`,2.50206`,2.49087`,2.48527`,2.47967`,2.46288`,2.45169`,2.43489`,2.4237`,2.4237`,2.4293`,2.43489`,2.44609`,2.45728`,2.46848`,2.48527`,2.50766`,2.52445`,2.54125`,2.55244`,2.56364`,2.57483`,2.58043`,2.58043`,2.57483`,2.56364`,2.55244`,2.54125`,2.53005`,2.51886`,2.50766`,2.49647`,2.49087`,2.49087`,2.49647`,2.50766`,2.51326`,2.51886`,2.51326`,2.51326`,2.50766`,2.50766`,2.50206`,2.49647`,2.49647`,2.49647`,2.50206`,2.50766`,2.51326`,2.50766`,2.51326`,2.51326`,2.51886`,2.53005`,2.54684`,2.54684`,2.54684`,2.54125`,2.53565`,2.52445`,2.51886`};
ListPlot[piano,Joined->True]
ListPlay[piano]

The students chose the points from 90 through 152 to represent one period. Does this look about right?

You will note that this makes the period slightly longer for the note "C" than for the note "A". Might this have anything to do with the notes themselves?

start=90;
stop=152;
λ=stop-start;
period=Table[piano[[c]],{c,start,stop}];
pianoplot=ListPlot[period,PlotRange->{{0,λ+1},{2.4`,2.6`}},AxesLabel->{count,"sound sensor values"},AxesOrigin->{0,2.4`},Joined->True,PlotStyle->RGBColor[0,1,0]]

Clear[a,b,n,t,terms,pianofour]
terms=30;
a[0]=(((∑_(t=1)^λ)period[[t]])/λ);
a[n_]:=a[n]=((2 (∑_(t=1)^λ)period[[t]] N[Cos[((2 π n t)/λ)]])/λ)
b[n_]:=b[n]=((2 (∑_(t=1)^λ)period[[t]] N[Sin[((2 π n t)/λ)]])/λ)
pianofour[t_]=N[a[0]+(∑_(n=1)^terms)(a[n] Cos[((2 π n t)/λ)]+b[n] Sin[((2 π n t)/λ)])];

pianofourplot=Plot[Evaluate[pianofour[t]],{t,1,λ},PlotStyle->{RGBColor[1,0,0]},AxesLabel->{count,"sound sensor values"},AxesOrigin->{0,2.4`},DisplayFunction->Identity];
Show[pianoplot,pianofourplot,DisplayFunction->$DisplayFunction]
Print["The original plot is in green and the Fourier series plot is in red."]

Compare the clarinet and the piano. The clarinet is in blue and the piano is in green in the following graph.

Show[clarinetplot,pianoplot,PlotRange->All]

Why might the piano tone be smoother than the clarinet tone? Listen to their sounds in succession

ListPlay[clarinet]
ListPlay[piano]

\[MathematicaIcon] About Mathematica

NOTE:  The following double assignment terminology used, b[n_]:= b[n] = .... , forces Mathematica to "remember" the value of b[n] for each value of n, making subsequent computations which repeatedly use b[n] much more efficiently. Because of the way it is stored, if you decide to enter a new function, be sure that you Clear a and b first.
Go back to where you were in the notebook.

There are many ways to define piecewise continuous functions in Mathematica. This is one of them. The /; is used to specify the values of x over which f[x] is to take on the specified value. Other ways to define piecewise continuous functions include using the UnitStep function or the Which function. You can look these up in the Help menu to get more information on their use. You will need to use the UnitStep function if you want to call upon the Mathematica FourierTrigSeries. 
Go back to where you were in the notebook.