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 (numpy.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

numpy.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 (numpy.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

numpy.ndarray

prysm.fttools.fourier_resample(f, zoom)

Resample f via Fourier methods (truncated sinc interpolation).

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

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

Returns

zoomed f

Return type

numpy.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

class prysm.fttools.MatrixDFTExecutor

Bases: object

MatrixDFTExecutor is an engine for performing matrix triple product DFTs as fast as possible.

dft2(ary, Q, samples, shift=(0, 0))

Compute the two dimensional Discrete Fourier Transform of a matrix.

Parameters
  • ary (numpy.ndarray) – an array, 2D, real or complex. Not fftshifted.

  • Q (float) – oversampling / padding factor to mimic an FFT. If Q=2, Nyquist sampled

  • samples (int or Iterable) – number of samples in the output plane. If an int, used for both dimensions. If an iterable, used for each dim

  • shift (float, optional) – shift of the output domain, as a frequency. Same broadcast rules apply as with samples.

Returns

2D array containing the shifted transform. Equivalent to ifftshift(fft2(fftshift(ary))) modulo output sampling/grid differences

Return type

numpy.ndarray

idft2(ary, Q, samples, shift=(0, 0))

Compute the two dimensional inverse Discrete Fourier Transform of a matrix.

Parameters
  • ary (numpy.ndarray) – an array, 2D, real or complex. Not fftshifted.

  • Q (float) – oversampling / padding factor to mimic an FFT. If Q=2, Nyquist sampled

  • samples (int or Iterable) – number of samples in the output plane. If an int, used for both dimensions. If an iterable, used for each dim

  • shift (float, optional) – shift of the output domain, as a frequency. Same broadcast rules apply as with samples.

Returns

2D array containing the shifted transform. Equivalent to ifftshift(ifft2(fftshift(ary))) modulo output sampling/grid differences

Return type

numpy.ndarray

clear()

Empty the internal caches to release memory.

nbytes()

Total size in memory of the cache in bytes.

class prysm.fttools.ChirpZTransformExecutor

Bases: object

Type which executes Chirp Z Transforms on 2D data, aka zoom FFTs.

czt2(ary, Q, samples, shift=(0, 0))

Compute the two dimensional Chirp Z Transform of a matrix.

Parameters
  • ary (numpy.ndarray) – an array, 2D, real or complex. Not fftshifted.

  • Q (float) – oversampling / padding factor to mimic an FFT. If Q=2, Nyquist sampled

  • samples (int or Iterable) – number of samples in the output plane. If an int, used for both dimensions. If an iterable, used for each dim

  • shift (float, optional) – shift of the output domain, as a number of samples at the output sample rate. I.e., if ary is 256x256, Q=2, and samples=512, then the output is identical to a padded FFT. If shift=256, the DC frequency will be at the edge of the array; shift=(-256,256) would produce the same result as a padded FFT without shifts.

Returns

2D array containing the shifted transform. Equivalent to ifftshift(fft2(fftshift(ary))) modulo output sampling/grid differences

Return type

numpy.ndarray

iczt2(ary, Q, samples, shift=(0, 0))

Compute the two dimensional inverse Chirp Z Transform of a matrix.

Parameters
  • ary (numpy.ndarray) – an array, 2D, real or complex. Not fftshifted.

  • Q (float) – oversampling / padding factor to mimic an FFT. If Q=2, Nyquist sampled

  • samples (int or Iterable) – number of samples in the output plane. If an int, used for both dimensions. If an iterable, used for each dim

  • shift (float, optional) – shift of the output domain, as a number of samples at the output sample rate. I.e., if ary is 256x256, Q=2, and samples=512, then the output is identical to a padded FFT. If shift=256, the DC frequency will be at the edge of the array; shift=(-256,256) would produce the same result as a padded FFT without shifts.

Returns

2D array containing the shifted transform. Equivalent to ifftshift(fft2(fftshift(ary))) modulo output sampling/grid differences

Return type

numpy.ndarray

clear()

Empty the cache.

nbytes()

Total size in memory of the cache in bytes.