Search code examples
c++fftw

What if n_samples and size_input do not match in fftw implementation


I am using fftw3 lib for computing a fourier transform in c++. I have to repeatedly call it with a different input array everytime. I am initializing my array (called 'input' in code ) just once with size MAX_ELEMS and then using it as input to the fftw_plan function (changing contents and their size) on every call. However I am changing my n_samples everytime according to actual size of my array (number of valid data samples in the array). I wanted to know if this will cause any problems in my implementation- execution/accuracy wise.

fftw_plan fftw_plan_dft_r2c_1d(int n_samples,
                                double *in, fftw_complex *out,

Solution

  • Ideally you want to pick a specific size of FFT and stick with it, for two reasons:

    • creating/destroying plans takes a lot of time relative to the FFT itself, so you're throwing away any speed advantage from using FFTW if you create/destroy a plan for evert FFT you perform
    • the frequency resolution will be different for each different size of FFT, which could make interpretation of successive output data troublesome.

    Ideally you should use a fixed FFT size so that you create/destroy a plan just once.

    If your data stream is contiguous then just pick an appropriate FFT size, N, and accumulate samples in a buffer until you have N samples to process, and save the rest until you have N samples again.

    If your data is not contiguous then pick a reasonable size FFT, N, and then when you get < N samples you can pad the input with zeroes. When you get > N samples you can discard the extra samples.

    In all cases of course you must apply a suitable window function prior to the FFT.