15. FFT Reference

The FFT.py module provides a simple interface to the FFTPACK FORTRAN library, which is a powerful standard library for doing fast Fourier transforms of real and complex data sets, or the C fftpack library, which is algorithmically based on FFTPACK and provides a compatible interface. On some platforms, optimized version of one of these libraries may be available, and can be used to provide optimal performance (see Compilation Notes).

Python Interface

The Python user imports the FFT module, which provides a set of utility functions which provide access to the most commonly used FFT routines, and allows the specification of which axes (dimensions) of the input arrays are to be used for the FFT's. These routines are:

fft(data, n=None, axis=-1)

Performs a n-point discrete Fourier transform of the array data. n defaults to the size of data. It is most efficient for n a power of two. If n is larger than data , then data will be zero-padded to make up the difference. If n is smaller than data, then data will be aliased to reduce its size. This also stores a cache of working memory for different sizes of fft's, so you could theoretically run into memory problems if you call this too many times with too many different n's.

The FFT is performed along the axis indicated by the axis argument, which defaults to be the last dimension of data .

The format of the returned array is a complex array of the same shape as data , where the first element in the result array contains the DC (steady-state) value of the FFT, and where each successive ...XXX

Example of use:

>>> print fft(array((1,0,1,0,1,0,1,0))+ 10).real

[ 84. 0. 0. 0. 4. 0. 0. 0.]

>>> print fft(array((0,1,0,1,0,1,0,1))+ 10).real

[ 84. 0. 0. 0. -4. 0. 0. 0.]

>>> print fft(array((0,1,0,0,0,1,0,0))+ 10).real

[ 82. 0. 0. 0. -2. 0. 0. 0.]

inverse_fft(data, n=None, axis=-1)

Will return the n point inverse discrete Fourier transform of data . n defaults to the length of data . This is most efficient for n a power of two. If n is larger than data , then data will be zero-padded to make up the difference. If n is smaller than data , then data will be aliased to reduce its size. This also stores a cache of working memory for different sizes of FFT's, so you could theoretically run into memory problems if you call this too many times with too many different n 's.

real_fft(data, n=None, axis=-1)

Will return the n point discrete Fourier transform of the real valued array data . n defaults to the length of data . This is most efficient for n a power of two. The returned array will be one half of the symmetric complex transform of the real array.

>>> x = cos(arange(30.0)/30.0*2*pi)

>>> print real_fft(x)

[ -1. +0.j 13.69406641+2.91076367j

-0.91354546-0.40673664j -0.80901699-0.58778525j

-0.66913061-0.74314483j -0.5 -0.8660254j

-0.30901699-0.95105652j -0.10452846-0.9945219j

0.10452846-0.9945219j 0.30901699-0.95105652j

0.5 -0.8660254j 0.66913061-0.74314483j

0.80901699-0.58778525j 0.91354546-0.40673664j

0.9781476 -0.20791169j 1. +0.j ]

inverse_real_fft(data, n=None, axis=-1)

Will return the inverse FFT of the real valued array data .

fft2d(data, s=None, axes=(-2,-1))

Will return the 2-dimensional FFT of the array data .

real_fft2d(data, s=None, axes=(-2,-1))

Will return the 2d FFT of the real valued array data .

C API

The interface to the FFTPACK library is performed via the fftpackmodule module, which is responsible for making sure that the arrays sent to the FFTPACK routines are in the right format (contiguous memory locations, right numerical storage format, etc). It provides interfaces to the following FFTPACK routines, which are also the names of the Python functions:

The routines which start with c expect arrays of complex numbers, the routines which start with r expect real numbers only. The routines which end with i are the initalization functions, those which end with f perform the forward FFTs and those which end with b perform the backwards FFTs.

The initialization functions require a single integer argument corresponding to the size of the dataset, and returns a work array. The forward and backwards FFTs require two array arguments -- the first is the data array, the second is the work array returned by the initialization function. They return arrays corresponding to the coefficients of the FFT, with the first element in the returned array corresponding to the DC component, the second one to the first fundamental, etc.The length of the returned array is 1 + half the length of the input array in the case of real FFTs, and the same size as the input array in the case of complex data.

>>> x = cos(arange(30.0)/30.0*2*pi)

>>> w = rffti(30)

>>> f = rfftf(x, w)

>>> f[0]

(-1+0j)

>>> f[1]

(13.6940664103+2.91076367145j)

>>> f[2]

(-0.913545457643-0.406736643076j)

Compilation Notes

On some platforms, precompiled optimized versions of the FFTPACK library are preinstalled on the operating system, and the compilation procedure needs to be modified to force the fftpackmodule file to be linked against those rather than the fftpacklite.c file which is shipped with NumPy.

Go to Main Go to Previous Go to Next