CuPy: NumPy & SciPy for GPU - A NumPy/SciPy-compatible array library for GPU-accelerated computing with Python, specifically built for AMD ROCm 4.3 platform
—
GPU-accelerated Fast Fourier Transform operations supporting 1D, 2D, and N-dimensional transforms for both complex and real data. Built on cuFFT library for high-performance frequency domain computations.
Complex-to-complex Fast Fourier Transforms.
def fft(a, n=None, axis=-1, norm=None):
"""
Compute 1D discrete Fourier transform.
Parameters:
- a: array-like, input array
- n: int, length of transformed axis (zero-padded or truncated if needed)
- axis: int, axis over which to compute FFT
- norm: {None, 'ortho'}, normalization mode
Returns:
cupy.ndarray: Complex-valued FFT result
"""
def ifft(a, n=None, axis=-1, norm=None):
"""Compute 1D inverse discrete Fourier transform."""
def fft2(a, s=None, axes=(-2, -1), norm=None):
"""
Compute 2D discrete Fourier transform.
Parameters:
- a: array-like, input array
- s: tuple of ints, shape of result
- axes: tuple of ints, axes over which to compute FFT
- norm: {None, 'ortho'}, normalization mode
Returns:
cupy.ndarray: 2D FFT result
"""
def ifft2(a, s=None, axes=(-2, -1), norm=None):
"""Compute 2D inverse discrete Fourier transform."""
def fftn(a, s=None, axes=None, norm=None):
"""
Compute N-dimensional discrete Fourier transform.
Parameters:
- a: array-like, input array
- s: sequence of ints, shape of result
- axes: sequence of ints, axes over which to compute FFT
- norm: {None, 'ortho'}, normalization mode
Returns:
cupy.ndarray: N-D FFT result
"""
def ifftn(a, s=None, axes=None, norm=None):
"""Compute N-dimensional inverse discrete Fourier transform."""Optimized transforms for real-valued input data.
def rfft(a, n=None, axis=-1, norm=None):
"""
Compute 1D FFT of real input.
Parameters:
- a: array-like, real input array
- n: int, length of transformed axis
- axis: int, axis over which to compute FFT
- norm: {None, 'ortho'}, normalization mode
Returns:
cupy.ndarray: Complex FFT result (approximately half size)
"""
def irfft(a, n=None, axis=-1, norm=None):
"""
Compute inverse of rfft.
Returns:
cupy.ndarray: Real-valued result
"""
def rfft2(a, s=None, axes=(-2, -1), norm=None):
"""Compute 2D FFT of real input."""
def irfft2(a, s=None, axes=(-2, -1), norm=None):
"""Compute 2D inverse of rfft2."""
def rfftn(a, s=None, axes=None, norm=None):
"""Compute N-D FFT of real input."""
def irfftn(a, s=None, axes=None, norm=None):
"""Compute N-D inverse of rfftn."""Transforms for Hermitian-symmetric input.
def hfft(a, n=None, axis=-1, norm=None):
"""
Compute FFT of signal with Hermitian symmetry.
Parameters:
- a: array-like, Hermitian input array
- n: int, length of transformed axis
- axis: int, axis over which to compute FFT
- norm: {None, 'ortho'}, normalization mode
Returns:
cupy.ndarray: Real-valued FFT result
"""
def ihfft(a, n=None, axis=-1, norm=None):
"""Compute inverse of hfft."""Utility functions 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 (inverse of sampling rate)
Returns:
cupy.ndarray: Sample frequencies
"""
def rfftfreq(n, d=1.0):
"""Return sample frequencies for rfft."""
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."""import cupy as cp
import matplotlib.pyplot as plt
# Create test signal
t = cp.linspace(0, 1, 1000, endpoint=False)
signal = cp.sin(50 * 2 * cp.pi * t) + 0.5 * cp.sin(80 * 2 * cp.pi * t)
# Compute FFT
fft_result = cp.fft.fft(signal)
frequencies = cp.fft.fftfreq(len(signal), 1/1000)
# Get magnitude spectrum
magnitude = cp.abs(fft_result)
# Real FFT for real signals (more efficient)
rfft_result = cp.fft.rfft(signal)
rfrequencies = cp.fft.rfftfreq(len(signal), 1/1000)import cupy as cp
# Create 2D test pattern
x = cp.linspace(-5, 5, 256)
y = cp.linspace(-5, 5, 256)
X, Y = cp.meshgrid(x, y)
image = cp.sin(X) * cp.cos(Y)
# 2D FFT
fft2_result = cp.fft.fft2(image)
fft2_magnitude = cp.abs(fft2_result)
# Shift zero frequency to center
fft2_shifted = cp.fft.fftshift(fft2_result)
# Inverse transform
reconstructed = cp.fft.ifft2(fft2_result).realInstall with Tessl CLI
npx tessl i tessl/pypi-cupy-rocm-4-3