Question regarding fft output

So here's my code, what I'm confused about is why my locations in the frequency spectrum occur at 4 & 16 rather than 2 & 18. Does it have to do with the fact that I am sampling 2 seconds rather than 1?
clear all
N=20;
T=2;
fs=N/2;
f0=2;
t=0:(1/fs):(T-1/fs);
x=4*cos(2*pi*f0*t);
X=abs(fft(x))/N;
f=0:19;
figure(1)
subplot(2,1,1)
plot(t,x)
subplot(2,1,2)
stem(f,X)

답변 (3개)

Youssef  Khmou
Youssef Khmou 2013년 3월 27일
편집: Youssef Khmou 2013년 3월 27일

0 개 추천

hi,
Your approach is correct but Your signal contains only one frequency f0, the graph shows the two sided spectrum , but you need to adjust the frequency axis , try , this GENERALLY:
clear all
N=20;
T=2;
fs=N/2; % choose higher FS for better resolution
f0=2;
t=0:(1/fs):(T-1/fs);
x=4*cos(2*pi*f0*t);
L=length(x);
N=ceil(log2(L));
fx=fft(x,2^N)/(L/2); % for normalization to get the right MAGNIT
f=(fs/2^N)*(0:2^(N-1)-1);
figure(1)
subplot(2,1,1)
plot(t,x)
subplot(2,1,2)
plot(f,abs(fx(1:end/2)));
Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 27일
편집: Azzi Abdelmalek 2013년 3월 27일

0 개 추천

Your data are not correct, If your frequency f0=2 then the period T should be T=1/f0
N=20;
T=2;
f0=1/T;
ts=T/N
t=linspace(0,T-ts,N);
x=4*cos(2*pi*f0*t);
X=abs(fft(x))/N;
f=0:N-1;
figure(1)
subplot(2,1,1)
plot(t,x)
subplot(2,1,2)
stem(f,X)
w=2*(0:(N-1))*pi/(N*ts)
Also you have x=4*cos(2*pi*1*f0*t); % It's the first f0 neither 2nd nor 4th.
wich correspond to w(2), (w(1) corresponds to the continuous component)
w(2)

댓글 수: 3

Youssef  Khmou
Youssef Khmou 2013년 3월 27일
편집: Youssef Khmou 2013년 3월 27일
hi, the frequency is f0=2Hz, but his sampling rate is correct Fs=N/2=10 Hz,
your solution does not show the frequency at 2hz
Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 27일
Hi Yousef. If the frequency f0=2, then he should calculate the fft in one period, that means T=1/f0, which is not his case (T=2)
In my answer f0=1/T=1/2=.5
The result is w(2) (in rad/s) corresponds to 2*pi*f0=3.14 (f0=0.5)
Youssef  Khmou
Youssef Khmou 2013년 3월 27일
ok i see....thanks for explaining .

댓글을 달려면 로그인하십시오.

Wayne King
Wayne King 2013년 3월 27일

0 개 추천

The frequency axis is incorrect. The DF is not 1. The "bins" in the DFT are spaced at Fs/N where Fs is the sampling frequency, so the OP's bins are spaced at 1/2, not 1.
Using the OP's original code:
N=20;
T=2;
fs=N/2;
f0=2;
t=0:(1/fs):(T-1/fs);
x=4*cos(2*pi*f0*t);
X=abs(fft(x))/N;
% now just create a frequency axis for the nonnegative frequencies
df = fs/length(x);
f = 0:df:fs/2;
Xdft = X(1:length(x)/2+1);
figure(1)
subplot(2,1,1)
plot(t,x)
subplot(2,1,2)
stem(f,Xdft);

카테고리

태그

질문:

2013년 3월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by