Saturday, October 27, 2012

Making a Square out of Circles

Let's say you're a marine biologist, and you've recorded a bunch of whales and dolphins, and you want to study the sounds that they're making.  Some of the cetacean species you've recorded will be making sounds too high for you to hear (like porpoises and beaked whales), and some will be much too low (like blue whales).  In addition, some biologists (like the ones who are older or have attended too many KISS concerts, might not be able to hear very well at all.

One of the ways biologists get over this is by turning the sounds into pictures, like this picture of a dolphin whistle:

This is called a spectrogram.

Here, we can see that the whistle starts out high, and then drops down in frequency, and goes back up again. Frequency is on the y axis.

Remember, loud sounds have high amplitude (shown as a darker red in the spectrogram above), and high frequency sounds have a high frequency (which will put them high on the y axis of the spectrogram).


This transformation between sound and picture is done via something called the Fourier equation.  The Fourier equation is really cool - it takes a complex sound, made out of multiple different frequencies, and breaks it up into the component frequencies.  It's kind of hard to picture this happening, so I decided to start with a simple single-frequency sound, and make it into a complex frequency sound, so we could see what was happening in the reverse.

So what complex sound can I make from simple sine waves?  What about a square wave?

Figure from Khin Hooi.

Click here to listen to a square wave.
Single frequency sounds are composed of pure sine or cosine waves, which are rounded like this:



However, we can create a square wave by adding additional frequencies, each one of which is an odd harmonic frequency of the original sine wave, and which is smaller in amplitude by a specific amount. For example, in the first step to making a square wave, we would add sin(3*t)/3 to the original equation for the sine wave, y=sin(t).  Then we would add sin(5*t)/5, sin(7*t)/7, sin(9*t)/9, and so on for the odd numbers until the wave is square enough to make us happy. Here's an animation of the frequencies being added up to make a square wave:


Cool, so now we've made a sound out of a bunch of different frequencies.  The next thing to do is to use our Fourier transform on it, to take it back apart again.

The figures on the left are of the square wave I've created, and on the right are of the Fourier spectrogram of that wave.  In the first row of figures, you can see that there is only one frequency, and the spectrogram only shows one line (the light blue one at the bottom). As I add more frequencies, more and more (lighter and lighter) lines appear on the spectrogram.
I thought this was a pretty good demonstration of how Fourier Transform works. We've made a complex signal, and then Fourier helped us take it back apart again so we could visualize the different sine waves that make up the signal again.

If you can't get enough of this stuff, here's an excellent post I found about building a sawtooth wave.

Also, here's a great little youtube animation of the making of a square wave:


If you have matlab, and want to try this out, email me for the code.

No comments:

Post a Comment

What do you think?