CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyfftw

A pythonic wrapper around FFTW, the FFT library, presenting a unified interface for all the supported transforms.

Pending
Overview
Eval results
Files

scipy-fft-interface.mddocs/

scipy.fft Interface

Compatible interface for scipy.fft functions with FFTW backend, supporting the newer scipy FFT API including workers parameter and context manager compatibility. This interface provides the same functionality as scipy.fft but with FFTW's superior performance.

Capabilities

Complex FFT Functions

Core complex-to-complex transform functions compatible with scipy.fft.

def fft(
    x, 
    n=None, 
    axis=-1, 
    norm=None, 
    overwrite_x=False, 
    workers=None
):
    """
    1D discrete Fourier transform.
    
    Parameters:
    - x: Input array
    - n: Length of transform (default: x.shape[axis])
    - axis: Axis over which to compute FFT (default: -1) 
    - norm: Normalization mode ('backward', 'ortho', 'forward', None)
    - overwrite_x: Whether input can be overwritten (default: False)
    - workers: Number of workers (threads) to use (default: None)
    - plan: Precomputed plan (not used, for compatibility)
    
    Returns:
    - Complex array containing the transform
    """

def ifft(
    x, 
    n=None, 
    axis=-1, 
    norm=None, 
    overwrite_x=False, 
    workers=None
):
    """
    1D inverse discrete Fourier transform.
    
    Parameters: Same as fft()
    
    Returns:
    - Complex array containing the inverse transform
    """

def fft2(
    x, 
    s=None, 
    axes=(-2, -1), 
    norm=None, 
    overwrite_x=False, 
    workers=None
):
    """
    2D discrete Fourier transform.
    
    Parameters:
    - x: Input array
    - s: Shape of transform (default: x.shape[axes])
    - axes: Axes over which to compute FFT (default: (-2, -1))
    - Other parameters: Same as fft()
    
    Returns:
    - Complex array containing the 2D transform
    """

def ifft2(
    x, 
    s=None, 
    axes=(-2, -1), 
    norm=None, 
    overwrite_x=False, 
    workers=None
):
    """
    2D inverse discrete Fourier transform.
    
    Parameters: Same as fft2()
    
    Returns:
    - Complex array containing the 2D inverse transform
    """

def fftn(
    x, 
    s=None, 
    axes=None, 
    norm=None, 
    overwrite_x=False, 
    workers=None
):
    """
    N-dimensional discrete Fourier transform.
    
    Parameters:
    - x: Input array
    - s: Shape of transform (default: x.shape[axes])
    - axes: Axes over which to compute FFT (default: all axes)
    - Other parameters: Same as fft()
    
    Returns:
    - Complex array containing the N-D transform
    """

def ifftn(
    x, 
    s=None, 
    axes=None, 
    norm=None, 
    overwrite_x=False, 
    workers=None
):
    """
    N-dimensional inverse discrete Fourier transform.
    
    Parameters: Same as fftn()
    
    Returns:
    - Complex array containing the N-D inverse transform
    """

Real FFT Functions

Real-to-complex and complex-to-real transform functions.

def rfft(
    x, 
    n=None, 
    axis=-1, 
    norm=None, 
    overwrite_x=False, 
    workers=None
):
    """
    1D real-to-complex discrete Fourier transform.
    
    Parameters: Same as fft()
    
    Returns:
    - Complex array with shape [..., n//2 + 1] along transform axis
    """

def irfft(
    x, 
    n=None, 
    axis=-1, 
    norm=None, 
    overwrite_x=False, 
    workers=None
):
    """
    1D complex-to-real inverse discrete Fourier transform.
    
    Parameters: Same as fft()
    
    Returns:
    - Real array with specified length n along transform axis
    """

def rfft2(
    x, 
    s=None, 
    axes=(-2, -1), 
    norm=None, 
    overwrite_x=False, 
    workers=None
):
    """
    2D real-to-complex discrete Fourier transform.
    
    Parameters: Same as fft2()
    
    Returns:
    - Complex array with last axis shape s[-1]//2 + 1
    """

def irfft2(
    x, 
    s=None, 
    axes=(-2, -1), 
    norm=None, 
    overwrite_x=False, 
    workers=None
):
    """
    2D complex-to-real inverse discrete Fourier transform.
    
    Parameters: Same as fft2()
    
    Returns:
    - Real array with specified shape s
    """

