prysm.fttools#

Supplimental tools for computing fourier transforms.

prysm.fttools.fftrange(n, dtype=None)#

FFT-aligned coordinate grid for n samples.

prysm.fttools.next_fast_len(n)#

The next fast FFT size.

Defaults to powers of two if the FFT backend does not provide a function of the same name.

prysm.fttools.fftfreq(n, d=1.0)#

Fast Fourier Transform frequency vector.

prysm.fttools.pad2d(array, Q=2, value=0, mode='constant', out_shape=None)#

Symmetrically pads a 2D array with a value.

Parameters:
  • array (ndarray) – source array

  • Q (float, optional) – oversampling factor; ratio of input to output array widths

  • value (float, optioanl) – value with which to pad the array

  • mode (str, optional) – mode, passed directly to np.pad

  • out_shape (tuple) – output shape for the array. Overrides Q if given. in_shape * Q ~= out_shape (up to integer rounding)

Returns:

padded array, may share memory with input array

Return type:

ndarray

Notes

padding will be symmetric.

prysm.fttools.crop_center(img, out_shape)#

Crop the central (out_shape) of an image, with FFT alignment.

As an example, if img=512x512 and out_shape=200 out_shape => 200x200 and the returned array is 200x200, 156 elements from the [0,0]th pixel

This function is the adjoint of pad2d.

Parameters:
  • img (ndarray) – ndarray of shape (m, n)

  • out_shape (int or iterable of int) – shape to crop out, either a scalar or pair of values

prysm.fttools.forward_ft_unit(dx, samples, shift=True)#

Compute the units resulting from a fourier transform.

Parameters:
  • dx (float) – center-to-center spacing of samples in an array

  • samples (int) – number of samples in the data

  • shift (bool, optional) – whether to shift the output. If True, first element is a negative freq if False, first element is 0 freq.

Returns:

array of sample frequencies in the output of an fft

Return type:

ndarray

class prysm.fttools.MDFT(x, y, fx, fy, sign=-1, norm=1.0)#

Bases: object

Matrix DFT parameterized by input coordinates and output frequencies.

Computes out[i, j] = norm * sum_{k, l} ary[k, l] * exp(sign * 2j*pi * (y[k]*fy[i] + x[l]*fx[j])) by precomputing two 1D basis matrices and applying them as Ey @ ary @ Ex.T.

Parameters:
  • x (ndarray) – 1D arrays of input-plane coordinates along the second and first axis of the input array, respectively. Lengths must match ary.shape[1] and ary.shape[0].

  • y (ndarray) – 1D arrays of input-plane coordinates along the second and first axis of the input array, respectively. Lengths must match ary.shape[1] and ary.shape[0].

  • fx (ndarray) – 1D arrays of output-plane frequencies along the second and first axis of the output array, respectively. Output shape will be (len(fy), len(fx)).

  • fy (ndarray) – 1D arrays of output-plane frequencies along the second and first axis of the output array, respectively. Output shape will be (len(fy), len(fx)).

  • sign (int, optional) – Sign of the kernel exponent. -1 (default) is the forward DFT convention; +1 is the inverse DFT convention.

  • norm (float, optional) – Real scalar applied to the result of every __call__ and adjoint. Default 1.0 (bare DFT sum). Set this to bake the problem-appropriate scaling into the operator.

Notes

The same instance can be reused on different input arrays of matching shape — basis matrices are precomputed once at construction. Holding an instance is the caching mechanism; there is no global cache.

Build the basis matrices.

adjoint(grad)#

Apply the adjoint (conjugate transpose) of the forward DFT.

For a real norm, this is the gradient backpropagation operator for __call__. It also coincides with the inverse DFT (i.e. an MDFT of opposite sign that maps the output shape back to the input shape) up to scaling.

nbytes()#

Total size in memory of the basis matrices, bytes.

class prysm.fttools.CZT(x, y, fx, fy, sign=-1, norm=1.0)#

Bases: object

Chirp-Z transform with the same external API as MDFT.

Internally uses the Bluestein/Jurling factorization for O(N log N) cost per axis. Requires x, y, fx, and fy to be uniformly spaced. Center offsets in the input coordinates and output frequency grids are handled by linear phase factors.

Precompute CZT components for both axes.

adjoint(grad)#

Apply the adjoint (conjugate transpose) of the forward CZT.

nbytes()#

Total size in memory of the cached components, bytes.

prysm.fttools.fourier_resample(f, zoom)#

Resample f via Fourier methods (truncated sinc interpolation).

Parameters:
  • f (ndarray) – ndim 2 ndarray, floating point dtype

  • zoom (float) – zoom factor to apply out.shape == f.shape*zoom

Returns:

zoomed f

Return type:

ndarray

Notes

Assumes F is (reasonably) bandlimited

Energy will be deleted, not aliased, if zoom results in the output domain being smaller than the Fourier support of f