0

I need a small clarification on this wonderful answer. I have generated an harmonic sample with 5 frequencies (3 9 15 21 27), using the Fourier series formula for square wave. Then I sampled it at 32Hz, expecting to identify first 3 frequencies which are below the Nyquist limit. After I passed the 32 samples(16bit) through the FFT function, I calculated the magnitude using sqrt(real^2 + imag^2).

Generated Frequencies: 3 9 15 21 27
The magnitudes [sqrt(real^2 + imag^2)] after FFT:
6.87475e-15
2.71352e-15
2.14864e-16
20.3718-----------3
1.75333e-15
2.26354-----------5
3.97083e-15
1.16198e-14
2.33558e-14
6.79061-----------9
9.38245e-15
2.91026-----------11
6.42032e-15
9.03851e-15
1.48744e-14
4.07437-----------15

So... I found 5 frequency bins having high magnitudes. I know only 3, 9, 15 are the valid values expected. But I see 5 and 11 having respectable magnitudes. How do I determine the cut-off magnitude to discard these invalid frequency bins?

Sharath
  • 1,627
  • 2
  • 18
  • 34
  • 2
    You first need to run your signal through a low pass filter before sampling, otherwise you''ll get aliasing. Once you've filtered your signal, then you can perform an FFT on it (for better accuracy you should use windowing as mentioned ny hotpaw2) – dsp_user Feb 02 '19 at 18:30
  • Low pass filter to remove frequencies above 16Hz? – Sharath Feb 02 '19 at 18:39
  • Yes, that's right. – dsp_user Feb 03 '19 at 20:13

2 Answers2

2

You will likely get better results if you sample for a longer time window (say 2X more cycles of each waveform), and apply a window function (Hamming or Von Hann, et.al.) to the data before doing the FFT.

If you don't use a smooth window function, your frequency resolution can be poor due to the rectangular windowing artifacts (side-lobes) from any nearby sinusoidal components that are not exactly integer periodic in the FFT width. These side-lobes cause the FFT results of nearby frequency components to interfere with each other, sometimes constructively, sometimes destructively. So there's no simple rule to determine the actual frequency peaks of interest from the mix.

A longer data window (more samples, more periods of each frequency of interest) also better separates the FFT result bins of the peaks of interest, thus reducing potential interference.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
1

The issue is that you violated the Nyquist–Shannon sampling theorem: The sampling frequency needs to be at least twice the signal bandwidth. In your case (where the frequency range starts at 0 like most of the times) this means that the sampling frequency needs to be at least twice the maximum signal frequency. So, you either need to sample with at least 54Hz or you need to limit the signal frequencies to 16Hz by using a low pass filter. Not taking care for that leads to aliasing, in your case showing the peaks at 5Hz and 11Hz.

chrisemb
  • 79
  • 5
  • You didn't understand question. I was only looking to identify frequencies below 16Hz in the signal I generated. I was surprised to find two other frequencies I didn't generate. Anyway, it turned out my FFT function was buggy. Then I used the one from Eigen library, and it worked fine. – Sharath Jan 18 '21 at 17:55
  • Well, the question „How do I determine the cut-off magnitude to discard these invalid frequency bins?“ does not address the problem. Invalid frequency bins cannot be identified by magnitude. They occurred due to aliasing which can only be avoided by having the highest frequency of the input signal below half of the sample frequency. So, cut off any frequencies above 16Hz by a low pass filter (or increase sample frequency). This aliasing is not a result of a buggy FFT, it occurs also with a ‘good’ FFT. Windowing does not solve it either, you need to pay attention to Shannon. – chrisemb Jan 19 '21 at 07:46