Let’s do some signal processing exercises in MATLAB!
So in this tutorial we’re going to go a little bit deeper, and more general then just sound, and we’re going to talk about signal processing. So, one of the most important concepts in signal processing is the Fourier transform. If you’ve never heard of this you can go to Wolfram.com and look up Fourier transform. So, if you’ve never taken calculus or some sort of advanced university math this probably won’t make a lot of sense to you, but if that were the case you probably wouldn’t be doing this course in the first place. The Fourier transform is essentially in a more of a conceptual way it lets you view the frequency components of a signal. So, you convert a signal from the time domain into the frequency domain and then you can use the reverse Fourier transform to go from the frequency domain back to the time domain. So, now if you look at these equations you notice that f(x) the original signal is a continuous function of X, and so we call this the continuous Fourier transform. Now since we’re working in matlab and we’re working with arrays and matrices that won’t really work for us. So, we need a sort of discrete version of the Fourier transform, and appropriately it’s called the discrete Fourier transform. So, let’s look that up. Ok, so, we’ve sampled f(x) or sometimes we call it f(t) since you know if we’re looking at a sound signal its varying in time. So, we sample f(t) at a certain frequency delta, so that’s the sampling period, and then we assign those values at each sample to f(k), and so f(k) becomes a discrete signal. One thing to note from last time that we talked about sampling and quantization, so this is sampling only not quantization. So, once we have f(k) we can then calculate the discrete Fourier transform f(n). So, this brings us to the next concept is what’s the function in matlab that actually calculates the discrete Fourier transform? So, there is an algorithm called the fast Fourier transform, also known as FFT for short, it calculates the discrete Fourier transform in n log n time. So, if you were to, if you’ve studied algorithms you know about o(n) or Big O notation, so if you were to calculate the Fourier transform naively you would get an o(n^2) algorithm which is slower than and n log n. We don’t need to worry about the details of the algorithm just that matlab has a function called FFT. So, as an example let’s do sine wave. So, let’s calculate the FFT of sine wave…so now let’s try to plot the FFT, or the transform signal. And so you’ll notice it looks kind of weird, this is not what we want to see, so this is just an intermediate step that I’m showing you. So, if we go back to the definition of the Fourier transform, let’s look at the discrete Fourier transform, you’ll see that it’s the original signal f(k), times e to the power of -2, pi, i, nk/N. So, the exponent to the power of i where i squared is -1 is going to give you a complex number, so there’s going to be a real part and an imaginary part. You can use Euler’s equation to see this more easily, cos(theta) + i(sin)(theta), so there’s a real part and an imaginary part. So, if we look back at big Y which is the transform signal, you’ll see that each of the parts here has a real part and an imaginary part. So, typically when we’re plotting the FFT we usually just look at the real part. So, there’s a method called real in matlab, so suppose I have some number 2 plus 3 times i,, real will just drop the imaginary part. So, now I’m going to plot real times big Y which is the FFT of little y. Ok, and you can see that there are two spikes, one on the left and one on the right. So, when you’re looking at the frequency domain the Fourier transform you really only need to pay attention to the left half. So, this area is the lowest frequency, this area in the middle is the highest frequency, and because you look at the Fourier transform again there’s this 2pi in the exponent, and also that it’s a complex number which then resolves to cosines and sines. The Fourier transform’s actually periodic, so it’s periodic in 2pi, and so when we look at the continuous Fourier transform you’ll see that the signal actually repeats every 2pi. So, when you look at a signal, so let’s just look at and see if we can find one, this is a good picture what I’m trying to explain, so usually the way that the Fourier transform is visualized is that it’s symmetric around the center. So, we only show from -pi to pi in the frequency domain, and then anything you see on the left side of the zero-point here it’s just carried over to the far right when you’re looking at the discrete Fourier transform. Another thing to notice is that the transform signal is the same size as the original, so size big Y is the same as size little y. So, the important thing I want you to grab from this is that the sine wave has only one frequency, right, and so that’s what we’re seeing here, that’s what this spike is. Now so when we look at real signals,, so we’re going to look at some voices soon, what we’re going to see is there are going to be multiple frequency components, alright. So,, when you speak or when you play an instrument they produce different sounds because they have different frequencies playing at the same time. So, we’re going to go back to the hello world from the first and second lecture. So, audioread helloworld.mp3. So, remember that d has two channels, so we’re just going to look at the left or first channel in the FFT lectures. So, I’ll say big D is equal to FFT of little d, so I’m going to select all the rows and then the first column. Now I’m going to plot the real part of the FFT(D). Alright, so, you can see the multiple frequency components that show up this is my voice. So I’ve downloaded a female version of hello, I couldn’t find hello world so she just says hello, going to read that in. So, I’m going to play it so you know what it sounds like…”hello.” Okay, so, the female voice is more high pitched than the male voice, so mine is a male voice. So, what we should see then I’m going to take the FFT of the female voice, I’m going to plot that, so this doesn’t tell us much because it’s just a plot by itself. So, we’re going to use some of the things we learned before, okay, I’m going to use subplot. I could try to plot these on the same plot but we have a problem, right, so we look at the size of D and it’s about 85k, and if we look at the size of F it only 8,000, so it’s a much shorter “hello.” So, the problem with that is we plot them both on the same axis one is going to be really short and the other one is going to be really long so you can’t really compare the frequencies that well, so what we’re going to do is we’re going to use subplot. I’m going to plot the female version up on top, and I’m going to plot the male version which is me at the bottom. Ok, so, remember that the signal is symmetric, right, so I only have to pay attention to one side, and they’re relatively the same length on these plots. So, now notice that the female voice takes up higher frequencies and my male voice takes up lower frequencies and its loudest on a very low frequency. So, the Fourier transform can be used to analyze sound signals or any kind of signal that you want to know the frequencies of.