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

4 views (last 30 days)
Chanoknunt Sangsobhon on 28 Jul 2022
Edited: David Goodmanson on 4 Aug 2022
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.
datafile='name.csv';
%initial setting
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')
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.

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])
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.

R2020b

### Community Treasure Hunt

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

Start Hunting!