def rfftn(
    x, 
    s=None, 
    axes=None, 
    norm=None, 
    overwrite_x=False, 
    workers=None
):
    """
    N-dimensional real-to-complex discrete Fourier transform.
    
    Parameters: Same as fftn()
    
    Returns:
    - Complex array with last transform axis shape s[-1]//2 + 1
    """

def irfftn(
    x, 
    s=None, 
    axes=None, 
    norm=None, 
    overwrite_x=False, 
    workers=None
):
    """
    N-dimensional complex-to-real inverse discrete Fourier transform.
    
    Parameters: Same as fftn()
    
    Returns:
    - Real array with specified shape s
    """

Hermitian FFT Functions

Functions for transforms of Hermitian (conjugate-symmetric) data.

def hfft(
    x, 
    n=None, 
    axis=-1, 
    norm=None, 
    overwrite_x=False, 
    workers=None
):
    """
    FFT of Hermitian (conjugate-symmetric) sequence.
    
    Parameters: Same as fft()
    
    Returns:
    - Real array containing the transform
    """

def ihfft(
    x, 
    n=None, 
    axis=-1, 
    norm=None, 
    overwrite_x=False, 
    workers=None
):
    """
    Inverse FFT of Hermitian sequence.
    
    Parameters: Same as fft()
    
    Returns:
    - Complex array containing the inverse transform
    """

Discrete Cosine and Sine Transforms

Discrete Cosine Transform (DCT) and Discrete Sine Transform (DST) functions compatible with scipy.fft, supporting multiple transform types and normalization modes.

def dct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, planner_effort=None, auto_align_input=True, auto_contiguous=True):
    """
    Perform a 1D discrete cosine transform.

    Parameters:
    - x: array_like, input array
    - type: int, optional, DCT type (1, 2, 3, or 4), default is 2
    - n: int, optional, length of the transform
    - axis: int, optional, axis over which to compute the DCT
    - norm: str, optional, normalization mode ('backward', 'ortho', 'forward', None)
    - overwrite_x: bool, optional, whether input can be overwritten
    - workers: int, optional, number of workers (threads) to use
    - planner_effort: str, optional, FFTW planner effort
    - auto_align_input: bool, optional, automatically align input
    - auto_contiguous: bool, optional, ensure input is contiguous

    Returns:
    ndarray: DCT result
    """

def idct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, planner_effort=None, auto_align_input=True, auto_contiguous=True):
    """
    Perform an inverse 1D discrete cosine transform.

    Parameters:
    - x: array_like, input array
    - type: int, optional, DCT type (1, 2, 3, or 4), default is 2
    - n: int, optional, length of the inverse transform
    - axis: int, optional, axis over which to compute the inverse DCT
    - norm: str, optional, normalization mode ('backward', 'ortho', 'forward', None)
    - overwrite_x: bool, optional, whether input can be overwritten
    - workers: int, optional, number of workers (threads) to use
    - planner_effort: str, optional, FFTW planner effort
    - auto_align_input: bool, optional, automatically align input
    - auto_contiguous: bool, optional, ensure input is contiguous

    Returns:
    ndarray: Inverse DCT result
    """

def dst(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, planner_effort=None, auto_align_input=True, auto_contiguous=True):
    """
    Perform a 1D discrete sine transform.

    Parameters:
    - x: array_like, input array
    - type: int, optional, DST type (1, 2, 3, or 4), default is 2
    - n: int, optional, length of the transform
    - axis: int, optional, axis over which to compute the DST
    - norm: str, optional, normalization mode ('backward', 'ortho', 'forward', None)
    - overwrite_x: bool, optional, whether input can be overwritten
    - workers: int, optional, number of workers (threads) to use
    - planner_effort: str, optional, FFTW planner effort
    - auto_align_input: bool, optional, automatically align input
    - auto_contiguous: bool, optional, ensure input is contiguous

    Returns:
    ndarray: DST result
    """

def idst(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, planner_effort=None, auto_align_input=True, auto_contiguous=True):
    """
    Perform an inverse 1D discrete sine transform.

    Parameters:
    - x: array_like, input array
    - type: int, optional, DST type (1, 2, 3, or 4), default is 2
    - n: int, optional, length of the inverse transform
    - axis: int, optional, axis over which to compute the inverse DST
    - norm: str, optional, normalization mode ('backward', 'ortho', 'forward', None)
    - overwrite_x: bool, optional, whether input can be overwritten
    - workers: int, optional, number of workers (threads) to use
    - planner_effort: str, optional, FFTW planner effort
    - auto_align_input: bool, optional, automatically align input
    - auto_contiguous: bool, optional, ensure input is contiguous

    Returns:
    ndarray: Inverse DST result
    """

