FFT of Cosine wave in Matlab

5 次查看(过去 30 天)
Sufyan
Sufyan 2012-9-26
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 ?

回答(1 个)

Honglei Chen
Honglei Chen 2012-9-26
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 个评论
Sufyan
Sufyan 2012-9-27
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 2012-9-27
编辑:Wayne King 2012-9-27
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).

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by