hwoutils.fft#

FFT-based sub-pixel image shifting.

Provides Fourier shift primitives for sub-pixel image translation. The JAX versions (fft_shift_x, fft_shift_y) accept precomputed phasors for efficient repeated shifts. The NumPy versions (fft_shift, fft_shift_1d) are standalone.

All functions operate on 2D images via separable 1D FFTs along each axis, which is O(2N * N log N) vs O(N^2 log N^2) for a full 2D FFT.

Functions#

get_pad_info(image, pad_factor)

Compute padding sizes for FFT shift operations.

fft_shift_x(image, shift_pixels, phasor[, clamp])

Apply a Fourier shift along the x-axis (JAX, JIT-compatible).

fft_shift_y(image, shift_pixels, phasor[, clamp])

Apply a Fourier shift along the y-axis (JAX, JIT-compatible).

fft_shift_1d(image, shift_pixels, axis)

Apply a Fourier shift along a specified axis (NumPy).

fft_shift(image[, x, y])

Apply Fourier shifts along x and/or y axes (NumPy).

Module Contents#

hwoutils.fft.get_pad_info(image, pad_factor)[source]#

Compute padding sizes for FFT shift operations.

Args:

image: 2D input image (JAX or NumPy array). pad_factor: Factor by which to pad (e.g. 1.5 gives 50% on each side).

Returns:

Tuple of (n_pixels_orig, n_pad, img_edge, n_pixels_final).

hwoutils.fft.fft_shift_x(image, shift_pixels, phasor, clamp=True)[source]#

Apply a Fourier shift along the x-axis (JAX, JIT-compatible).

Uses a precomputed phasor for efficient repeated shifts of images with the same shape.

Args:

image: 2D input image (JAX array). shift_pixels: Sub-pixel shift amount along x. phasor: Precomputed exp(-2j * pi * fft_freqs) for the padded size. clamp: If True, clamp negative values to zero after shift.

Returns:

Shifted image with same shape as input.

hwoutils.fft.fft_shift_y(image, shift_pixels, phasor, clamp=True)[source]#

Apply a Fourier shift along the y-axis (JAX, JIT-compatible).

Uses a precomputed phasor for efficient repeated shifts of images with the same shape.

Args:

image: 2D input image (JAX array). shift_pixels: Sub-pixel shift amount along y. phasor: Precomputed exp(-2j * pi * fft_freqs) for the padded size. clamp: If True, clamp negative values to zero after shift.

Returns:

Shifted image with same shape as input.

hwoutils.fft.fft_shift_1d(image, shift_pixels, axis)[source]#

Apply a Fourier shift along a specified axis (NumPy).

Pads, applies a 1D FFT phasor shift, and unpads. Standalone version that computes its own phasor internally.

Args:

image: 2D input image (NumPy array). shift_pixels: Sub-pixel shift amount. axis: Axis to shift (0 for vertical/y, 1 for horizontal/x).

Returns:

Shifted image with same shape as input.

hwoutils.fft.fft_shift(image, x=0, y=0)[source]#

Apply Fourier shifts along x and/or y axes (NumPy).

Convenience wrapper that calls fft_shift_1d for each non-zero axis.

Args:

image: 2D input image (NumPy array). x: Sub-pixel shift along x-axis. y: Sub-pixel shift along y-axis.

Returns:

Shifted image with same shape as input.

Raises:

AssertionError: If both x and y are zero.