def dctn(x, type=2, s=None, axes=None, norm=None, overwrite_x=False, workers=None, planner_effort=None, auto_align_input=True, auto_contiguous=True):
    """
    Perform an N-dimensional discrete cosine transform.

    Parameters:
    - x: array_like, input array
    - type: int, optional, DCT type (1, 2, 3, or 4), default is 2
    - s: sequence of ints, optional, shape of the result
    - axes: sequence of ints, optional, axes over which to compute the DCT
    - norm: str, optional, normalization mode ('backward', 'ortho', 'forward', None)
    - overwrite_x: bool, optional, whether input can be overwritten
    - workers: int, optional, number of workers (threads) to use
    - planner_effort: str, optional, FFTW planner effort
    - auto_align_input: bool, optional, automatically align input
    - auto_contiguous: bool, optional, ensure input is contiguous

    Returns:
    ndarray: N-dimensional DCT result
    """

def idctn(x, type=2, s=None, axes=None, norm=None, overwrite_x=False, workers=None, planner_effort=None, auto_align_input=True, auto_contiguous=True):
    """
    Perform an N-dimensional inverse discrete cosine transform.

    Parameters:
    - x: array_like, input array
    - type: int, optional, DCT type (1, 2, 3, or 4), default is 2
    - s: sequence of ints, optional, shape of the result
    - axes: sequence of ints, optional, axes over which to compute the inverse DCT
    - norm: str, optional, normalization mode ('backward', 'ortho', 'forward', None)
    - overwrite_x: bool, optional, whether input can be overwritten
    - workers: int, optional, number of workers (threads) to use
    - planner_effort: str, optional, FFTW planner effort
    - auto_align_input: bool, optional, automatically align input
    - auto_contiguous: bool, optional, ensure input is contiguous

    Returns:
    ndarray: N-dimensional inverse DCT result
    """

def dstn(x, type=2, s=None, axes=None, norm=None, overwrite_x=False, workers=None, planner_effort=None, auto_align_input=True, auto_contiguous=True):
    """
    Perform an N-dimensional discrete sine transform.

    Parameters:
    - x: array_like, input array
    - type: int, optional, DST type (1, 2, 3, or 4), default is 2
    - s: sequence of ints, optional, shape of the result
    - axes: sequence of ints, optional, axes over which to compute the DST
    - norm: str, optional, normalization mode ('backward', 'ortho', 'forward', None)
    - overwrite_x: bool, optional, whether input can be overwritten
    - workers: int, optional, number of workers (threads) to use
    - planner_effort: str, optional, FFTW planner effort
    - auto_align_input: bool, optional, automatically align input
    - auto_contiguous: bool, optional, ensure input is contiguous

    Returns:
    ndarray: N-dimensional DST result
    """

def idstn(x, type=2, s=None, axes=None, norm=None, overwrite_x=False, workers=None, planner_effort=None, auto_align_input=True, auto_contiguous=True):
    """
    Perform an N-dimensional inverse discrete sine transform.

    Parameters:
    - x: array_like, input array
    - type: int, optional, DST type (1, 2, 3, or 4), default is 2
    - s: sequence of ints, optional, shape of the result  
    - axes: sequence of ints, optional, axes over which to compute the inverse DST
    - norm: str, optional, normalization mode ('backward', 'ortho', 'forward', None)
    - overwrite_x: bool, optional, whether input can be overwritten
    - workers: int, optional, number of workers (threads) to use
    - planner_effort: str, optional, FFTW planner effort
    - auto_align_input: bool, optional, automatically align input
    - auto_contiguous: bool, optional, ensure input is contiguous

    Returns:
    ndarray: N-dimensional inverse DST result
    """

Utility Functions

Additional utility functions compatible with scipy.fft.

def next_fast_len(target, real=False):
    """
    Find the optimal length for FFT transforms.
    
    Parameters:
    - target: Target transform length
    - real: Whether this is for a real transform (default: False)
    
    Returns:
    - int: Optimal length >= target that gives efficient transforms
    """

Usage Examples

Drop-in scipy.fft Replacement

# Original scipy.fft code
from scipy import fft
import numpy as np

data = np.random.randn(1024) + 1j * np.random.randn(1024)
result_scipy = fft.fft(data)

# Replace with pyFFTW scipy interface
from pyfftw.interfaces import scipy_fft

result_pyfftw = scipy_fft.fft(data)
print(f"Results match: {np.allclose(result_scipy, result_pyfftw)}")

