FFT of Cosine wave in Matlab

7 views (last 30 days)
Sufyan
Sufyan on 26 Sep 2012
I am having trouble understanding how FFT has calculated,
Here is my code that first generates the cosine wave with Sampling freq of 1MHz, and then calculate its FFT.
Note that i've used matlab FFT example to plot the FFT of my signal.
n=[0:0.1:10]
F1=100*10^3; %100KHz and t=1*10^-5
Fs=1*10^6; %1MHz
T=1/Fs; %1*10^-6
%Time period = t/T => 10
xn=cos(2*pi*(F1)*n*T)
subplot(2,1,1),stem(n,xn);
ylim([-1.5 1.5]);
xlim ([0 10]);
grid on;
subplot(2,1,2),fft(xn);
ylim([-1.5 1.5]);
xlim ([0 10]);
grid on;
Y = fft(xn,512);
Pyy = Y.*conj(Y)/512;
f = 1000*(0:256)/512;
plot(f,Pyy(1:257))
title('Frequency content of Signal')
xlabel('frequency (Hz)')
My Questions are,
What is the power Spectrum (Pyy) and why Matlab plotted 512 points FFT how it knows the range ? How the value of 'f' is calculated i.e f = 1000*(0:256)/512 ?
Now have a look at Output
Please explain how the frequency spectrum is generated and why it is limited to 0 to 50Hz ?

Answers (1)

Honglei Chen
Honglei Chen on 26 Sep 2012
Hi Sufyan,
First of all, please delete the duplicate posts so people can contribute at the same place.
The power spectrum tells you what are the frequency components in your signal. The number of points in the spectrum is determined by the number of points in your FFT. In this case, you chose 512. In addition, you only plotted half of it.
I think the more important question is why you are seeing the frequency of signal at within 50 Hz when it is actually should be at 100 kHz. This is due to the wrong sampling rate you specified in the computation. 512 is the number of points in FFT, but it is not the sampling rate. You should actually write your f variable as
f = 1e7*(0:256)/512
If you do this, you can see that the peak appears at the right place.
Note that your sample rate is actually 10 MHz, not 1 MHz you claimed. Because in your signal
xn = cos(2*pi*F1*n*T)
You are actually sampling 1e7 points a second since your n is 0:0.1:10.
HTH
  2 Comments
Sufyan
Sufyan on 27 Sep 2012
Sir, but i set Fs = 1*10^6 then how sampling rate is 10MHz , i'm a bit confused with actual frequency and Sampling frequency, if sampling frequency is 1MHz and actual Frequency is 100KHz
and when the samples are generated don.t they generate at the frequecy rate of Fs ? P.S: how am i supposed to set the value of 'n'
Wayne King
Wayne King on 27 Sep 2012
Edited: Wayne King on 27 Sep 2012
I think Honglei explained it as well as it can be explained. As Honglei states, you are evaluating cosine over time increments of n*T. If you do:
t = n*T;
diff(t)
You'll see the increment is 10^(-7).

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!