Why does pyfftw output zero?

147 Views Asked by At

This must be a stupid question but I could not figure what is wrong by myself. I wanted to test pyfftw so I ran the following code:

import numpy as np
import pyfftw

a = np.random.randn(2,64,64)
b = np.zeros(2,64,33)*np.complex(0.)

pyfftw.FFTW(a,b,axes = (-2,-1), direction = 'FFTW_FORWARD')

I expect that the array b to be changed to the Fourier modes of array a. But it turns out that b is still all zeros. So what is wrong here? Can anyone give a hint? Thank you very much.

Here is the follow up. Thanks AKX and Hamaza for pointing out that I should run the execute() method to get the FFT done. But Now there is another issue. I tried calling pyfftw in a self-defined function. The output shows that the input array is changed to all zeros.

def f2fh(f):
    ftmp = np.copy(f)
    nz,nx,ny = f.shape
    nky = ny
    nkx = (nx/2)+1
    fh = np.zeros((nz,nky,nkx))*np.complex(0.)
    print 'ksksks',ftmp.shape,fh.shape,ftmp
    pyfftw.FFTW(ftmp, fh, axes = (-2,-1), direction = 'FFTW_FORWARD').execute()
    print 'a',ftmp
    return fh

The output is enter image description here

Can anyone give a hint what is wrong this time? Thanks a lot...

2

There are 2 best solutions below

2
On

You're not calling execute(). Via the docs:

The actual FFT or iFFT is performed by calling the execute() method.

execute(): Execute the planned operation, taking the correct kind of FFT of the input array (i.e. FFTW.input_array), and putting the result in the output array (i.e. FFTW.output_array).

You might also want to use the "easier" interfaces described over here:

b = pyfftw.interfaces.numpy_fft.fft(a)

0
On

You need to call execute().

pyfftw.FFTW(a,b,axes = (-2,-1), direction = 'FFTW_FORWARD').execute()