# Does the number of samples matter for FFT, and how to get a specific frequency visible?

If I have a signal that has 740 samples, the sampling frequency is 1000 Hz, and I take an FFT of length 1024, are the frequencies I get now 0, fs/1024, 2*fs/1000, ..., 511*fs/1024? I mean, do I get this correctly: the frequency resolution does not depend on the number of samples I have, only FFT length and sampling frequency. If I had 1480 samples, the resolution would be still the same? This makes my head hurt.

Next, suppose that the signal I have has an interesting frequency at 26 Hz (strictly that one frequency). How do I get it appear properly in the frequency spectrum (my frequency bins are spaced 1000/1024 = 0.9766 Hz apart, so am I at dead end?).

• How would you go about taking a length 1024 FFT of a signal of length 1480? – jan Jul 25 '13 at 17:26
• @jan : there are "wrap-around" windows where you additively put more samples into an FFT than the length of the FFT. – hotpaw2 Jul 26 '13 at 4:37

The "information content" is related to the number of samples. The length of the FFT merely interpolates the spectral frequency curve represented by the number of samples. If an FFT result bin doesn't end up exactly centered on some desired frequency, you can interpolate it from the nearest bins, using a high-quality (Sinc kernel, et.al.) interpolator, since FFT bins have a greater-than-zero bandwidth (the shape of the transform of the default rectangular or other window). Using a longer zero-padded FFT gives you more interpolation points, and thus a denser looking point plot, but no better spectral separation resolution than provided by the number of actual samples.

this is John BG jgb2012@sky.com

1.- Let be the following example

N1=740;

fs=1e3;dt=1/fs  % time reference
t=[0:dt:N1*dt];

A=1    % signal
f0=26
s0=A*cos(2*pi*f0*t);

n0=wgn(N1+1,1,-36,'complex')'; % -6dBmW noise

n0=wgn(N1+1,1,-16,'complex')';  % 24dBm noise

s=s0+real(n0);

figure(1);plot(t,s);grid on


Nfft=2^10;
S=fft(s,Nfft)

figure(2);
Sabs=abs(S);
Sang=angle(S);
subplot(2,1,1);plot(Sabs);grid on;title('|S|')
subplot(2,1,2);plot(Sang);grid on;title('angle(S)')


How do I get it appear properly in the frequency spectrum?

the way to translate bin position in the FFT to Hz is as follows:

f0_measure=28/(Nffs/2)*fs/2

=
27.3437


the peak on the FFT bin 28 is equivalent to frequency 27.3437 Hz.

The added noise is complex, so it's going to affect both phase and amplitude.

The error on the frequency measurement is

abs(f0-f0_measure)

=
1.343750000000000


2.- Get MATLAB to find the peaks, instead of manually placing the marker on the peak and get the bin # 28:

nmax=find(Sabs==max(Sabs))
nmax=nmax(1)   % remove the FFT mirror


replace 28 with nmax in the above expression translating FFT bin to frequency.

3.- Now your 1st part of the 1st question

the frequency resolution does not depend on the number of samples I have, only FFT length and sampling frequency?

if one increases the amount of FFT bins for the same amount of input time samples, yes the FFT looks the same, but that doesn't mean that the frequency measurement is correct.

Let be

f0=.75*fs


Now there's alias, the sampling frequency is not fast enough and one doesn't notice sampling below the Nyquist limit, then this happens, repeating the above

now

f0_measure=257/(1024/2)*fs/2
f0_measure =
2.509765625000000e+02


the peak should be somewhere around 26Hz, yet you see 250.9Hz

And the 2nd part of the 1st question

If I had 1480 samples, the resolution would be still the same?

No, if you increase the amount of time samples, by this understanding that for the same signal, not a longer observation window, but more samples within the same observation window, thus higher sampling frequency fs, then if you also increase the amount of FFT bins, then you improve the frequency resolution, see the following example.

Let be a 2 tone example, one at 26Hz, the frequency of interest you mentioned, and another one at 28Hz, half amplitude of the 1st one:

N1=740;

fs=1e3;dt=1/fs  % time reference
t=[0:dt:N1*dt];

A=1

f01=26
f02=28

s01=A*cos(2*pi*f01*t);
s02=A*cos(2*pi*f02*t+pi/2);

n0=wgn(N1+1,1,-16,'complex')';  % 24dBm

s=s01+s02+real(n0);

figure(1);plot(t,s);grid on


repeating the above FFT lines,

Nfft=2^10;
S=fft(s,Nfft);

figure(2);
Sabs=abs(S);
Sang=angle(S);
subplot(2,1,1);plot(Sabs);grid on;title('|S|')
subplot(2,1,2);plot(Sang);grid on;title('angle(S)')


one can tell there's not enough frequency resolution

But important, observe that merely increasing the amount of bins, from 2^10 to 2^20

Nfft=2^20;
S=fft(s,Nfft);

figure(2);
Sabs=abs(S);
Sang=angle(S);
subplot(2,1,1);plot(Sabs);grid on;title('|S|')
subplot(2,1,2);plot(Sang);grid on;title('angle(S)')


It DOES improve somehow the frequency resolution, without additionally increasing the amount of input time samples:

It's still not clear whether there are 2 or more tones, but it's clear in comparison to the 2^10 bins FFT that there's something it has to be refined, that would have been ignored with a lower amount of FFT bins, with the same amount of input time samples.

The closer to MATLAB default upper limit of matrix size,

  Nfft=2^26;
..

Nfft=2^28;
..


Above certain amount of FFT bins, there's no significant improvement, following an overlap of FFTs with same amount of input time samples

   for k=1:1:5
N2=N1*2^(k-1)
t=[0:dt:N2*dt];
s01=A*cos(2*pi*f01*t);
s02=A*cos(2*pi*f02*t+pi/2);
n0=wgn(N2+1,1,-16,'complex')';  % 24dBm
s=s01+s02+real(n0);

Nfft=2^10;
S=fft(s,Nfft)

Sabs=abs(S);
Sang=angle(S);

figure(5);plot(Sabs);grid on;title('|S|')
hold all
end

axis( 1.0e+02 *[   0.041474654377880   0.649769585253456    0    4.440962099125365])


and the larger the amount of FFT samples to calculate, the longer it takes to get each FFT.

The result is still slightly better than with a lower amount of FFT bins, yet the amplitude of the 2 tones now resolved, is still not accurate, because we know that they do not have equal amplitude, yet it shows as if they were.

So, YES, increasing the amount of FFT bins for same amount of time input signals, may to a certain extend improve the frequency resolution.

Conclusion:

But the best thing a do, is to increase both at the same time: amount of time samples and amount of FFT bins:

for k=1:1:5
N2=N1*2^(k-1)
t=[0:dt:N2*dt];
s01=A*cos(2*pi*f01*t);
s02=A*cos(2*pi*f02*t+pi/2);
n0=wgn(N2+1,1,-16,'complex')';  % 24dBm
s=s01+s02+real(n0);

Nfft=2^(10+k-1);
S=fft(s,Nfft)

Sabs=abs(S);
Sang=angle(S);

figure(6);plot(Sabs);grid on;title('|S|')
hold all
end

axis=(  1.0e+03 *[ 0.271758436944937   0.815275310834813    0  5.952380952380953])


To the question originator, if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?

Regarding your first question, your frequency granularity is simply $\frac{f_s}{N}$, where $f_s$ is your sampling frequency in Hz, and $N$ is the length of your FFT. (Remember that you always want to take an FFT length of at least equal to your data record length).