How can I plot a correct fft of cosine wave?
46 views (last 30 days)
Show older comments
Henrique Orlandini
on 2 Dec 2016
Commented: Image Analyst
on 2 Dec 2016
I'm starting with Matlab and our professor told us to do some exercises to get it going.
One of them it's to plot the Fourier Transform of a cosine (cos(2*pi*t)), but sampled with sample frequency of fs=12Hz.
So I did the following:
>> fs=12;
>> n=0:1/fs:11/fs;
>> x=cos(2*pi*n);
>> stem(n,x)
Which gives me the following:
Then I calculated the fft of x:
>> y=abs(fft(x));
>> k=0:fs:11*fs;
>> stem(k,y)
Which should give me the real part of the Fourier Transform of a cosine, but If I recall correctly, the FT of a cosine is two spikes, one at (wave frequency)/2*pi and another at -(wave frequency)/2*pi, but I got this:
Why I'm getting both spikes at positive side of the axis? One could argue the DFT is periodic, which is, but it should be periodic with 2*pi, no?
I think I'm really messing up DTFT and DFT of cosine, but I can't figure out what can I do to correct my plot.
Thanks in advance.
2 Comments
Adam
on 2 Dec 2016
Edited: Adam
on 2 Dec 2016
It isn't the easiest thing to find in the help (so much so that I gave up!), but the output of the fft is as follows:
0 positiveFrequencies nyquist negativeFrequencies
with there being n/2 - 1 of each of positive and negative frequencies. The negative frequencies can equally well be regarded as frequencies between nyquist and the sampling frequency though, which is where they sit in that output, it is just convention as to how you wish to display it, so long as you understand what order you start with.
Personally I always just take the first n/2 + 1 points when looking at the spectrum because I am not interested in the negative frequencies. Some people like fftshift to move 0 to the middle, but I have never used it as I am used to looking at a double-side spectrum this way now.
Accepted Answer
Image Analyst
on 2 Dec 2016
You forgot to use fftshift(). Remember, with fft, the origin is at element 1 and then "wraps around" so to see the origin in the middle like you'd expect, and positive frequencies to the right and negative frequencies to the left, you need to call fftshift().
2 Comments
Image Analyst
on 2 Dec 2016
It's because you don't have a signal infinite in the x direction. It's multiplied by a rect function, so your fft will be two delta functions convolved with sinc functions (which gives two sinc functions) instead of two delta functions like you might think.
More Answers (1)
John D'Errico
on 2 Dec 2016
Edited: John D'Errico
on 2 Dec 2016
What did you plot? Hint: look at the code you wrote:
k=0:fs:11*fs;
stem(k,y)
What is plotted as the x axis? The numbers 0:fs:11*fs.
Did MATLAB plot what you told it to plot? It did give you two spikes as you expected.
0 Comments
See Also
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!