A pythonic wrapper around FFTW, the FFT library, presenting a unified interface for all the supported transforms.
—
Drop-in replacement for scipy.fftpack functions with FFTW backend. This interface provides compatibility with the legacy scipy.fftpack module while offering FFTW's superior performance. Note that scipy.fftpack is considered legacy - new code should use scipy.fft instead.
Warning: scipy.fftpack is deprecated in favor of scipy.fft. This interface is provided for backward compatibility with existing code.
from pyfftw.interfaces import scipy_fftpackIndividual function imports:
from pyfftw.interfaces.scipy_fftpack import fft, ifft, dct, dstfrom pyfftw.interfaces import scipy_fftpack
import numpy as np
# Create sample data
x = np.random.randn(128) + 1j * np.random.randn(128)
# FFT using scipy.fftpack interface with FFTW backend
y = scipy_fftpack.fft(x)
# Discrete Cosine Transform
real_data = np.random.randn(128)
dct_result = scipy_fftpack.dct(real_data, type=2, norm='ortho')
# Use additional FFTW parameters for optimization
fft_result = scipy_fftpack.fft(x, overwrite_x=True,
planner_effort='FFTW_MEASURE',
threads=4)Standard complex FFT transforms compatible with scipy.fftpack interface.
def fft(x, n=None, axis=-1, overwrite_x=False, planner_effort=None, threads=None, auto_align_input=True, auto_contiguous=True):
"""
Perform an 1D FFT.
Parameters:
- x: array_like, input array
- n: int, optional, length of the transform
- axis: int, optional, axis over which to compute the FFT
- overwrite_x: bool, optional, whether input can be overwritten
- planner_effort: str, optional, FFTW planner effort ('FFTW_ESTIMATE', 'FFTW_MEASURE', 'FFTW_PATIENT', 'FFTW_EXHAUSTIVE')
- threads: int, optional, number of threads to use
- auto_align_input: bool, optional, automatically align input for optimal performance
- auto_contiguous: bool, optional, ensure input is contiguous
Returns:
ndarray: Complex FFT result
"""
def ifft(x, n=None, axis=-1, overwrite_x=False, planner_effort=None, threads=None, auto_align_input=True, auto_contiguous=True):
"""
Perform an 1D inverse FFT.
Parameters:
- x: array_like, input array
- n: int, optional, length of the inverse transform
- axis: int, optional, axis over which to compute the inverse FFT
- overwrite_x: bool, optional, whether input can be overwritten
- planner_effort: str, optional, FFTW planner effort
- threads: int, optional, number of threads to use
- auto_align_input: bool, optional, automatically align input
- auto_contiguous: bool, optional, ensure input is contiguous
Returns:
ndarray: Complex inverse FFT result
"""
def fft2(x, shape=None, axes=(-2, -1), overwrite_x=False, planner_effort=None, threads=None, auto_align_input=True, auto_contiguous=True):
"""
Perform a 2D FFT.
Parameters:
- x: array_like, input array
- shape: sequence of ints, optional, shape of the result
- axes: sequence of ints, optional, axes over which to compute the FFT
- overwrite_x: bool, optional, whether input can be overwritten
- planner_effort: str, optional, FFTW planner effort
- threads: int, optional, number of threads to use
- auto_align_input: bool, optional, automatically align input
- auto_contiguous: bool, optional, ensure input is contiguous
Returns:
ndarray: Complex 2D FFT result
"""
def ifft2(x, shape=None, axes=(-2, -1), overwrite_x=False, planner_effort=None, threads=None, auto_align_input=True, auto_contiguous=True):
"""
Perform a 2D inverse FFT.
Parameters:
- x: array_like, input array
- shape: sequence of ints, optional, shape of the result
- axes: sequence of ints, optional, axes over which to compute the inverse FFT
- overwrite_x: bool, optional, whether input can be overwritten
- planner_effort: str, optional, FFTW planner effort
- threads: int, optional, number of threads to use
- auto_align_input: bool, optional, automatically align input
- auto_contiguous: bool, optional, ensure input is contiguous
Returns:
ndarray: Complex 2D inverse FFT result
"""
def fftn(x, shape=None, axes=None, overwrite_x=False, planner_effort=None, threads=None, auto_align_input=True, auto_contiguous=True):
"""
Perform an N-dimensional FFT.
Parameters:
- x: array_like, input array
- shape: sequence of ints, optional, shape of the result
- axes: sequence of ints, optional, axes over which to compute the FFT
- overwrite_x: bool, optional, whether input can be overwritten
- planner_effort: str, optional, FFTW planner effort
- threads: int, optional, number of threads to use
- auto_align_input: bool, optional, automatically align input
- auto_contiguous: bool, optional, ensure input is contiguous
Returns:
ndarray: Complex N-dimensional FFT result
"""
def ifftn(x, shape=None, axes=None, overwrite_x=False, planner_effort=None, threads=None, auto_align_input=True, auto_contiguous=True):
"""
Perform an N-dimensional inverse FFT.
Parameters:
- x: array_like, input array
- shape: sequence of ints, optional, shape of the result
- axes: sequence of ints, optional, axes over which to compute the inverse FFT
- overwrite_x: bool, optional, whether input can be overwritten
- planner_effort: str, optional, FFTW planner effort
- threads: int, optional, number of threads to use
- auto_align_input: bool, optional, automatically align input
- auto_contiguous: bool, optional, ensure input is contiguous
Returns:
ndarray: Complex N-dimensional inverse FFT result
"""Real-to-complex and complex-to-real FFT transforms.
def rfft(x, n=None, axis=-1, overwrite_x=False, planner_effort=None, threads=None, auto_align_input=True, auto_contiguous=True):
"""
Perform a real-input FFT.
Parameters:
- x: array_like, real input array
- n: int, optional, length of the transform
- axis: int, optional, axis over which to compute the FFT
- overwrite_x: bool, optional, whether input can be overwritten
- planner_effort: str, optional, FFTW planner effort
- threads: int, optional, number of threads to use
- auto_align_input: bool, optional, automatically align input
- auto_contiguous: bool, optional, ensure input is contiguous
Returns:
ndarray: Complex FFT result with conjugate symmetry
"""
def irfft(x, n=None, axis=-1, overwrite_x=False, planner_effort=None, threads=None, auto_align_input=True, auto_contiguous=True):
"""
Perform an inverse real-input FFT.
Parameters:
- x: array_like, complex input array with conjugate symmetry
- n: int, optional, length of the inverse transform
- axis: int, optional, axis over which to compute the inverse FFT
- overwrite_x: bool, optional, whether input can be overwritten
- planner_effort: str, optional, FFTW planner effort
- threads: int, optional, number of threads to use
- auto_align_input: bool, optional, automatically align input
- auto_contiguous: bool, optional, ensure input is contiguous
Returns:
ndarray: Real inverse FFT result
"""Discrete Cosine Transform functions with multiple types and normalization options.
def dct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False, planner_effort=None, threads=None, auto_align_input=True, auto_contiguous=True):
"""
Perform a 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 (None or 'ortho')
- overwrite_x: bool, optional, whether input can be overwritten
- planner_effort: str, optional, FFTW planner effort
- threads: int, optional, number of threads to use
- 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, planner_effort=None, threads=None, auto_align_input=True, auto_contiguous=True):
"""
Perform an inverse 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 (None or 'ortho')
- overwrite_x: bool, optional, whether input can be overwritten
- planner_effort: str, optional, FFTW planner effort
- threads: int, optional, number of threads to use
- auto_align_input: bool, optional, automatically align input
- auto_contiguous: bool, optional, ensure input is contiguous
Returns:
ndarray: Inverse DCT result
"""
def dctn(x, type=2, shape=None, axes=None, norm=None, overwrite_x=False, planner_effort=None, threads=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
- shape: 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 (None or 'ortho')
- overwrite_x: bool, optional, whether input can be overwritten
- planner_effort: str, optional, FFTW planner effort
- threads: int, optional, number of threads to use
- 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, shape=None, axes=None, norm=None, overwrite_x=False, planner_effort=None, threads=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
- shape: 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 (None or 'ortho')
- overwrite_x: bool, optional, whether input can be overwritten
- planner_effort: str, optional, FFTW planner effort
- threads: int, optional, number of threads to use
- auto_align_input: bool, optional, automatically align input
- auto_contiguous: bool, optional, ensure input is contiguous
Returns:
ndarray: N-dimensional inverse DCT result
"""Discrete Sine Transform functions with multiple types and normalization options.
def dst(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False, planner_effort=None, threads=None, auto_align_input=True, auto_contiguous=True):
"""
Perform a 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 (None or 'ortho')
- overwrite_x: bool, optional, whether input can be overwritten
- planner_effort: str, optional, FFTW planner effort
- threads: int, optional, number of threads to use
- 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, planner_effort=None, threads=None, auto_align_input=True, auto_contiguous=True):
"""
Perform an inverse 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 (None or 'ortho')
- overwrite_x: bool, optional, whether input can be overwritten
- planner_effort: str, optional, FFTW planner effort
- threads: int, optional, number of threads to use
- auto_align_input: bool, optional, automatically align input
- auto_contiguous: bool, optional, ensure input is contiguous
Returns:
ndarray: Inverse DST result
"""
def dstn(x, type=2, shape=None, axes=None, norm=None, overwrite_x=False, planner_effort=None, threads=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
- shape: 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 (None or 'ortho')
- overwrite_x: bool, optional, whether input can be overwritten
- planner_effort: str, optional, FFTW planner effort
- threads: int, optional, number of threads to use
- 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, shape=None, axes=None, norm=None, overwrite_x=False, planner_effort=None, threads=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
- shape: 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 (None or 'ortho')
- overwrite_x: bool, optional, whether input can be overwritten
- planner_effort: str, optional, FFTW planner effort
- threads: int, optional, number of threads to use
- auto_align_input: bool, optional, automatically align input
- auto_contiguous: bool, optional, ensure input is contiguous
Returns:
ndarray: N-dimensional inverse DST result
"""Additional utility functions provided by the interface.
def next_fast_len(target):
"""
Find the next fast size of input data to fft, for zero-padding, etc.
Parameters:
- target: int, target transform length
Returns:
int: Next fast length >= target
"""# Original scipy.fftpack code
import scipy.fftpack as fftpack
result = fftpack.fft(data)
# Drop-in replacement with FFTW backend
from pyfftw.interfaces import scipy_fftpack as fftpack
result = fftpack.fft(data) # Now uses FFTW backendfrom pyfftw.interfaces import scipy_fftpack
import numpy as np
# Image compression example with DCT
image = np.random.randn(64, 64)
dct_coeffs = scipy_fftpack.dctn(image, type=2, norm='ortho')
reconstructed = scipy_fftpack.idctn(dct_coeffs, type=2, norm='ortho')
# Signal processing with DST
signal = np.random.randn(256)
dst_coeffs = scipy_fftpack.dst(signal, type=1)
original = scipy_fftpack.idst(dst_coeffs, type=1)# Use FFTW-specific parameters for better performance
result = scipy_fftpack.fft(data,
overwrite_x=True, # Allow input modification
planner_effort='FFTW_PATIENT', # Better planning
threads=8, # Multi-threading
auto_align_input=True) # Optimal alignmentInstall with Tessl CLI
npx tessl i tessl/pypi-pyfftw