Why I got the dominant frequency as 1/time when I running fft?

8 views (last 30 days)
Hi, I have a problem with fft.
When I was doing fft , it give the dominant frequency as 1/time Hz. I can not figure out what is problem.
For example, when my data is 40 seconds, the highest peak is at 0.025 Hz, but when I cut it of as 20 seconds, the dominant frequency is 0.05 Hz. The original data are the same set data and it is a low frequency data.
%file read
datafile='name.csv';
%initial setting
all_data = load(datafile);
Fs = 100;
L = length(all_data);
N = length(all_data);
x = fft(all_data,N);
f = (0:N/2-1)*(Fs/N);
ssb = x(1:floor(L/2));
ssb(2:end) = 2*ssb(2:end);
% drawing figure
figure
plot(f,abs(ssb/L))
xlabel('f (Hz)')
label('Magnitude')
  4 Comments
dpb
dpb on 28 Jul 2022
Edited: dpb on 28 Jul 2022
Would need to see both cases together to see what you did to make the difference. If you recompute the f vector for the new time series length, the Fmax will still be the same (approx, your N/2-1 will cut it off at one df short of Nyquist which is Fmax = Fs/2, but it's close).
If you don't use the new N and only cut the number of elements of f used to match, then you'll only go to the fraction of the total Fmax as that ratio which would seem to match the symptom.
The relationship is that Fmax stays the same, fixed by the sampling rate only the df (the frequency resolution) has to change with the changing sample time, T, overall (number of points). If you only change the number of elements used of the same frequency vector, then the df won't be consistent with the different number of samples.
Chanoknunt Sangsobhon
Chanoknunt Sangsobhon on 29 Jul 2022
Oh, I'm sorry. I deleted above message and attached both data here.
But yes, I just cut some of not imaportant element off. So you mean the mistake is the scale of f vector, isn't it?
btw thank you for your answer, I think I understand it more.

Sign in to comment.

Answers (1)

David Goodmanson
David Goodmanson on 29 Jul 2022
Edited: David Goodmanson on 29 Jul 2022
Hi CS,
here is an example, first with 800 points, then 400 points. The frequency scale is constructed differently in each one of those cases, and if you compare fig 2 and fig 3 you can see that the peak appears at the same frequency, 25 Hz. The key is that in each case the time array spacing delt and the frequency array spacing delf satisfy the golden rule for fft array spacing:
delt*delf = 1/n
where n is the number of fft points
fs = 1000;
delt = 1/fs;
n = 800;
t = (0:n-1)*delt; % delt = 1/fs
a = 20;
f0 = 25;
y = exp(-a*t).*sin(2*pi*f0*t);
figure(1)
plot(t,y); grid on
f = (0:n-1)*(fs/n); % delf = fs/n
z = fft(y);
figure(2)
plot(f,real(z),f,imag(z)); grid on
xlim([0 50])
n1 = 400;
t(1:n1);
y1 = y(1:n1);
f1 = (0:n1-1)*(fs/n1);
z1 = fft(y1);
figure(3)
plot(f1,real(z1),f1,imag(z1)); grid on
xlim([0 50])
  3 Comments
dpb
dpb on 3 Aug 2022
Edited: dpb on 3 Aug 2022
Not sure about what you think your understanding is, but for a baseband FFT (from DC to Fmax-->Nyquist frequency) that the MATLAB FFT computes, Fmax is ALWAYS 1/2 the sampling frequency no matter what the sample length -- ergo, if you keep the sampling frequency the same and cut the number of points in half, then the frequency resolution of the FFT has to also be cut in half -- otherwise it would show the same thing as the time in being half as long, the frequency vector would only reach Fmax/2 or one-fourth the Nyquist. This clearly just can't be right.
As @David Goodmanson notes above,
dt*df = 1/n
is a fixed relationship that must hold -- consider
dt1*df1 = 1/n1 % case 1
dt2*df2 = 1/(n1/2) % case 2 -- n2 --> 1/2 n1
If the sample rate (delt) is the same, then dividing out common factors one is left with
df1/df2 = (1/n1)/(1/(n1/2))
or
df1/df2 = 1/2
or
df2/df1 = 2
The other relationship that follows from the above is that
df = 1/(n*dt)
or
df = 1/T
where T is the total sample time (n*dt). Clearly if you halve T, you double df.
David Goodmanson
David Goodmanson on 4 Aug 2022
Edited: David Goodmanson on 4 Aug 2022
Hi CS,
here is just an alternate way of saying what dpb has already laid out. Starting with
delt*delf = 1/n
if you cut out the second half of the time domain as you did, then delt stays the same, n --> n/2 and 1/n --> 2/n. Therefore delf must double. The frequency grid is coarser, but the maximum frequency fmax, which is proportional to n*delf, becomes (n/2)*(2*delf) and stays the same. I think that's the simplest way to look at it.
That is all consistent with the idea that the sampling frequency fs = 1/delt, you have not changed delt, and regardless of the number of points, fmax (the nyquist frequency) = fs/2, so it does not change.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!