NumPy & SciPy-compatible array library for GPU-accelerated computing with Python
—
GPU-accelerated discrete Fourier transforms for signal processing and frequency domain analysis. CuPy provides a complete FFT interface compatible with NumPy and SciPy, optimized for GPU execution using cuFFT.
One-dimensional and multi-dimensional complex-to-complex transforms.
def fft(a, n=None, axis=-1, norm=None):
"""
Compute 1-D discrete Fourier Transform.
Parameters:
- a: array-like, input array
- n: int, length of transformed axis
- axis: int, axis over which to compute FFT
- norm: {'backward', 'ortho', 'forward'}, normalization mode
Returns:
cupy.ndarray: Complex FFT result
"""
def ifft(a, n=None, axis=-1, norm=None):
"""Compute 1-D inverse discrete Fourier Transform."""
def fft2(a, s=None, axes=(-2, -1), norm=None):
"""
Compute 2-D discrete Fourier Transform.
Parameters:
- a: array-like, input array
- s: sequence of ints, shape of output
- axes: sequence of ints, axes over which to compute FFT
- norm: {'backward', 'ortho', 'forward'}, normalization mode
Returns:
cupy.ndarray: 2-D FFT result
"""
def ifft2(a, s=None, axes=(-2, -1), norm=None):
"""Compute 2-D inverse discrete Fourier Transform."""
def fftn(a, s=None, axes=None, norm=None):
"""
Compute N-D discrete Fourier Transform.
Parameters:
- a: array-like, input array
- s: sequence of ints, shape of output
- axes: sequence of ints, axes over which to compute FFT
- norm: {'backward', 'ortho', 'forward'}, normalization mode
Returns:
cupy.ndarray: N-D FFT result
"""
def ifftn(a, s=None, axes=None, norm=None):
"""Compute N-D inverse discrete Fourier Transform."""Optimized transforms for real-valued input data.
def rfft(a, n=None, axis=-1, norm=None):
"""
Compute 1-D real-input discrete Fourier Transform.
Parameters:
- a: array-like, real input array
- n: int, length of transformed axis
- axis: int, axis over which to compute FFT
- norm: {'backward', 'ortho', 'forward'}, normalization mode
Returns:
cupy.ndarray: Complex FFT result (positive frequencies only)
"""
def irfft(a, n=None, axis=-1, norm=None):
"""
Compute 1-D inverse real-input discrete Fourier Transform.
Parameters:
- a: array-like, complex input array
- n: int, length of output along transformed axis
- axis: int, axis over which to compute IFFT
- norm: {'backward', 'ortho', 'forward'}, normalization mode
Returns:
cupy.ndarray: Real IFFT result
"""
def rfft2(a, s=None, axes=(-2, -1), norm=None):
"""Compute 2-D real-input discrete Fourier Transform."""
def irfft2(a, s=None, axes=(-2, -1), norm=None):
"""Compute 2-D inverse real-input discrete Fourier Transform."""
def rfftn(a, s=None, axes=None, norm=None):
"""Compute N-D real-input discrete Fourier Transform."""
def irfftn(a, s=None, axes=None, norm=None):
"""Compute N-D inverse real-input discrete Fourier Transform."""Transforms for Hermitian symmetric input.
def hfft(a, n=None, axis=-1, norm=None):
"""
Compute 1-D Hermitian-input discrete Fourier Transform.
Parameters:
- a: array-like, Hermitian input array
- n: int, length of output
- axis: int, axis over which to compute FFT
- norm: {'backward', 'ortho', 'forward'}, normalization mode
Returns:
cupy.ndarray: Real FFT result
"""
def ihfft(a, n=None, axis=-1, norm=None):
"""Compute 1-D inverse Hermitian-input discrete Fourier Transform."""Utilities for frequency analysis and array manipulation.
def fftfreq(n, d=1.0):
"""
Return discrete Fourier Transform sample frequencies.
Parameters:
- n: int, window length
- d: scalar, sample spacing
Returns:
cupy.ndarray: Sample frequencies
"""
def rfftfreq(n, d=1.0):
"""
Return sample frequencies for real-input FFT.
Parameters:
- n: int, window length
- d: scalar, sample spacing
Returns:
cupy.ndarray: Sample frequencies (positive only)
"""
def fftshift(x, axes=None):
"""
Shift zero-frequency component to center of spectrum.
Parameters:
- x: array-like, input array
- axes: int or shape tuple, axes over which to shift
Returns:
cupy.ndarray: Shifted array
"""
def ifftshift(x, axes=None):
"""
Inverse of fftshift.
Parameters:
- x: array-like, input array
- axes: int or shape tuple, axes over which to shift
Returns:
cupy.ndarray: Shifted array
"""import cupy as cp
# 1D signal processing
N = 1024
t = cp.linspace(0, 1, N, endpoint=False)
signal = cp.sin(2 * cp.pi * 50 * t) + cp.sin(2 * cp.pi * 120 * t)
# Forward FFT
fft_result = cp.fft.fft(signal)
frequencies = cp.fft.fftfreq(N, d=1/N)
# Find dominant frequencies
magnitude = cp.abs(fft_result)
dominant_freq_idx = cp.argmax(magnitude[:N//2])
dominant_freq = frequencies[dominant_freq_idx]
# Inverse FFT
reconstructed = cp.fft.ifft(fft_result)
assert cp.allclose(signal, reconstructed.real)# 2D FFT for image processing
image = cp.random.random((512, 512))
# 2D FFT
fft_image = cp.fft.fft2(image)
fft_shifted = cp.fft.fftshift(fft_image)
# Apply frequency domain filter (low-pass)
rows, cols = image.shape
crow, ccol = rows // 2, cols // 2
mask = cp.zeros((rows, cols), dtype=bool)
r = 50 # Filter radius
y, x = cp.ogrid[:rows, :cols]
mask_area = (x - ccol)**2 + (y - crow)**2 <= r*r
mask[mask_area] = True
# Apply mask and inverse transform
fft_filtered = fft_shifted * mask
filtered_image = cp.fft.ifft2(cp.fft.ifftshift(fft_filtered))
filtered_image = cp.real(filtered_image)# Efficient FFT for real signals
real_signal = cp.random.random(1000)
# Real FFT (more efficient for real input)
rfft_result = cp.fft.rfft(real_signal)
# Result has shape (501,) instead of (1000,)
# Frequency analysis
freqs = cp.fft.rfftfreq(len(real_signal))
power_spectrum = cp.abs(rfft_result)**2
# Inverse transform
reconstructed = cp.fft.irfft(rfft_result)
assert cp.allclose(real_signal, reconstructed)Install with Tessl CLI
npx tessl i tessl/pypi-cupy