Plotting the Fourier Transform in Python (DFT/FFT)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to Signal processing with Paul in this video what I want to do is show you how you can plot the Fourier transform of signals here in Python I think it can be very difficult for people it was certainly difficult for me to go from understanding the theory to being able to plot these things in a you know software code so that's what we're going to try to do I'm going to try to walk you through how to do this and show you some of the difficulties that can come up when you do this so what I'm going to do is create a new file and I'm going to just call this plot fft and I'm going to call this a Jupiter notebook so we can run this code interactively I'm using vs code for this and I'm going to choose my anaconda terminal it's already set up this way you can basically look at your python environments and if you're using vs code like I am it'll automatically find it for you so what I'm going to do is import numpy as NP and we want to import matplotlib dot Pi plot as PLT so we can actually plot things so now what I'm going to do is Define X of t as a pulse function so what I'm going to do is say num T equals 100 and we're going to say XT equals numpy.zeros so we're gonna have a bunch of zeros and I want to have 100 of them so I'm going to put that in there and what I'm going to do next is just say XT from 0 to up to nine so it's not inclusive of this second number so it's going to be zero one two three four five six seven eight nine not to ten equals one and what we're then going to do is now I'm going to just plot the signal so that's why we imported matplotlib.pi plot we're going to just do plt.plot X of t and I'm just going to give it a title plot of X of t X label now because we don't have an actual X label here this is just going to be the sample index and plt.y label is going to be the value of x of T so let's go ahead and plot that and sure enough what we see here is this pulse or this box function all the way up to 9 and then it flattens out and it's zero otherwise so we defined it as zero and then we just basically set these guys to be one now let's go ahead and plot the F Fourier transform this or the fft so the way to do that is I'm going to say fxt equals numpy dot fft.fft so we're calling the fft package and we're just calling this simple fft function and we're going to pass in X of t so now we have this we can go ahead and plot it so I'm going to say plt.plot fxt plt.title plot of Ft of X of t lt.y label now this is difficult because remember that the Fourier transform is going to have both real and imaginary parts and that's one thing that's really important to realize is that you actually are going to have two plots one for the real and one for the imaginary part now what some people always ask is well I'm putting in a real signal shouldn't I only have a real Fourier transform the answer is no and that's because remember you have even and odd Parts whenever you have any signal and all the even Parts get shoved into the cosine term or the real term and all the odd Parts get shoved into the sine term which is purely imaginary so a signal that is both you know that is not necessarily even or odd is going to have real and imaginary parts so let's go ahead and split this up into two different plots so the way I'm going to do this is say plt.subplot and I'm going to create a 1 by 2 grid of plots and this is going to be the first one so first plot is the real part and we're going to plt.plot numpy dot real the real part of the Fourier transform Fourier transform of X of T so the Y label is going to be the magnitude and the PLT dot X label is going to be the frequency index and similarly I'm going to do the imaginary part as well so this is the second part which is the imaginary part we need to look at the second subplot so this controls the index so it's a one by two this is the First Column the first I guess plot and this is going to be the second one so this is numpy dot imaginary and this is plot of the imaginary part of this so sure enough I run this and what we see is we have both a real signal this is the real part and we have the imaginary part because our signal here is neither completely even nor odd it's a little bit of both and by even is what I mean is X of T equals x of minus t so it would need to be mirrored across the middle here now one thing that's a little bit confusing about the way this is plotted and this is just has to do with the way the transform itself is done is we start at zero frequency then we go up to halfway and then after halfway we then jump from the highest frequency we have to the lowest negative frequency we have so we go up to you know basically frequency index 50 and then now at 51 we're now plotting frequency index of minus 50 pretty much so this can be a little bit confusing because of the way that it's plotted this doesn't look like our standard sync function that we're used to seeing so there's this really Nifty feature it's present in Matlab and it's present here in numpy called fft shift and what that'll do is it'll take this half of the signal and it'll just bring it over here and shift everything to the right so that way you're looking at this from left to right you start with negative frequency you have zero frequency in the middle and then you have your positive frequency on the right same with the imaginary part so to do this all we have to do is just type in numpy.ffft.fft shift and I'm going to do that over here like so and sure enough when I plot it look we have our standard sync function it's completely real or sorry it's completely yeah this is the real part and it's even as we expect and then this is the sort of odd part here this is what we see from the conjugate symmetry the reason we see this where the imaginary part the the positive imaginary part is equal to the negative of the negative imaginary part and then for the real Parts the real part is basically equal to the negative of the real part is because this signal was a completely real and a real signal is going to have conjugate symmetry meaning the real part is going to be even symmetric and the odd part is going to be odd symmetric which is what we see here now what I want to do is show you what happens when we actually get an even symmetry in the time domain doing this the right way will actually cause us to only have a completely real Fourier transform there shouldn't be any imaginary part so the way I'm going to do this is rather than setting 0 to 9 to be equal to one what I'm going to do is say x t from zero to five so this is going to be 0 1 2 3 and 4 is going to equal one and then what I'm going to do is say x t from minus 4 to the end is going to be equal to 1. now why am I doing this what this is going to do is this is going to set 0 1 2 3 and 4 so these five elements to be one and then why I need to do minus 4 to the end is it's going to be the N minus 4 the N minus 3 the N minus 2 the N minus 1 and then the very last component equal to 1 as well so this is going to ensure we have the Symmetry that we need and let's go ahead and see what happens when we plot it sure enough notice what we have this is the imaginary part and it's pretty much zero this is one times ten to the minus sixteenth so that's you know pretty much down to numerical precision and what we have here is a completely even signal as well now suppose our signal was completely odd that would mean rather than we had one here and then we had minus one down here so if I plot this this would sort of be like an odd type of signal and if we look at the Fourier transform sure enough this is what we have we have zero for the real part and our spectrum is completely imaginary so remember that distinction between even and odd symmetry that we see here if our signal is even symmetric it's going to be completely real if it's odd symmetric it's going to be completely imaginary if it's a mix of both it's going to have both components but because this signal is completely real we have conjugate symmetry meaning the imaginary part the positive part is equal to the negative of the negative part and then the real part is even symmetric the one thing to remember is that you're always plotting something that's a combination of a real and imaginary part now if you want to plot this in terms of polar form instead of rectangular form we can do that as well so I'm going to run this back to what we had before so we have both a real and an imaginary component and what I'm going to do is copy my plot except this time I'm going to plot it a little bit differently so this is going to be the plot in polar form plot in polar form and what we're going to see here is this rather than the real part we're going to do numpy.abs so this is the magnitude the Fourier transform of X of T and then this is going to be numpy Dot angle luckily they have a nice little function for this this is going to be the angle of the Fourier transform of X of T so this is the angle and sure enough when we run this here's what we see we see our magnitude notice how unlike the previous plot it's only plot it's not plotting the sort of negative pieces it's basically rectified because it's only plotting from zero up and then our angle here this is what you're seeing you're basically seeing the phase angle that's due to the rotation once again if we go back to what we had before where we didn't have where we had our even symmetry looking here or this would I guess be the the odd symmetry so let me let me do the even symmetry um you can kind of see the angle this is basically telling you almost where it changes Direction um to a certain point here so that's what's being plot here but part of this is due to the fact that you have this wrap around from minus pi to Pi um that's kind of what you're seeing at this particular point so anyway thanks for watching I hope you learned something I hope you found this helpful and I will see in the next video peace
Info
Channel: Signal Processing with Paul
Views: 17,470
Rating: undefined out of 5
Keywords: DSP, Signal Processing, Engineering
Id: arYKpiVACvY
Channel Id: undefined
Length: 10min 46sec (646 seconds)
Published: Fri Sep 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.