Transcript:

Hello, everyone, Today’s video. I have two goals. The first goal is to demonstrate how to compute a fast fourier transform using the numpy fft function in python. So I kind of assume that you know what is a fast fourier transform so today I’m focusing on how to calculate in Python. Using Numpy module. Second goal is to plot the frequency spectrum of the output of the FFT using Maplip. So I hope you find this video helpful, so we do it in three steps so first we will construct a time domain signal, so this is the the input signal we are going to compute the fourier transform, so normally it can be a sound signal or any kind of time. Dom, A sensor signal, any kind of time domain signal, right so second step, we will perform fft, right so Lastly, we will plot the both the time domain signal and the frequency spectrum first thing we do we’re trying to construct a time domain signal. Let’s say we have a sampling system, so we have sampling frequency of 2000 kilohertz. So this is the sampling frequency, so we’re going to build a time domain signal. So so this t step this T step? Uh, is the sample time interval? We need this input. Signal frequency. Let’s say for the moment. We assume that I have a sine wave signal an ideal sine wave signal with a 100 hertz frequency. So this is my, uh, signal frequency. Okay, So then we will so this. This is the number of samples of the signals. Okay, so then we need to construct a list for times timestamps so we use the numpy linear space function to create, so it will be from time 0 to T step. Okay, so we have N data points. Oh, I’ve forgotten to import the input module. So first thing you do Is you need to import numpy as np later, you also need a map lib to plot so so this is the time steps are timestamps of the time domain signal, so we compute fft, there is a frequency interval for each frequency bin. So if you know the theory, it’s basically, uh, the trampling frequency divided by the number of samples, right, so if you don’t know why I recommend you to do online research, so this is a frequency interval. Similarly, we need to because we need to plot the, uh, frequency spectrum, so we need to create a x-axis for the frequency spectrum, which is a list of frequencies so again we use the numpy linear space function to create this so we can do similar things like the timestamps except that we change this to the frequency intervals. Okay, this is the frequency steps. Okay, so, Lastly, we just need to, uh, create a signal, so we’re going to create a sine wave signal, So I’ll call it. Y is our signal input time domain signal. So I have an amplitude of one a sine function, so I need a frequency of frequency of 100 Hertz right so I will just compute that 2 times Pi 2 times Pi Times the frequency and the time steps, so this will be our signal so basically, what I’ve done is. I created a time series with the interval of T steps, and then I created a sine wave using the numpy sine wave function. So let me plot that for you to quickly show so here. I plot one cycle of the time domain signal. So let’s do this plot’s, Okay, For the moment, we just have a one row and one column for the plot. Okay, so we simply plot the time versus the signal. Okay, you convert this into an integer. Okay, so let’s run. I forgot to show show the plot here so now you can see, uh, I’ve created a time Domain signal, so X axis is the time Y axis is a signal signal, so probably clear. So now you can see those are the discrete time data points here. Those dots, okay. Wait a moment, okay, so next we will perform the FFT. Uh, using the numpy fft function, so it’s actually quite easy, so it’s simply using a numpy fft fft. So you just simply pass in the input time domain signal here. So the details about this function you can refer to this website for the Numpy Fft function, lets. Go back so this will give you so X will be a series of, uh, complex numbers, so this isn’t the definition of the FFT, so you get a complex number as the output so for now we’re only interested in the magnitude of the of the FFT output. So you just do an empire, you just take the absolute value of that. Also, you need to normalize the the magnitude, right, you need to normalize magnitude by the number of samples here, so in this case, it’s basically N Okay, so now technically we can plot. We can plot the frequency domain also so here. I just need to add I. We need to add, uh, more one one more plot, So let’s say, let’s do it this way, so I’m gonna have two rows so, okay, so here we plot the frequency. The, uh, fft output, which is FFT magnitude versus frequency. So now you can plot, so you see now, you basically what you’re seeing. Is the frequency domain in the in the second plot here, so the X-axi’s is the three is the frequency and Y-axis is the magnitude, but it’s not that correct yet. So in fact, you can see that if you know about the sampling theorem, so so if you have a sampling frequency of 1000 2000 Hertz, so you’re able to reconstruct your signal below half of this frequency, which is one 1000 Hertz, it’s called the Nikoi Nyquist sampling theorem, so what we’ll do here we will do more small modifications, so we are only going to plot half of the frequency of the sampling frequency, so we will create another x-axis for the frequency, so it’s going to be from zero to half half of that. Okay into plus, so I already know that you need to convert it into integer because there is a python list index and you need to plot the okay with X magnitude for the plotting, so it’s basically two times the original one. So, Max, Max, so one point one node need is a DC. Component, we don’t need to multiply by two, so we just need to divide divided by two, so I am not going to explain why, because I’m not really good at that so but just to say you need to do this. Otherwise you end up. Having a wrong DC. Magnitude as you will see later, DC. Component 2 Okay, so now we can plot the proper plot. Proper frequency domain signal. So now you can see. I’m only plotting up to 0 to 100 1000 Hertz and my signal frequency is actually at 100 Hertz, so the magnitude is 1 which is exactly what you see here, so my signal frequency is 100 Hertz and the magnitude is one we get more. Uh, fine resolution in the frequency domain, you just need to increase the number of samples. Let’s say I have 10 cycles of samples. Now you can see this is my time domain signal, and this is the frequency spectrum. So you can see at 100 hertz here. I have a magnitude. Let’s say I have, uh. My signal has two frequencies. For example. I have a another frequency, which is. Let’s say three times. The three times, the input frequency original infrequency. Let’s say 300 Hertz. So I multiply 3 here, so it’s probably easier to understand if I put it here and I have magnitude of a of a 4. For example, so if I run this, so you can see that this is my time domain signal. It has two frequencies and it is clearly shown that on the frequency domain at 100 Hertz, you have a component of a magnitude of 1 and at 300 Hertz here you have a magnitude over 4. So again we can add a DC. Component, Let’s say I have a DC. Signal over 2 so I run it again. Now you can see the DC, which is a zero frequency here has a magnitude of two remember just now I have divided two back in in the previous calculation here because in DC. You don’t need to multiply by two here, so I divide the two back here. So Lastly, let’s try to make the plot, uh, more beautiful, so well first, we will put some labels on the axis so set label X label, so the first is will be the time. So the unit is the second and the label for the second for the frequency spectrum will be a frequency so it will be in Hertz And, okay, you can turn on the grids also, all right. See you again, so now you can see this. Uh, you see you see this? Uh, X label here and I turn on the grid. Okay, so let’s try to set the limit of the axis, also just to make it even better, so let’s say just to limit that try again. Oh, what’s happening? Okay, So this should be X two here. Yeah, now you can see. I’ve limit the the X X axis, so it looks a bit better, so I’m going to remove this second frequency here, so I can have a single frequency, all right, so you have a single frequency. Look, look at that, Okay, You can see the label here cannot be seen, so let’s make a change, so I believe we can try this. Uh, tight layout function here here. We go so now! This is of this the final result, so basically, we have, uh, created create a time con time domain signal, which is here and we have compute fft, then plot the frequency spectrum here, So I think it looks quite good. I hope this video is helpful to you. Thank you for your time bye.