CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-cupy-cuda101

CuPy: NumPy & SciPy for GPU (CUDA 10.1 version)

Pending
Overview
Eval results
Files

fft.mddocs/

Fast Fourier Transform

GPU-accelerated FFT operations for 1D, 2D, and N-dimensional transforms. CuPy's FFT module provides comprehensive frequency domain processing capabilities powered by cuFFT library with NumPy-compatible interfaces.

Capabilities

One-Dimensional FFT

Standard 1D Fourier transforms for signal processing and frequency analysis.

def fft(a, n=None, axis=-1, norm=None):
    """
    One-dimensional discrete Fourier transform.
    
    Parameters:
    - a: array-like, input array
    - n: int, length of transform, optional
    - axis: int, axis for FFT, default -1
    - norm: str, normalization mode, optional
    
    Returns:
    cupy.ndarray: Complex FFT result on GPU
    """

def ifft(a, n=None, axis=-1, norm=None):
    """
    One-dimensional inverse discrete Fourier transform.
    
    Parameters:
    - a: array-like, input array
    - n: int, length of transform, optional
    - axis: int, axis for IFFT, default -1
    - norm: str, normalization mode, optional
    
    Returns:
    cupy.ndarray: Complex IFFT result on GPU
    """

def rfft(a, n=None, axis=-1, norm=None):
    """
    One-dimensional real discrete Fourier transform.
    
    Parameters:
    - a: array-like, real input array
    - n: int, length of transform, optional
    - axis: int, axis for FFT, default -1
    - norm: str, normalization mode, optional
    
    Returns:
    cupy.ndarray: Complex FFT result for real input on GPU
    """

def irfft(a, n=None, axis=-1, norm=None):
    """
    One-dimensional inverse real discrete Fourier transform.
    
    Parameters:
    - a: array-like, complex input array
    - n: int, length of output, optional
    - axis: int, axis for IFFT, default -1
    - norm: str, normalization mode, optional
    
    Returns:
    cupy.ndarray: Real IFFT result on GPU
    """

def hfft(a, n=None, axis=-1, norm=None):
    """
    Hermitian discrete Fourier transform.
    
    Parameters:
    - a: array-like, Hermitian input array
    - n: int, length of transform, optional
    - axis: int, axis for FFT, default -1
    - norm: str, normalization mode, optional
    
    Returns:
    cupy.ndarray: Real FFT result for Hermitian input on GPU
    """

def ihfft(a, n=None, axis=-1, norm=None):
    """
    Inverse Hermitian discrete Fourier transform.
    
    Parameters:
    - a: array-like, real input array
    - n: int, length of output, optional
    - axis: int, axis for IFFT, default -1
    - norm: str, normalization mode, optional
    
    Returns:
    cupy.ndarray: Complex Hermitian IFFT result on GPU
    """

Two-Dimensional FFT

2D Fourier transforms for image processing and 2D signal analysis.

def fft2(a, s=None, axes=(-2, -1), norm=None):
    """
    Two-dimensional discrete Fourier transform.
    
    Parameters:
    - a: array-like, input array
    - s: tuple of ints, shape of transform, optional
    - axes: tuple of ints, axes for 2D FFT, default (-2, -1)
    - norm: str, normalization mode, optional
    
    Returns:
    cupy.ndarray: Complex 2D FFT result on GPU
    """

def ifft2(a, s=None, axes=(-2, -1), norm=None):
    """
    Two-dimensional inverse discrete Fourier transform.
    
    Parameters:
    - a: array-like, input array
    - s: tuple of ints, shape of transform, optional
    - axes: tuple of ints, axes for 2D IFFT, default (-2, -1)
    - norm: str, normalization mode, optional
    
    Returns:
    cupy.ndarray: Complex 2D IFFT result on GPU
    """

def rfft2(a, s=None, axes=(-2, -1), norm=None):
    """
    Two-dimensional real discrete Fourier transform.
    
    Parameters:
    - a: array-like, real input array
    - s: tuple of ints, shape of transform, optional
    - axes: tuple of ints, axes for 2D FFT, default (-2, -1)
    - norm: str, normalization mode, optional
    
    Returns:
    cupy.ndarray: Complex 2D FFT result for real input on GPU
    """

