[Python] Discrepancy between C++ FFTW and pyFFTW

Discussão em 'Python' iniciado por Stack, Maio 2, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using the FFTW library in C++, and now need to use it in Python. The common approach to this seems to be the pyFFTW library.

    I've found a difference between the outputs of very basic programs using the FFTW library in C++ and the pyFFTW library in Python.

    The following is a minimal reproducable example.

    With pyFFTW version 0.15.0:

    import numpy as np
    import pyfftw

    K = pyfftw.empty_aligned(4, dtype='complex128')
    X = pyfftw.empty_aligned(4, dtype='complex128')
    ifftDoer = pyfftw.FFTW(K, X, direction='FFTW_BACKWARD')

    K[:] = np.array([1, 2, 3, 4])

    ifftDoer()

    print(X)

    > OUTPUT: [ 2.5+0.j -0.5-0.5j -0.5+0.j -0.5+0.5j]


    With FFTW version 3.3.8:

    int main() {

    int N = 4;

    fftw_complex *in, *out;
    fftw_plan p;
    in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
    out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
    p = fftw_plan_dft_1d(N, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);

    for (int i = 0; i < 4; i++) {
    in[0] = i+1;
    in[1] = 0;
    }

    fftw_execute(p);
    printFFTWComplexArr(out, N);
    fftw_destroy_plan(p);
    }

    > OUTPUT: (10, 0), (-2, -2), (-2, 0), (-2, 2),


    There is a factor of 4 between the results. Playing with the code, this is actually a factor of N, the size of the array.

    This in and of itself is okay, but it is very strange considering that the code is basically 1 to 1, and pyFFTW always always calls itself an FFTW wrapper. It'd seem strange to me to include subtle normalizing factors with 0 mention.

    Furthermore, I couldn't find anything about this online, searching stuff like "pyFFTW scaling" and "FFTW vs pyFFTW different result" etc, so this makes me worried that somehow I've made a mistake.

    If someone has an explanation, I'd love to know.

    Continue reading...

Compartilhe esta Página