pyFFTW

0.15.1 · active · verified Thu Apr 16

pyFFTW is a high-performance Pythonic wrapper around FFTW 3, the highly optimized Fast Fourier Transform C library. It provides a unified interface for various FFT operations, including interfaces compatible with NumPy's `fft` module, SciPy's `fft` module, and Dask's `fft` module. The current version is 0.15.1, with releases typically occurring a few times a year, including significant updates for Python and dependency compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use pyFFTW's `numpy_fft` interface for a drop-in replacement, and also shows the lower-level `pyfftw.FFTW` class for more granular control over the FFT process. It includes configuration for multithreading and planning effort.

import pyfftw
import numpy as np
import pyfftw.interfaces.numpy_fft as fft
import os

# Configure pyFFTW for optimal performance
pyfftw.config.NUM_THREADS = os.cpu_count() or 1 # Use all available CPU cores
pyfftw.config.PLANNER_EFFORT = 'FFTW_ESTIMATE' # 'FFTW_MEASURE' for better, but slower, planning
pyfftw.config.overwrite_input = True # Allow FFTW to overwrite input array for efficiency

# Create a sample NumPy array
input_data = np.random.randn(128) + 1j*np.random.randn(128)

# Perform FFT using the numpy_fft interface (drop-in replacement)
output_fft = fft.fft(input_data)

# Perform inverse FFT
output_ifft = fft.ifft(output_fft)

print(f"Original data (first 5): {input_data[:5]})")
print(f"FFT output (first 5): {output_fft[:5]})")
print(f"IFFT output (first 5): {output_ifft[:5]})")
print(f"Difference (should be close to zero): {np.max(np.abs(input_data - output_ifft))}")

# Example using the direct FFTW class for more control
# Pre-allocate arrays for in-place transform if desired
a = pyfftw.empty_aligned(128, dtype='complex128')
b = pyfftw.empty_aligned(128, dtype='complex128')

a[:] = input_data # Copy data to aligned array

fft_object = pyfftw.FFTW(a, b, direction='FFTW_FORWARD',
                          flags=('FFTW_MEASURE',), threads=pyfftw.config.NUM_THREADS,
                          planning_effort=pyfftw.config.PLANNER_EFFORT)

fft_object()

print(f"\nDirect FFTW class output (first 5): {b[:5]})")

view raw JSON →