Learn MATLAB Episode #15: Fourier Transform

So, in this matlab tutorial we’re going to extend what we learned last time about the Fourier transform. So, last time we took the Fourier transform of a sine wave, and if you’ve ever studied the Fourier transform in school you know that when you take the Fourier transform of a sine wave you should see a delta function or a spike in the frequency domain, and what we were seeing was something a little strange. So, suppose I set my x to linspace(0,5*pi,1000); and then I plot(real(fft(sin(x))) Ok, so, what we see here is actually two spikes not one spike, and this is due to artifacts, due to sampling, we don’t have an infinite sine wave which is assumed when we calculate the fft of the sine wave, and then we get a delta function in the frequency domain. So, what we could do if you want to have closer to an internet sine wave you can do 0 to 10 pi, so that’s five full sine waves, and then do 500 samples. Plot sine of X again, and so now you can see something much nicer. So, now I want to extend the idea of the Fourier transform a little bit. So let’s say we’re doing the Fourier transform of a song or somebody playing the piano, the frequencies if you’ve ever studied music you know are at certain notes, are multiples of each other, and so the frequency is going to change as the song progresses. Now how do we show that using the Fourier transform? The Fourier transform takes the entire signal from time equals minus infinity to infinity, and so what we’re really doing is we’re taking all the different frequencies over all of time and then plotting that on the same Fourier transform. And so the answer to that question is we use something called the short-time fourier transform. And so the way that works is it’s a two dimensional heat map, or you can think of it as a contour plot or something similar, that shows time on one axis and frequency on the other axis. It shows you different frequency components at different times, and the way it works is it takes the Fourier transform of small windows of time and then moves it along the signal as time goes on. So, it’s only showing you the frequency spectrum of a small window at each time. Another name for the short-time fourier transform is the spectrogram. So, if we look here there are different function signatures that you could use. Notice that we require the signal processing tool box if we want to use this method. A spectrogram look something like this, you have time and frequency and it shows you how the frequency, the main frequency component is changing as time goes on. So, how do we create, let’s say we want to test this function out, how do we create a function that changes frequency as time goes on? So let’s say we set X as before, actually have already signed it, so it goes from 0 to 5pi or 0 to 10pi, and it has 5,000 points. So, suppose we set the frequency. So, a wave looks like this, frequency*time. So, suppose we set the frequency Omega, and I’m just typing this out this isn’t real matlab, frequency as a function of time is just equal to the time. So, we’re going to increase the frequency linearly as time goes on. So, if we combine those two we get something like this, y2 = sin(x.*x) because this is element by element multiplication. Ok, so, now I do spectrogram of y2, and you see that the frequency is increasing linearly, so it takes an equal size step at each window, and notice that there are some artifacts due to the fact that the frequency is not equal within the window that we’re calculating the fft. So, you notice that it looked kind of blockage, what you can do is you can pass in the window size along with the data. So, let’s try window size of 100, so now you can see it looks much more like a line. So, let’s try that again with the window size of 500. So, now you see the little red line is a little bit skinnier but the time windows are a little bit bigger, and so this is what we call the uncertainty principle and you may have heard this if you take in quantum mechanics. The uncertainty principle when you measure the momentum and it’s very accurate, you can’t measure the position as accurately. So, it’s the same when you’re taking the Fourier transform because if we want to know the frequency very accurately we need to have a very long window of time, but that gives us different frequencies at a lot of different times. If we want to know the frequency at a very specific time we have to shorten the window, but then we have a frequency that’s not as accurate.