Workers Parameter

from pyfftw.interfaces import scipy_fft
import numpy as np

# Use multiple workers (threads)
data = np.random.randn(1024, 512) + 1j * np.random.randn(1024, 512)

result = scipy_fft.fft2(data, workers=4)  # Use 4 threads

Context Manager Compatibility

from pyfftw.interfaces import scipy_fft
from scipy.fft import set_workers
import numpy as np

# Works with scipy's context manager
data = np.random.randn(2048, 1024)

with set_workers(4):
    # All transforms in this block use 4 workers
    spectrum = scipy_fft.rfft2(data)
    filtered = scipy_fft.irfft2(spectrum * 0.5)

Optimal Transform Sizes

from pyfftw.interfaces import scipy_fft
import numpy as np

# Find optimal sizes for efficient transforms
target_size = 1000
optimal_size = scipy_fft.next_fast_len(target_size)
optimal_real_size = scipy_fft.next_fast_len(target_size, real=True)

print(f"Target: {target_size}")
print(f"Optimal complex: {optimal_size}")
print(f"Optimal real: {optimal_real_size}")

# Use optimal size for better performance
data = np.random.randn(optimal_real_size)
result = scipy_fft.rfft(data, workers=2)

Real Signal Processing

from pyfftw.interfaces import scipy_fft
import numpy as np

# Generate real signal
t = np.linspace(0, 1, 1024, endpoint=False)
signal = np.sin(2 * np.pi * 50 * t) + 0.5 * np.sin(2 * np.pi * 120 * t)
noise = 0.2 * np.random.randn(len(t))
noisy_signal = signal + noise

# Analyze in frequency domain
spectrum = scipy_fft.rfft(noisy_signal, workers=2)
freqs = scipy_fft.rfftfreq(len(noisy_signal), t[1] - t[0])

# Apply frequency domain filter
spectrum[np.abs(freqs) > 100] = 0  # Low-pass filter at 100 Hz

# Transform back
filtered_signal = scipy_fft.irfft(spectrum, n=len(noisy_signal))

3D Volume Processing

from pyfftw.interfaces import scipy_fft
import numpy as np

# 3D medical/scientific volume data
volume = np.random.randn(128, 128, 64)

# 3D real FFT with multiple workers
spectrum_3d = scipy_fft.rfftn(
    volume, 
    workers=4,
    norm='ortho'  # Preserve energy
)

# Process in frequency domain
# (e.g., apply 3D filter, etc.)

# Transform back
processed_volume = scipy_fft.irfftn(
    spectrum_3d, 
    s=volume.shape,
    workers=4,
    norm='ortho'
)

Normalization Modes

from pyfftw.interfaces import scipy_fft
import numpy as np

data = np.random.randn(256) + 1j * np.random.randn(256)

# Different normalization modes (same as scipy.fft)
forward_norm = scipy_fft.fft(data, norm='forward')
backward_norm = scipy_fft.fft(data, norm='backward')  # Default
ortho_norm = scipy_fft.fft(data, norm='ortho')

# Verify round-trip with orthogonal normalization
reconstructed = scipy_fft.ifft(ortho_norm, norm='ortho')
print(f"Reconstruction error: {np.max(np.abs(data - reconstructed))}")

Performance Comparison

from pyfftw.interfaces import scipy_fft
from scipy import fft as scipy_fft_native
import numpy as np
import time

# Compare performance with native scipy
N = 1024 * 1024
data = np.random.randn(N) + 1j * np.random.randn(N)

# Time scipy native
start = time.time()
for _ in range(10):
    result_scipy = scipy_fft_native.fft(data)
time_scipy = time.time() - start

# Time pyFFTW scipy interface
start = time.time()
for _ in range(10):
    result_pyfftw = scipy_fft.fft(data, workers=4)
time_pyfftw = time.time() - start

print(f"SciPy native: {time_scipy:.3f}s")
print(f"pyFFTW: {time_pyfftw:.3f}s")
print(f"Speedup: {time_scipy/time_pyfftw:.2f}x")
print(f"Results match: {np.allclose(result_scipy, result_pyfftw)}")

Install with Tessl CLI

npx tessl i tessl/pypi-pyfftw

docs

configuration-utilities.md

core-fftw.md

fft-builders.md

index.md

interfaces-cache.md

memory-management.md

numpy-fft-interface.md

scipy-fft-interface.md

scipy-fftpack-interface.md

wisdom-management.md

tile.json