In this notebook, you will do some Fourier synthesis and listen to the results.  You will also do some Fourier analysis on a sound you record.  You will then do some filtering on the Fourier spectrum, reconsitute the sound and listen to the changes.

Let's start by defining a simple sine function of time.  Activate the following cell.

f=200;
function1[t_]=Sin[2*π*f*t]

What's the period of the function?


Plot the function over a few periods.  (You will have to fill in or define a value for tmax.)

Plot[function1[t],{t,0,tmax}]

and listen to the sound of the function.

Play[function1[t],{t,0,1}]

Create another function (call it function2) that is a cosine function with the same period.  Plot and play the this function.

Can you tell the difference between the sound of the two functions?  Explain why your answer makes sense.



Now, let's create an approximate square wave.  You've learned already that to do this you want to add together sines of odd harmonics with coefficients (amplitudes) that are proportional to 1/n, where n is the number of the harmonic.  Create a function (function3) that has harmonics up to n=9 with the appropriate coefficients.  Plot the function to make sure it looks approximately like a square wave.  Then play it and compare it to the sound of function1 or function2.

Let's make a 4th function (function4) that's the same as function3 but with all of the sines replaced by cosines.  Before you do that, make a guess at any differences you expect between function3 and function4 in terms of how they look when plotted and how they sound:



Now create function4, plot it and play it.

Did you get what you expected?  Do the two functions look alike?  To what extent do they sound alike?  What does this tell you about how our auditory system processes sound?




Let's begin to do some Fourier analysis.  Mathematica has some powerful tools for this.  In order to make use of them, we need to make a discrete set of data.  The following cell makes a table of data for function3 over two periods with 1000 points per period.  The ListPlot command makes a plot of the data in the table.  Activate the cell:

data3=Table[function3[t],{t,0,2/f,.001/f}];
ListPlot[data3]

We're ready to begin Fourier analysis on function3.  Mathematica's FourierDST command finds all of the Fourier coefficients for the sine functions in a Fourier expansion of a function.  (DST stands for Digital Sine Transform.)  Let's apply this to your set of data from function3:

fourierSdata=FourierDST[data3];
ListPlot[fourierSdata,PlotRange->{{0,100},{-50,50}}]

Note the series of peaks in the data.  Are the peaks where you expect?  Explain.  (Note that frequency -- the horizontal axis -- is in rather strange units that depends on how many points we chose for our data table.  What matters is the relative frequecies, i.e. the ratio of one frequency to another.)





Now try finding the cosine coefficients for function3.  All we need to do is change S to C in the Fourier analysis command.  What would you expect the results to be?  Write your answer here before you activate the next cell.

fourierCdata=FourierDCT[data3];
ListPlot[fourierCdata,PlotRange->{{0,100},{-50,50}}]

Did you get what you expected?  When working with discrete data tables, sometimes the distinction between sines and cosines gets blurred.  That's something to keep in mind when doing Fourier analysis, as we will see in more detail below.

Copy the above cells, making appropriate name changes, to do Fourier analysis on function4.  Make some predictions about what you expect to get here before activating the cells:

Did you get what you expected?  Explain.




Now we can start doing some Fourier analysis on a sound.  Mathematica has the capability to record sound.  Get a microphone and plug it into the appropriate input on the computer.  Now activate the following cell.

sound=SystemDialogInput["RecordSound"]

You should get a dialog box.  For "input", use the default setting.  For "format", choose "11.025 kHz, mono, 16-bit".  Record a few seconds of a sound by pressing the red record button and play a note on an instrument or sing a vowel sound.  When you are done, press the black stop button and then "OK".  Your sound should now be recorded and a spectrogram/play box should appear.  Verify the recording by pressing the play button.  Make sure you have headphones/earbuds connected to the computer in order to hear the sound.  Note the number 11025 Hz that appears in the lower right of the box.  This is the sampling rate.  Your computer measured the microphone's voltage at this rate.  We will use this number a bit later.

Let's do some Fourier analysis on your sound.  Activate the next cell to find the Fourier coefficients of the sines.

fourierSsound=FourierDST[sound[[1,1,1]]];
ListPlot[Abs[fourierSsound], PlotRange->All]

(Note we are taking the absolute value - with the Abs function - of the Fourier coefficients to make the visualization easier.)

How many harmonics are you able to clearly identify relating to your sound?

Let's do something similar to find the Fourier coefficients of the cosines:

fourierCsound=FourierDCT[sound[[1,1,1]]];
ListPlot[Abs[fourierCsound], PlotRange->All]

Do you see much difference between the sine and cosine coefficients?

Now that we've done the Fourier "transform" of the sound, let's see what happens when we do it again, i.e. we apply FourierDST to Fourier data itself:

ffSsound=FourierDST[fourierSsound];
ListPlay[ffSsound, SampleRate->11025]

Play the result you got and compare the result to the sound of your original "sound".  What can you say about the effect of the second application of the Fourier transform?

Note that we used a sampling rate of 11025 Hz here to tell Mathematica how to play back the data.  Try changing it by a factor of two or so and listen to the result.  Comment on what you find and why it makes sense.  

Now it's time to do some crude filtering on your sound.  To do so, I've created for you a special "mask" function.  It's a function of two "frequencies", f1 and f2.  Between these frequencies, the mask is equal to 1; everywhere else it's equal to zero.  When you multiply a Fourier spectrum by a mask, Fourier coefficients between the two frequencies "survive" while all other coefficients are "killed".  Activate the mask function below.

mask[f1_,f2_]:=Table[If[f1<i<f2,1,0],{i,1,Length[fourierSsound]}]

Look at your plot of fourierSsound above.  Identify a frequency somewhere in the middle of all the harmonics (such that there are a few peaks above that frequency).  Now let's do what's called "low-pass" filtering.  Set f1=0 and f2 equal to the frequency you picked.  Fill in your value for f2 and activate this cell.

f1=0;
f2=;
choppedfSsound=mask[f1,f2]*fourierSsound;
ListPlot[Abs[choppedfSsound], PlotRange->All]

If you did this correctly, you should see that this plot resembles the one for fourierSsound, except that all of the peaks to the right of your value of f2 have disappeared.  Let's apply the Fourier transform to this filtered data and listen to the result:

filteredsound=FourierDST[choppedfSsound];
ListPlay[filteredsound,SampleRate->11025]

How does the sound compare to the original?  In a few sentences, explain what you have just done - What does each step of the process accomplish?  How does the process produce filtering?





Reduce f2 to the point that only the fundamental frequency survives and repeat the above steps.  This may require some trial and error.  How does the result sound now?    (Don't change what you did above.  Instead copy what you need into cell below.  Create more cells if you need to.)

We can also do "high-pass filtering"  by setting f2 to be very high and f1 to be the "kill frequency" - all frequencies below it get killed.  In the cells below, filter out the fundamental from your sound and comment on how the result sounds.

Finally, you can do "bandpass" filtering, where you set f1 and f2 to remove both some low and some high frequencies.  Do this to create some interesting sound that is distinct from any of the others you have created.  Comment on the result.  Would you be able to recognize the sound as being related to the original?  What does this sound have in common with the original?  What's noticeably different?