rshuston / FFT-C

FFT implemented in ANSI C
MIT License
20 stars 6 forks source link

Windowing Functions? #1

Open RussellHaley opened 3 years ago

RussellHaley commented 3 years ago

Hi,

Thank you so much for your work on this package. I have limited math skills so I apologize if I am way off, but I don't have the ability to write an FFT and most of the ones I have found are GPL or proprietary. From what I understand, FFTs can use different "Windowing" functions being something like Ham, Hanning, BlackmanHarris, etc. Is it possible to adjust the Window for these FFT algorithms? I am trying to work through your excellent "Trip Down Fourier Lane".

Thanks!

rshuston commented 3 years ago

Hello,

You can use any windowing function. Basically all you are doing with a window function is shape the input data to your chosen window envelope. Each point in the window is a weighting value. The size of the window is the size of your data, so for a 1024-pt FFT, your window will be 1024 points also. For instance, a Hanning window is an offset cosine:

int N = 1024;
float w[1024];
int k;
for (k = 0; k < N; k++) {
    w(k) = 0.5 - 0.5 * cos(2 * M_PI * k / N);
}

Hanning Window

You apply your window to weight your input data before performing the FFT:

for (k = 0; k < N; k++) {
    data[k].re *= w[k];
    data[k].im *= w[k];
}
ffti_f(data, ...);

The main reason you use windowing is to reduce what is called DFT leakage. This is due to the fact that the DFT (and FFT) is inherently cyclic; it establishes a periodic relationship between the time and frequency domains. If you are attempting to DFT a single pulse waveform, the resulting sequence of frequencies is still implicitly periodic, and the resulting envelope can be misleading in the sense that it is approximating a periodic time waveform as its input. Periodicity is a consequence of the sampling relationships of the DFT. Windowing essentially shapes the input to look more like an an aperiodic input, thus reducing the effects of the DFT leakage frequencies.

Understand that the choice of windowing envelope is a design choice that is based on the intended use of the spectral data. For instance, Doppler target detection warrants Hanning or Hamming windows because they produce good frequency resolution of the target velocity, whereas signal power level detection warrants Blackman or Flat top windows because they produce good amplitude accuracy.

Windowing is like applying a color profile on a computer screen. It depends on what you're trying to do. Edit a picture? Edit source code? Watch a movie? Each one produces better results for a given profile. Windowing is the same thing.

Wikipedia has a good write-up on window functions (https://en.wikipedia.org/wiki/Window_function). Don't let the math throw you; concentrate on the pictures of the windows to get a better feel for what they're doing.

Good luck, and I hope this helps you.