def irfft2(a, s=None, axes=(-2, -1), norm=None):
    """
    Two-dimensional inverse real discrete Fourier transform.
    
    Parameters:
    - a: array-like, complex input array
    - s: tuple of ints, shape of output, optional
    - axes: tuple of ints, axes for 2D IFFT, default (-2, -1)
    - norm: str, normalization mode, optional
    
    Returns:
    cupy.ndarray: Real 2D IFFT result on GPU
    """

N-Dimensional FFT

Multi-dimensional Fourier transforms for complex data analysis.

def fftn(a, s=None, axes=None, norm=None):
    """
    N-dimensional discrete Fourier transform.
    
    Parameters:
    - a: array-like, input array
    - s: tuple of ints, shape of transform, optional
    - axes: tuple of ints, axes for N-D FFT, optional
    - norm: str, normalization mode, optional
    
    Returns:
    cupy.ndarray: Complex N-D FFT result on GPU
    """

def ifftn(a, s=None, axes=None, norm=None):
    """
    N-dimensional inverse discrete Fourier transform.
    
    Parameters:
    - a: array-like, input array
    - s: tuple of ints, shape of transform, optional
    - axes: tuple of ints, axes for N-D IFFT, optional
    - norm: str, normalization mode, optional
    
    Returns:
    cupy.ndarray: Complex N-D IFFT result on GPU
    """

def rfftn(a, s=None, axes=None, norm=None):
    """
    N-dimensional real discrete Fourier transform.
    
    Parameters:
    - a: array-like, real input array
    - s: tuple of ints, shape of transform, optional
    - axes: tuple of ints, axes for N-D FFT, optional
    - norm: str, normalization mode, optional
    
    Returns:
    cupy.ndarray: Complex N-D FFT result for real input on GPU
    """

def irfftn(a, s=None, axes=None, norm=None):
    """
    N-dimensional inverse real discrete Fourier transform.
    
    Parameters:
    - a: array-like, complex input array
    - s: tuple of ints, shape of output, optional
    - axes: tuple of ints, axes for N-D IFFT, optional
    - norm: str, normalization mode, optional
    
    Returns:
    cupy.ndarray: Real N-D IFFT result on GPU
    """

Frequency Sampling

Functions for generating frequency arrays and shifting frequency components.

def fftfreq(n, d=1.0):
    """
    Discrete Fourier transform sample frequencies.
    
    Parameters:
    - n: int, window length
    - d: float, sample spacing, default 1.0
    
    Returns:
    cupy.ndarray: Frequency array on GPU
    """

def rfftfreq(n, d=1.0):
    """
    Real discrete Fourier transform sample frequencies.
    
    Parameters:
    - n: int, window length
    - d: float, sample spacing, default 1.0
    
    Returns:
    cupy.ndarray: Frequency array for real FFT on GPU
    """

def fftshift(x, axes=None):
    """
    Shift zero-frequency component to center.
    
    Parameters:
    - x: array-like, input array
    - axes: int or tuple, axes to shift, optional
    
    Returns:
    cupy.ndarray: Shifted array on GPU
    """

def ifftshift(x, axes=None):
    """
    Inverse of fftshift.
    
    Parameters:
    - x: array-like, input array
    - axes: int or tuple, axes to shift, optional
    
    Returns:
    cupy.ndarray: Inverse shifted array on GPU
    """

Usage Examples

Basic 1D FFT Operations

import cupy as cp
import numpy as np

# Create sample signal
t = cp.linspace(0, 1, 1000, endpoint=False)
signal = cp.sin(2 * cp.pi * 50 * t) + cp.sin(2 * cp.pi * 120 * t)
signal += 0.5 * cp.random.random(1000)  # Add noise

# Forward FFT
fft_result = cp.fft.fft(signal)
frequencies = cp.fft.fftfreq(len(signal), 1/1000)  # Sample rate 1000 Hz

