Fourier Transforms

In the C++ layer we use FFTW for our 2D Fourier transforms. This package is generally faster than numpy fft functions. So for at least a subset of the functionality available in the numpy versions, we have implemented python functions that call out to the backend C++ FFTW functions.

These should be drop in replacements for np.fft.* functions. e.g.:

>>> karray = galsim.fft.fft2(xarray)

is functionally equivalent to:

>>> karray = np.fft.fft2(xarray)

but should be a bit faster.

Note

The GalSim versions often only implement the normal use case without many of the advanced options available with the numpy functions. This is mostly laziness on our part – we only implemented the functions that we needed. If your usage requires some option available in the numpy version, feel free to post a feature request on our GitHub page.

galsim.fft.fft2(a, shift_in=False, shift_out=False)[source]

Compute the 2-dimensional discrete Fourier Transform.

For valid inputs, the result is equivalent to numpy.fft.fft2(a), but usually faster.:

>>> ka1 = numpy.fft.fft2(a)
>>> ka2 = galsim.fft.fft2(a)

Restrictions on this version vs the numpy version:

  • The input array must be 2-dimensional.

  • The size in each direction must be even. (Ideally 2^k or 3*2^k for speed, but this is not required.)

  • If it has a real dtype, it will be coerced to numpy.float64.

  • If it has a complex dtype, it will be coerced to numpy.complex128.

The returned array will be complex with dtype numpy.complex128.

If shift_in is True, then this is equivalent to applying numpy.fft.fftshift to the input.:

>>> ka1 = numpy.fft.fft2(numpy.fft.fftshift(a))
>>> ka2 = galsim.fft.fft2(a, shift_in=True)

If shift_out is True, then this is equivalent to applying numpy.fft.fftshift to the output.:

>>> ka1 = numpy.fft.fftshift(numpy.fft.fft2(a))
>>> ka2 = galsim.fft.fft2(a, shift_out=True)
Parameters:
  • a – The input array to be transformed

  • shift_in – Whether to shift the input array so that the center is moved to (0,0). [default: False]

  • shift_out – Whether to shift the output array so that the center is moved to (0,0). [default: False]

Returns:

a complex numpy array

galsim.fft.ifft2(a, shift_in=False, shift_out=False)[source]

Compute the 2-dimensional inverse discrete Fourier Transform.

For valid inputs, the result is equivalent to numpy.fft.ifft2(a), but usually faster.:

>>> a1 = numpy.fft.ifft2(ka)
>>> a2 = galsim.fft.ifft2(ka)

Restrictions on this version vs the numpy version:

  • The array must be 2-dimensional.

  • The size in each direction must be even. (Ideally 2^k or 3*2^k for speed, but this is not required.)

  • The array is assumed to be Hermitian, which means the k values with kx<0 are assumed to be equal to the conjuate of their inverse. This will always be the case if a is an output of fft2 (with a real input array). i.e.

    • for kx >= N/2, ky > 0: a[ky, kx] == a[N-ky, N-kx].conjugate()

    • for kx >= N/2, ky = 0: a[0, kx] == a[0, N-kx].conjugate()

    Only the elements a[:,0:N/2+1] are accessed by this function.

  • If it has a real dtype, it will be coerced to numpy.float64.

  • If it has a complex dtype, it will be coerced to numpy.complex128.

The returned array will be complex with dtype numpy.complex128.

If shift_in is True, then this is equivalent to applying numpy.fft.fftshift to the input:

>>> a1 = numpy.fft.ifft2(numpy.fft.fftshift(ka))
>>> a2 = galsim.fft.ifft2(ka, shift_in=True)

If shift_out is True, then this is equivalent to applying numpy.fft.fftshift to the output:

>>> a1 = numpy.fft.fftshift(numpy.fft.ifft2(ka))
>>> a2 = galsim.fft.ifft2(ka, shift_out=True)
Parameters:
  • a – The input array to be transformed

  • shift_in – Whether to shift the input array so that the center is moved to (0,0). [default: False]

  • shift_out – Whether to shift the output array so that the center is moved to (0,0). [default: False]

Returns:

a complex numpy array

galsim.fft.rfft2(a, shift_in=False, shift_out=False)[source]

Compute the one-dimensional discrete Fourier Transform for real input.

For valid inputs, the result is equivalent to numpy.fft.rfft2(a), but usually faster.:

>>> ka1 = numpy.fft.rfft2(a)
>>> ka2 = galsim.fft.rfft2(a)

Restrictions on this version vs the numpy version:

  • The input array must be 2-dimensional.

  • If it does not have dtype numpy.float64, it will be coerced to numpy.float64.

  • The size in each direction must be even. (Ideally 2^k or 3*2^k for speed, but this is not required.)

The returned array will be complex with dtype numpy.complex128.

If shift_in is True, then this is equivalent to applying numpy.fft.fftshift to the input.:

>>> ka1 = numpy.fft.rfft2(numpy.fft.fftshift(a))
>>> ka2 = galsim.fft.rfft2(a, shift_in=True)

If shift_out is True, then this is equivalent to applying numpy.fft.fftshift to the output.:

>>> ka1 = numpy.fft.fftshift(numpy.fft.rfft2(a),axes=(0,))
>>> ka2 = galsim.fft.rfft2(a, shift_out=True)
Parameters:
  • a – The input array to be transformed

  • shift_in – Whether to shift the input array so that the center is moved to (0,0). [default: False]

  • shift_out – Whether to shift the output array so that the center is moved to (0,0). [default: False]

Returns:

a complex numpy array

galsim.fft.irfft2(a, shift_in=False, shift_out=False)[source]

Compute the 2-dimensional inverse FFT of a real array.

For valid inputs, the result is equivalent to numpy.fft.irfft2(a), but usually faster.:

>>> a1 = numpy.fft.irfft2(ka)
>>> a2 = galsim.fft.irfft2(ka)

Restrictions on this version vs the numpy version:

  • The array must be 2-dimensional.

  • If it does not have dtype numpy.complex128, it will be coerced to numpy.complex128.

  • It must have shape (M, N/2+1).

  • The size M must be even. (Ideally 2^k or 3*2^k for speed, but this is not required.)

The returned array will be real with dtype numpy.float64.

If shift_in is True, then this is equivalent to applying numpy.fft.fftshift to the input.:

>>> a1 = numpy.fft.irfft2(numpy.fft.fftshift(a, axes=(0,)))
>>> a2 = galsim.fft.irfft2(a, shift_in=True)

If shift_out is True, then this is equivalent to applying numpy.fft.fftshift to the output.:

>>> a1 = numpy.fft.fftshift(numpy.fft.irfft2(a))
>>> a2 = galsim.fft.irfft2(a, shift_out=True)
Parameters:
  • a – The input array to be transformed

  • shift_in – Whether to shift the input array so that the center is moved to (0,0). [default: False]

  • shift_out – Whether to shift the output array so that the center is moved to (0,0). [default: False]

Returns:

a real numpy array