tensor.fft – Fast Fourier Transforms

Performs Fast Fourier Transforms (FFT).

FFT gradients are implemented as the opposite Fourier transform of the output gradients.

Warning

The real and imaginary parts of the Fourier domain arrays are stored as a pair of floatarrays, emulating complex. Since theano has limited support for complexnumber operations, care must be taken to manually implement operations such as gradients.

  • theano.tensor.fft.rfft(inp, norm=None)[source]
  • Performs the fast Fourier transform of a real-valued input.

The input must be a real-valued variable of dimensions (m, …, n).It performs FFTs of size (…, n) on m batches.

The output is a tensor of dimensions (m, …, n//2+1, 2). The second tolast dimension of the output contains the n//2+1 non-trivial elements ofthe real-valued FFTs. The real and imaginary parts are stored as a pair offloat arrays.

Parameters:

  • inp – Array of floats of size (m, …, n), containing m inputs ofsize (…, n).
  • norm ({None, 'ortho', 'no_norm'}) – Normalization of transform. Following numpy, default None normalizesonly the inverse transform by n, ‘ortho’ yields the unitary transform(1/\sqrt n forward and inverse). In addition, ‘no_norm’ leavesthe transform unnormalized.
  • theano.tensor.fft.irfft(inp, norm=None, is_odd=False)[source]
  • Performs the inverse fast Fourier Transform with real-valued output.

The input is a variable of dimensions (m, …, n//2+1, 2)representing the non-trivial elements of m real-valued Fourier transformsof initial size (…, n). The real and imaginary parts are stored as apair of float arrays.

The output is a real-valued variable of dimensions (m, …, n)giving the m inverse FFTs.

Parameters:

  • inp – Array of size (m, …, n//2+1, 2), containing m inputswith n//2+1 non-trivial elements on the last dimension and realand imaginary parts stored as separate real arrays.
  • norm ({None, 'ortho', 'no_norm'}) – Normalization of transform. Following numpy, default None normalizesonly the inverse transform by n, ‘ortho’ yields the unitary transform(1/\sqrt n forward and inverse). In addition, ‘no_norm’ leavesthe transform unnormalized.
  • is_odd ({True, False}) – Set to True to get a real inverse transform output with an odd last dimensionof length (N-1)*2 + 1 for an input last dimension of length N.

For example, the code below performs the real input FFT of a box function,which is a sinc function. The absolute value is plotted, since the phaseoscillates due to the box function being shifted to the middle of the array.

  1. import numpy as np
  2. import theano
  3. import theano.tensor as T
  4. from theano.tensor import fft
  5.  
  6. x = T.matrix('x', dtype='float64')
  7.  
  8. rfft = fft.rfft(x, norm='ortho')
  9. f_rfft = theano.function([x], rfft)
  10.  
  11. N = 1024
  12. box = np.zeros((1, N), dtype='float64')
  13. box[:, N//2-10: N//2+10] = 1
  14.  
  15. out = f_rfft(box)
  16. c_out = np.asarray(out[0, :, 0] + 1j*out[0, :, 1])
  17. abs_out = abs(c_out)

../../_images/plot_fft1.png