# Magnitude spectrum
magnitude = cp.abs(fft_result)
power_spectrum = magnitude**2

# Inverse FFT to reconstruct signal
reconstructed = cp.fft.ifft(fft_result)
print(cp.allclose(signal, reconstructed.real))  # Should be True

2D FFT for Image Processing

# Create 2D image-like data
image = cp.random.random((256, 256)).astype(cp.float32)

# Add some structure (sine wave pattern)
x, y = cp.meshgrid(cp.linspace(0, 10, 256), cp.linspace(0, 10, 256))
pattern = cp.sin(2 * cp.pi * x) + cp.cos(2 * cp.pi * y)
image += pattern

# 2D FFT
fft_image = cp.fft.fft2(image)
fft_shifted = cp.fft.fftshift(fft_image)  # Center DC component

# Magnitude spectrum
magnitude_spectrum = cp.log(cp.abs(fft_shifted) + 1)

# Apply frequency domain filter (low-pass)
center = (128, 128)
radius = 50
mask = cp.zeros_like(fft_shifted)
y_grid, x_grid = cp.ogrid[:256, :256]
distance = cp.sqrt((x_grid - center[0])**2 + (y_grid - center[1])**2)
mask[distance <= radius] = 1

# Apply filter and inverse transform
filtered_fft = fft_shifted * mask
filtered_fft = cp.fft.ifftshift(filtered_fft)
filtered_image = cp.fft.ifft2(filtered_fft).real

Real FFT for Efficient Processing

# Real-valued signal processing
real_signal = cp.cos(2 * cp.pi * cp.linspace(0, 10, 1024))

# Real FFT (more efficient for real inputs)
rfft_result = cp.fft.rfft(real_signal)
rfreqs = cp.fft.rfftfreq(len(real_signal))

# Modify spectrum (e.g., remove high frequencies)
cutoff = len(rfft_result) // 4
rfft_result[cutoff:] = 0

# Inverse real FFT
filtered_signal = cp.fft.irfft(rfft_result)

# Compare original and filtered
print(f"Original signal length: {len(real_signal)}")
print(f"Filtered signal length: {len(filtered_signal)}")

N-Dimensional FFT

# 3D data (e.g., volumetric data)
volume = cp.random.random((64, 64, 64)).astype(cp.complex64)

# 3D FFT on all axes
fft_3d = cp.fft.fftn(volume)

# FFT on specific axes only
fft_2d_slices = cp.fft.fftn(volume, axes=(0, 1))  # FFT on first two dimensions

# Frequency analysis
freq_x = cp.fft.fftfreq(64)
freq_y = cp.fft.fftfreq(64)
freq_z = cp.fft.fftfreq(64)

# Power spectrum
power_3d = cp.abs(fft_3d)**2
total_power = cp.sum(power_3d)

Advanced FFT Applications

# Convolution using FFT (often faster for large arrays)
def fft_convolve(a, b):
    # Pad arrays to avoid circular convolution artifacts
    size = len(a) + len(b) - 1
    fft_a = cp.fft.fft(a, n=size)
    fft_b = cp.fft.fft(b, n=size)
    return cp.fft.ifft(fft_a * fft_b).real[:size]

# Example signals
signal1 = cp.random.random(1000)
kernel = cp.array([0.25, 0.5, 0.25])  # Simple smoothing kernel

convolved = fft_convolve(signal1, kernel)

# Cross-correlation using FFT
def fft_correlate(a, b):
    fft_a = cp.fft.fft(a, n=len(a) + len(b) - 1)
    fft_b = cp.fft.fft(b[::-1], n=len(a) + len(b) - 1)  # Reverse b
    return cp.fft.ifft(fft_a * cp.conj(fft_b)).real

correlation = fft_correlate(signal1, kernel)

Install with Tessl CLI

npx tessl i tessl/pypi-cupy-cuda101

docs

array-creation.md

array-manipulation.md

binary-operations.md

cuda.md

fft.md

index.md

indexing-searching.md

linalg.md

logic-functions.md

math-functions.md

memory-performance.md

random.md

sorting-counting.md

statistics.md

tile.json