2
votes

I am performing a fft on a wave consisting of two simple sine waves with fft-js. The number of samples is 512 and the running time is 16 seconds. So the sample frequency is 32Hz. When looking into the resulting frequency this goes up to 15.9375 instead to the sample frequency divided by two (16 Hz). Why is this the case and is there a way to include the sample frequency divided by two?

1
Information at fs / 2 is generally not useful since it's the Nyquist frequency and it will have been significantly reduced in amplitude by your anti-aliassing filter.Paul R
Your question is well posed and legitimate. The FFT will give you a result where you expect it, but this could depend on the implementation (always try higher level environments first to check the math, say Mathematica, matlab, numpy) . Check whether you have a frequency bin at -16 Hz. Since the FFT wraps around, this is the value to use.roadrunner66
Minor point. Technically the sampling rate is 32 Sa/sec. It's better not to use the term sampling frequency, because the highest frequency we sample is 16 Hz.roadrunner66
16-15.9375 = 0.0625. 1/0.0625= 16. So you were right to be suspicious, the program gave you the (n-1)th frequency only.roadrunner66
We can only sample frequencies lower than half the sampling frequency. A signal at exactly half the sampling frequency is aliased.Cris Luengo

1 Answers

0
votes

This short Python example shows that you were right to look for the highest frequency and it is at a nonintuitive place. The Nyquist Frequency for the FFT is actually at the other end of the frequency vector:

import numpy as np
import matplotlib.pyplot as p
%matplotlib inline

T=1 # secs
d=0.1 # secs
n=int(T/d)

t=np.arange(0,T,d)  
print(f'time : {t}')

freq=np.fft.fftfreq(n,d)
print(f' frequencies (unshifted) : {freq}')

sfreq=np.fft.fftshift(freq)
print(f' frequencies (shifted) : {sfreq}')

output:

time : [0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
 frequencies (unshifted) : [ 0.  1.  2.  3.  4. -5. -4. -3. -2. -1.]
 frequencies (shifted) : [-5. -4. -3. -2. -1.  0.  1.  2.  3.  4.]

Now Paul is also right, that a proper use of the FFT should use anti-aliasing measures such that ideally the energy at the very highest frequencies are almost zero.