roujhan
roujhan

Reputation: 13

FFT in MATLAB: wrong 0Hz frequency

I want to use fft in MATLAB to analize some exprimental data saved as an excell file. my code:

A=xlsread('Book.xls'); G=A'; x=G(2, : );
N=length(x);
F=[-N/2:N/2-1]/N;
X = abs(fft(x-mean(x),N))
X = fftshift(X);
plot(F,X)

But it plots a graph with a large 0Hz wrong component, my true frequency is about 395Hz and it is not shown in the plotted graph. Please tell me what is wrong.

Any help would be appreciated.

Upvotes: 1

Views: 2329

Answers (1)

Amro
Amro

Reputation: 124563

Assume we read the signal from file:

G = xlsread('Book.xls');
t = G(:,1);
x = G(:,2);
N = length(x);

First we estimate the sampling frequency from the time axis, and build the frequency vector:

Fs = 1 ./ abs( t(2)-t(1) );
F = (-N/2:N/2-1)*Fs/N;

then compute the FFT and plot:

X = abs( fft(x-mean(x),N) );
X = fftshift(X);
stem(F,X)

finally find the peak and the corresponding frequency:

>> [~,ind] = max(X);
>> F(ind)
ans =
         -400

you might want to zoom-in near the origin to see things more clearly:

xlim([-1000 1000])

Upvotes: 8

Related Questions