Chris@87: """ Chris@87: Discrete Fourier Transforms Chris@87: Chris@87: Routines in this module: Chris@87: Chris@87: fft(a, n=None, axis=-1) Chris@87: ifft(a, n=None, axis=-1) Chris@87: rfft(a, n=None, axis=-1) Chris@87: irfft(a, n=None, axis=-1) Chris@87: hfft(a, n=None, axis=-1) Chris@87: ihfft(a, n=None, axis=-1) Chris@87: fftn(a, s=None, axes=None) Chris@87: ifftn(a, s=None, axes=None) Chris@87: rfftn(a, s=None, axes=None) Chris@87: irfftn(a, s=None, axes=None) Chris@87: fft2(a, s=None, axes=(-2,-1)) Chris@87: ifft2(a, s=None, axes=(-2, -1)) Chris@87: rfft2(a, s=None, axes=(-2,-1)) Chris@87: irfft2(a, s=None, axes=(-2, -1)) Chris@87: Chris@87: i = inverse transform Chris@87: r = transform of purely real data Chris@87: h = Hermite transform Chris@87: n = n-dimensional transform Chris@87: 2 = 2-dimensional transform Chris@87: (Note: 2D routines are just nD routines with different default Chris@87: behavior.) Chris@87: Chris@87: The underlying code for these functions is an f2c-translated and modified Chris@87: version of the FFTPACK routines. Chris@87: Chris@87: """ Chris@87: from __future__ import division, absolute_import, print_function Chris@87: Chris@87: __all__ = ['fft', 'ifft', 'rfft', 'irfft', 'hfft', 'ihfft', 'rfftn', Chris@87: 'irfftn', 'rfft2', 'irfft2', 'fft2', 'ifft2', 'fftn', 'ifftn'] Chris@87: Chris@87: from numpy.core import asarray, zeros, swapaxes, shape, conjugate, \ Chris@87: take Chris@87: from . import fftpack_lite as fftpack Chris@87: Chris@87: _fft_cache = {} Chris@87: _real_fft_cache = {} Chris@87: Chris@87: def _raw_fft(a, n=None, axis=-1, init_function=fftpack.cffti, Chris@87: work_function=fftpack.cfftf, fft_cache = _fft_cache ): Chris@87: a = asarray(a) Chris@87: Chris@87: if n is None: Chris@87: n = a.shape[axis] Chris@87: Chris@87: if n < 1: Chris@87: raise ValueError("Invalid number of FFT data points (%d) specified." % n) Chris@87: Chris@87: try: Chris@87: # Thread-safety note: We rely on list.pop() here to atomically Chris@87: # retrieve-and-remove a wsave from the cache. This ensures that no Chris@87: # other thread can get the same wsave while we're using it. Chris@87: wsave = fft_cache.setdefault(n, []).pop() Chris@87: except (IndexError): Chris@87: wsave = init_function(n) Chris@87: Chris@87: if a.shape[axis] != n: Chris@87: s = list(a.shape) Chris@87: if s[axis] > n: Chris@87: index = [slice(None)]*len(s) Chris@87: index[axis] = slice(0, n) Chris@87: a = a[index] Chris@87: else: Chris@87: index = [slice(None)]*len(s) Chris@87: index[axis] = slice(0, s[axis]) Chris@87: s[axis] = n Chris@87: z = zeros(s, a.dtype.char) Chris@87: z[index] = a Chris@87: a = z Chris@87: Chris@87: if axis != -1: Chris@87: a = swapaxes(a, axis, -1) Chris@87: r = work_function(a, wsave) Chris@87: if axis != -1: Chris@87: r = swapaxes(r, axis, -1) Chris@87: Chris@87: # As soon as we put wsave back into the cache, another thread could pick it Chris@87: # up and start using it, so we must not do this until after we're Chris@87: # completely done using it ourselves. Chris@87: fft_cache[n].append(wsave) Chris@87: Chris@87: return r Chris@87: Chris@87: Chris@87: def fft(a, n=None, axis=-1): Chris@87: """ Chris@87: Compute the one-dimensional discrete Fourier Transform. Chris@87: Chris@87: This function computes the one-dimensional *n*-point discrete Fourier Chris@87: Transform (DFT) with the efficient Fast Fourier Transform (FFT) Chris@87: algorithm [CT]. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: a : array_like Chris@87: Input array, can be complex. Chris@87: n : int, optional Chris@87: Length of the transformed axis of the output. Chris@87: If `n` is smaller than the length of the input, the input is cropped. Chris@87: If it is larger, the input is padded with zeros. If `n` is not given, Chris@87: the length of the input along the axis specified by `axis` is used. Chris@87: axis : int, optional Chris@87: Axis over which to compute the FFT. If not given, the last axis is Chris@87: used. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : complex ndarray Chris@87: The truncated or zero-padded input, transformed along the axis Chris@87: indicated by `axis`, or the last one if `axis` is not specified. Chris@87: Chris@87: Raises Chris@87: ------ Chris@87: IndexError Chris@87: if `axes` is larger than the last axis of `a`. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: numpy.fft : for definition of the DFT and conventions used. Chris@87: ifft : The inverse of `fft`. Chris@87: fft2 : The two-dimensional FFT. Chris@87: fftn : The *n*-dimensional FFT. Chris@87: rfftn : The *n*-dimensional FFT of real input. Chris@87: fftfreq : Frequency bins for given FFT parameters. Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: FFT (Fast Fourier Transform) refers to a way the discrete Fourier Chris@87: Transform (DFT) can be calculated efficiently, by using symmetries in the Chris@87: calculated terms. The symmetry is highest when `n` is a power of 2, and Chris@87: the transform is therefore most efficient for these sizes. Chris@87: Chris@87: The DFT is defined, with the conventions used in this implementation, in Chris@87: the documentation for the `numpy.fft` module. Chris@87: Chris@87: References Chris@87: ---------- Chris@87: .. [CT] Cooley, James W., and John W. Tukey, 1965, "An algorithm for the Chris@87: machine calculation of complex Fourier series," *Math. Comput.* Chris@87: 19: 297-301. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> np.fft.fft(np.exp(2j * np.pi * np.arange(8) / 8)) Chris@87: array([ -3.44505240e-16 +1.14383329e-17j, Chris@87: 8.00000000e+00 -5.71092652e-15j, Chris@87: 2.33482938e-16 +1.22460635e-16j, Chris@87: 1.64863782e-15 +1.77635684e-15j, Chris@87: 9.95839695e-17 +2.33482938e-16j, Chris@87: 0.00000000e+00 +1.66837030e-15j, Chris@87: 1.14383329e-17 +1.22460635e-16j, Chris@87: -1.64863782e-15 +1.77635684e-15j]) Chris@87: Chris@87: >>> import matplotlib.pyplot as plt Chris@87: >>> t = np.arange(256) Chris@87: >>> sp = np.fft.fft(np.sin(t)) Chris@87: >>> freq = np.fft.fftfreq(t.shape[-1]) Chris@87: >>> plt.plot(freq, sp.real, freq, sp.imag) Chris@87: [, ] Chris@87: >>> plt.show() Chris@87: Chris@87: In this example, real input has an FFT which is Hermitian, i.e., symmetric Chris@87: in the real part and anti-symmetric in the imaginary part, as described in Chris@87: the `numpy.fft` documentation. Chris@87: Chris@87: """ Chris@87: Chris@87: return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftf, _fft_cache) Chris@87: Chris@87: Chris@87: def ifft(a, n=None, axis=-1): Chris@87: """ Chris@87: Compute the one-dimensional inverse discrete Fourier Transform. Chris@87: Chris@87: This function computes the inverse of the one-dimensional *n*-point Chris@87: discrete Fourier transform computed by `fft`. In other words, Chris@87: ``ifft(fft(a)) == a`` to within numerical accuracy. Chris@87: For a general description of the algorithm and definitions, Chris@87: see `numpy.fft`. Chris@87: Chris@87: The input should be ordered in the same way as is returned by `fft`, Chris@87: i.e., ``a[0]`` should contain the zero frequency term, Chris@87: ``a[1:n/2+1]`` should contain the positive-frequency terms, and Chris@87: ``a[n/2+1:]`` should contain the negative-frequency terms, in order of Chris@87: decreasingly negative frequency. See `numpy.fft` for details. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: a : array_like Chris@87: Input array, can be complex. Chris@87: n : int, optional Chris@87: Length of the transformed axis of the output. Chris@87: If `n` is smaller than the length of the input, the input is cropped. Chris@87: If it is larger, the input is padded with zeros. If `n` is not given, Chris@87: the length of the input along the axis specified by `axis` is used. Chris@87: See notes about padding issues. Chris@87: axis : int, optional Chris@87: Axis over which to compute the inverse DFT. If not given, the last Chris@87: axis is used. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : complex ndarray Chris@87: The truncated or zero-padded input, transformed along the axis Chris@87: indicated by `axis`, or the last one if `axis` is not specified. Chris@87: Chris@87: Raises Chris@87: ------ Chris@87: IndexError Chris@87: If `axes` is larger than the last axis of `a`. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: numpy.fft : An introduction, with definitions and general explanations. Chris@87: fft : The one-dimensional (forward) FFT, of which `ifft` is the inverse Chris@87: ifft2 : The two-dimensional inverse FFT. Chris@87: ifftn : The n-dimensional inverse FFT. Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: If the input parameter `n` is larger than the size of the input, the input Chris@87: is padded by appending zeros at the end. Even though this is the common Chris@87: approach, it might lead to surprising results. If a different padding is Chris@87: desired, it must be performed before calling `ifft`. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> np.fft.ifft([0, 4, 0, 0]) Chris@87: array([ 1.+0.j, 0.+1.j, -1.+0.j, 0.-1.j]) Chris@87: Chris@87: Create and plot a band-limited signal with random phases: Chris@87: Chris@87: >>> import matplotlib.pyplot as plt Chris@87: >>> t = np.arange(400) Chris@87: >>> n = np.zeros((400,), dtype=complex) Chris@87: >>> n[40:60] = np.exp(1j*np.random.uniform(0, 2*np.pi, (20,))) Chris@87: >>> s = np.fft.ifft(n) Chris@87: >>> plt.plot(t, s.real, 'b-', t, s.imag, 'r--') Chris@87: [, ] Chris@87: >>> plt.legend(('real', 'imaginary')) Chris@87: Chris@87: >>> plt.show() Chris@87: Chris@87: """ Chris@87: Chris@87: a = asarray(a).astype(complex) Chris@87: if n is None: Chris@87: n = shape(a)[axis] Chris@87: return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftb, _fft_cache) / n Chris@87: Chris@87: Chris@87: def rfft(a, n=None, axis=-1): Chris@87: """ Chris@87: Compute the one-dimensional discrete Fourier Transform for real input. Chris@87: Chris@87: This function computes the one-dimensional *n*-point discrete Fourier Chris@87: Transform (DFT) of a real-valued array by means of an efficient algorithm Chris@87: called the Fast Fourier Transform (FFT). Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: a : array_like Chris@87: Input array Chris@87: n : int, optional Chris@87: Number of points along transformation axis in the input to use. Chris@87: If `n` is smaller than the length of the input, the input is cropped. Chris@87: If it is larger, the input is padded with zeros. If `n` is not given, Chris@87: the length of the input along the axis specified by `axis` is used. Chris@87: axis : int, optional Chris@87: Axis over which to compute the FFT. If not given, the last axis is Chris@87: used. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : complex ndarray Chris@87: The truncated or zero-padded input, transformed along the axis Chris@87: indicated by `axis`, or the last one if `axis` is not specified. Chris@87: If `n` is even, the length of the transformed axis is ``(n/2)+1``. Chris@87: If `n` is odd, the length is ``(n+1)/2``. Chris@87: Chris@87: Raises Chris@87: ------ Chris@87: IndexError Chris@87: If `axis` is larger than the last axis of `a`. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: numpy.fft : For definition of the DFT and conventions used. Chris@87: irfft : The inverse of `rfft`. Chris@87: fft : The one-dimensional FFT of general (complex) input. Chris@87: fftn : The *n*-dimensional FFT. Chris@87: rfftn : The *n*-dimensional FFT of real input. Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: When the DFT is computed for purely real input, the output is Chris@87: Hermitian-symmetric, i.e. the negative frequency terms are just the complex Chris@87: conjugates of the corresponding positive-frequency terms, and the Chris@87: negative-frequency terms are therefore redundant. This function does not Chris@87: compute the negative frequency terms, and the length of the transformed Chris@87: axis of the output is therefore ``n//2 + 1``. Chris@87: Chris@87: When ``A = rfft(a)`` and fs is the sampling frequency, ``A[0]`` contains Chris@87: the zero-frequency term 0*fs, which is real due to Hermitian symmetry. Chris@87: Chris@87: If `n` is even, ``A[-1]`` contains the term representing both positive Chris@87: and negative Nyquist frequency (+fs/2 and -fs/2), and must also be purely Chris@87: real. If `n` is odd, there is no term at fs/2; ``A[-1]`` contains Chris@87: the largest positive frequency (fs/2*(n-1)/n), and is complex in the Chris@87: general case. Chris@87: Chris@87: If the input `a` contains an imaginary part, it is silently discarded. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> np.fft.fft([0, 1, 0, 0]) Chris@87: array([ 1.+0.j, 0.-1.j, -1.+0.j, 0.+1.j]) Chris@87: >>> np.fft.rfft([0, 1, 0, 0]) Chris@87: array([ 1.+0.j, 0.-1.j, -1.+0.j]) Chris@87: Chris@87: Notice how the final element of the `fft` output is the complex conjugate Chris@87: of the second element, for real input. For `rfft`, this symmetry is Chris@87: exploited to compute only the non-negative frequency terms. Chris@87: Chris@87: """ Chris@87: Chris@87: a = asarray(a).astype(float) Chris@87: return _raw_fft(a, n, axis, fftpack.rffti, fftpack.rfftf, _real_fft_cache) Chris@87: Chris@87: Chris@87: def irfft(a, n=None, axis=-1): Chris@87: """ Chris@87: Compute the inverse of the n-point DFT for real input. Chris@87: Chris@87: This function computes the inverse of the one-dimensional *n*-point Chris@87: discrete Fourier Transform of real input computed by `rfft`. Chris@87: In other words, ``irfft(rfft(a), len(a)) == a`` to within numerical Chris@87: accuracy. (See Notes below for why ``len(a)`` is necessary here.) Chris@87: Chris@87: The input is expected to be in the form returned by `rfft`, i.e. the Chris@87: real zero-frequency term followed by the complex positive frequency terms Chris@87: in order of increasing frequency. Since the discrete Fourier Transform of Chris@87: real input is Hermitian-symmetric, the negative frequency terms are taken Chris@87: to be the complex conjugates of the corresponding positive frequency terms. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: a : array_like Chris@87: The input array. Chris@87: n : int, optional Chris@87: Length of the transformed axis of the output. Chris@87: For `n` output points, ``n//2+1`` input points are necessary. If the Chris@87: input is longer than this, it is cropped. If it is shorter than this, Chris@87: it is padded with zeros. If `n` is not given, it is determined from Chris@87: the length of the input along the axis specified by `axis`. Chris@87: axis : int, optional Chris@87: Axis over which to compute the inverse FFT. If not given, the last Chris@87: axis is used. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : ndarray Chris@87: The truncated or zero-padded input, transformed along the axis Chris@87: indicated by `axis`, or the last one if `axis` is not specified. Chris@87: The length of the transformed axis is `n`, or, if `n` is not given, Chris@87: ``2*(m-1)`` where ``m`` is the length of the transformed axis of the Chris@87: input. To get an odd number of output points, `n` must be specified. Chris@87: Chris@87: Raises Chris@87: ------ Chris@87: IndexError Chris@87: If `axis` is larger than the last axis of `a`. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: numpy.fft : For definition of the DFT and conventions used. Chris@87: rfft : The one-dimensional FFT of real input, of which `irfft` is inverse. Chris@87: fft : The one-dimensional FFT. Chris@87: irfft2 : The inverse of the two-dimensional FFT of real input. Chris@87: irfftn : The inverse of the *n*-dimensional FFT of real input. Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: Returns the real valued `n`-point inverse discrete Fourier transform Chris@87: of `a`, where `a` contains the non-negative frequency terms of a Chris@87: Hermitian-symmetric sequence. `n` is the length of the result, not the Chris@87: input. Chris@87: Chris@87: If you specify an `n` such that `a` must be zero-padded or truncated, the Chris@87: extra/removed values will be added/removed at high frequencies. One can Chris@87: thus resample a series to `m` points via Fourier interpolation by: Chris@87: ``a_resamp = irfft(rfft(a), m)``. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> np.fft.ifft([1, -1j, -1, 1j]) Chris@87: array([ 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j]) Chris@87: >>> np.fft.irfft([1, -1j, -1]) Chris@87: array([ 0., 1., 0., 0.]) Chris@87: Chris@87: Notice how the last term in the input to the ordinary `ifft` is the Chris@87: complex conjugate of the second term, and the output has zero imaginary Chris@87: part everywhere. When calling `irfft`, the negative frequencies are not Chris@87: specified, and the output array is purely real. Chris@87: Chris@87: """ Chris@87: Chris@87: a = asarray(a).astype(complex) Chris@87: if n is None: Chris@87: n = (shape(a)[axis] - 1) * 2 Chris@87: return _raw_fft(a, n, axis, fftpack.rffti, fftpack.rfftb, Chris@87: _real_fft_cache) / n Chris@87: Chris@87: Chris@87: def hfft(a, n=None, axis=-1): Chris@87: """ Chris@87: Compute the FFT of a signal which has Hermitian symmetry (real spectrum). Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: a : array_like Chris@87: The input array. Chris@87: n : int, optional Chris@87: Length of the transformed axis of the output. Chris@87: For `n` output points, ``n//2+1`` input points are necessary. If the Chris@87: input is longer than this, it is cropped. If it is shorter than this, Chris@87: it is padded with zeros. If `n` is not given, it is determined from Chris@87: the length of the input along the axis specified by `axis`. Chris@87: axis : int, optional Chris@87: Axis over which to compute the FFT. If not given, the last Chris@87: axis is used. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : ndarray Chris@87: The truncated or zero-padded input, transformed along the axis Chris@87: indicated by `axis`, or the last one if `axis` is not specified. Chris@87: The length of the transformed axis is `n`, or, if `n` is not given, Chris@87: ``2*(m-1)`` where ``m`` is the length of the transformed axis of the Chris@87: input. To get an odd number of output points, `n` must be specified. Chris@87: Chris@87: Raises Chris@87: ------ Chris@87: IndexError Chris@87: If `axis` is larger than the last axis of `a`. Chris@87: Chris@87: See also Chris@87: -------- Chris@87: rfft : Compute the one-dimensional FFT for real input. Chris@87: ihfft : The inverse of `hfft`. Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: `hfft`/`ihfft` are a pair analogous to `rfft`/`irfft`, but for the Chris@87: opposite case: here the signal has Hermitian symmetry in the time domain Chris@87: and is real in the frequency domain. So here it's `hfft` for which Chris@87: you must supply the length of the result if it is to be odd: Chris@87: ``ihfft(hfft(a), len(a)) == a``, within numerical accuracy. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> signal = np.array([1, 2, 3, 4, 3, 2]) Chris@87: >>> np.fft.fft(signal) Chris@87: array([ 15.+0.j, -4.+0.j, 0.+0.j, -1.-0.j, 0.+0.j, -4.+0.j]) Chris@87: >>> np.fft.hfft(signal[:4]) # Input first half of signal Chris@87: array([ 15., -4., 0., -1., 0., -4.]) Chris@87: >>> np.fft.hfft(signal, 6) # Input entire signal and truncate Chris@87: array([ 15., -4., 0., -1., 0., -4.]) Chris@87: Chris@87: Chris@87: >>> signal = np.array([[1, 1.j], [-1.j, 2]]) Chris@87: >>> np.conj(signal.T) - signal # check Hermitian symmetry Chris@87: array([[ 0.-0.j, 0.+0.j], Chris@87: [ 0.+0.j, 0.-0.j]]) Chris@87: >>> freq_spectrum = np.fft.hfft(signal) Chris@87: >>> freq_spectrum Chris@87: array([[ 1., 1.], Chris@87: [ 2., -2.]]) Chris@87: Chris@87: """ Chris@87: Chris@87: a = asarray(a).astype(complex) Chris@87: if n is None: Chris@87: n = (shape(a)[axis] - 1) * 2 Chris@87: return irfft(conjugate(a), n, axis) * n Chris@87: Chris@87: Chris@87: def ihfft(a, n=None, axis=-1): Chris@87: """ Chris@87: Compute the inverse FFT of a signal which has Hermitian symmetry. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: a : array_like Chris@87: Input array. Chris@87: n : int, optional Chris@87: Length of the inverse FFT. Chris@87: Number of points along transformation axis in the input to use. Chris@87: If `n` is smaller than the length of the input, the input is cropped. Chris@87: If it is larger, the input is padded with zeros. If `n` is not given, Chris@87: the length of the input along the axis specified by `axis` is used. Chris@87: axis : int, optional Chris@87: Axis over which to compute the inverse FFT. If not given, the last Chris@87: axis is used. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : complex ndarray Chris@87: The truncated or zero-padded input, transformed along the axis Chris@87: indicated by `axis`, or the last one if `axis` is not specified. Chris@87: If `n` is even, the length of the transformed axis is ``(n/2)+1``. Chris@87: If `n` is odd, the length is ``(n+1)/2``. Chris@87: Chris@87: See also Chris@87: -------- Chris@87: hfft, irfft Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: `hfft`/`ihfft` are a pair analogous to `rfft`/`irfft`, but for the Chris@87: opposite case: here the signal has Hermitian symmetry in the time domain Chris@87: and is real in the frequency domain. So here it's `hfft` for which Chris@87: you must supply the length of the result if it is to be odd: Chris@87: ``ihfft(hfft(a), len(a)) == a``, within numerical accuracy. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> spectrum = np.array([ 15, -4, 0, -1, 0, -4]) Chris@87: >>> np.fft.ifft(spectrum) Chris@87: array([ 1.+0.j, 2.-0.j, 3.+0.j, 4.+0.j, 3.+0.j, 2.-0.j]) Chris@87: >>> np.fft.ihfft(spectrum) Chris@87: array([ 1.-0.j, 2.-0.j, 3.-0.j, 4.-0.j]) Chris@87: Chris@87: """ Chris@87: Chris@87: a = asarray(a).astype(float) Chris@87: if n is None: Chris@87: n = shape(a)[axis] Chris@87: return conjugate(rfft(a, n, axis))/n Chris@87: Chris@87: Chris@87: def _cook_nd_args(a, s=None, axes=None, invreal=0): Chris@87: if s is None: Chris@87: shapeless = 1 Chris@87: if axes is None: Chris@87: s = list(a.shape) Chris@87: else: Chris@87: s = take(a.shape, axes) Chris@87: else: Chris@87: shapeless = 0 Chris@87: s = list(s) Chris@87: if axes is None: Chris@87: axes = list(range(-len(s), 0)) Chris@87: if len(s) != len(axes): Chris@87: raise ValueError("Shape and axes have different lengths.") Chris@87: if invreal and shapeless: Chris@87: s[-1] = (a.shape[axes[-1]] - 1) * 2 Chris@87: return s, axes Chris@87: Chris@87: Chris@87: def _raw_fftnd(a, s=None, axes=None, function=fft): Chris@87: a = asarray(a) Chris@87: s, axes = _cook_nd_args(a, s, axes) Chris@87: itl = list(range(len(axes))) Chris@87: itl.reverse() Chris@87: for ii in itl: Chris@87: a = function(a, n=s[ii], axis=axes[ii]) Chris@87: return a Chris@87: Chris@87: Chris@87: def fftn(a, s=None, axes=None): Chris@87: """ Chris@87: Compute the N-dimensional discrete Fourier Transform. Chris@87: Chris@87: This function computes the *N*-dimensional discrete Fourier Transform over Chris@87: any number of axes in an *M*-dimensional array by means of the Fast Fourier Chris@87: Transform (FFT). Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: a : array_like Chris@87: Input array, can be complex. Chris@87: s : sequence of ints, optional Chris@87: Shape (length of each transformed axis) of the output Chris@87: (`s[0]` refers to axis 0, `s[1]` to axis 1, etc.). Chris@87: This corresponds to `n` for `fft(x, n)`. Chris@87: Along any axis, if the given shape is smaller than that of the input, Chris@87: the input is cropped. If it is larger, the input is padded with zeros. Chris@87: if `s` is not given, the shape of the input along the axes specified Chris@87: by `axes` is used. Chris@87: axes : sequence of ints, optional Chris@87: Axes over which to compute the FFT. If not given, the last ``len(s)`` Chris@87: axes are used, or all axes if `s` is also not specified. Chris@87: Repeated indices in `axes` means that the transform over that axis is Chris@87: performed multiple times. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : complex ndarray Chris@87: The truncated or zero-padded input, transformed along the axes Chris@87: indicated by `axes`, or by a combination of `s` and `a`, Chris@87: as explained in the parameters section above. Chris@87: Chris@87: Raises Chris@87: ------ Chris@87: ValueError Chris@87: If `s` and `axes` have different length. Chris@87: IndexError Chris@87: If an element of `axes` is larger than than the number of axes of `a`. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: numpy.fft : Overall view of discrete Fourier transforms, with definitions Chris@87: and conventions used. Chris@87: ifftn : The inverse of `fftn`, the inverse *n*-dimensional FFT. Chris@87: fft : The one-dimensional FFT, with definitions and conventions used. Chris@87: rfftn : The *n*-dimensional FFT of real input. Chris@87: fft2 : The two-dimensional FFT. Chris@87: fftshift : Shifts zero-frequency terms to centre of array Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: The output, analogously to `fft`, contains the term for zero frequency in Chris@87: the low-order corner of all axes, the positive frequency terms in the Chris@87: first half of all axes, the term for the Nyquist frequency in the middle Chris@87: of all axes and the negative frequency terms in the second half of all Chris@87: axes, in order of decreasingly negative frequency. Chris@87: Chris@87: See `numpy.fft` for details, definitions and conventions used. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> a = np.mgrid[:3, :3, :3][0] Chris@87: >>> np.fft.fftn(a, axes=(1, 2)) Chris@87: array([[[ 0.+0.j, 0.+0.j, 0.+0.j], Chris@87: [ 0.+0.j, 0.+0.j, 0.+0.j], Chris@87: [ 0.+0.j, 0.+0.j, 0.+0.j]], Chris@87: [[ 9.+0.j, 0.+0.j, 0.+0.j], Chris@87: [ 0.+0.j, 0.+0.j, 0.+0.j], Chris@87: [ 0.+0.j, 0.+0.j, 0.+0.j]], Chris@87: [[ 18.+0.j, 0.+0.j, 0.+0.j], Chris@87: [ 0.+0.j, 0.+0.j, 0.+0.j], Chris@87: [ 0.+0.j, 0.+0.j, 0.+0.j]]]) Chris@87: >>> np.fft.fftn(a, (2, 2), axes=(0, 1)) Chris@87: array([[[ 2.+0.j, 2.+0.j, 2.+0.j], Chris@87: [ 0.+0.j, 0.+0.j, 0.+0.j]], Chris@87: [[-2.+0.j, -2.+0.j, -2.+0.j], Chris@87: [ 0.+0.j, 0.+0.j, 0.+0.j]]]) Chris@87: Chris@87: >>> import matplotlib.pyplot as plt Chris@87: >>> [X, Y] = np.meshgrid(2 * np.pi * np.arange(200) / 12, Chris@87: ... 2 * np.pi * np.arange(200) / 34) Chris@87: >>> S = np.sin(X) + np.cos(Y) + np.random.uniform(0, 1, X.shape) Chris@87: >>> FS = np.fft.fftn(S) Chris@87: >>> plt.imshow(np.log(np.abs(np.fft.fftshift(FS))**2)) Chris@87: Chris@87: >>> plt.show() Chris@87: Chris@87: """ Chris@87: Chris@87: return _raw_fftnd(a, s, axes, fft) Chris@87: Chris@87: def ifftn(a, s=None, axes=None): Chris@87: """ Chris@87: Compute the N-dimensional inverse discrete Fourier Transform. Chris@87: Chris@87: This function computes the inverse of the N-dimensional discrete Chris@87: Fourier Transform over any number of axes in an M-dimensional array by Chris@87: means of the Fast Fourier Transform (FFT). In other words, Chris@87: ``ifftn(fftn(a)) == a`` to within numerical accuracy. Chris@87: For a description of the definitions and conventions used, see `numpy.fft`. Chris@87: Chris@87: The input, analogously to `ifft`, should be ordered in the same way as is Chris@87: returned by `fftn`, i.e. it should have the term for zero frequency Chris@87: in all axes in the low-order corner, the positive frequency terms in the Chris@87: first half of all axes, the term for the Nyquist frequency in the middle Chris@87: of all axes and the negative frequency terms in the second half of all Chris@87: axes, in order of decreasingly negative frequency. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: a : array_like Chris@87: Input array, can be complex. Chris@87: s : sequence of ints, optional Chris@87: Shape (length of each transformed axis) of the output Chris@87: (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.). Chris@87: This corresponds to ``n`` for ``ifft(x, n)``. Chris@87: Along any axis, if the given shape is smaller than that of the input, Chris@87: the input is cropped. If it is larger, the input is padded with zeros. Chris@87: if `s` is not given, the shape of the input along the axes specified Chris@87: by `axes` is used. See notes for issue on `ifft` zero padding. Chris@87: axes : sequence of ints, optional Chris@87: Axes over which to compute the IFFT. If not given, the last ``len(s)`` Chris@87: axes are used, or all axes if `s` is also not specified. Chris@87: Repeated indices in `axes` means that the inverse transform over that Chris@87: axis is performed multiple times. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : complex ndarray Chris@87: The truncated or zero-padded input, transformed along the axes Chris@87: indicated by `axes`, or by a combination of `s` or `a`, Chris@87: as explained in the parameters section above. Chris@87: Chris@87: Raises Chris@87: ------ Chris@87: ValueError Chris@87: If `s` and `axes` have different length. Chris@87: IndexError Chris@87: If an element of `axes` is larger than than the number of axes of `a`. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: numpy.fft : Overall view of discrete Fourier transforms, with definitions Chris@87: and conventions used. Chris@87: fftn : The forward *n*-dimensional FFT, of which `ifftn` is the inverse. Chris@87: ifft : The one-dimensional inverse FFT. Chris@87: ifft2 : The two-dimensional inverse FFT. Chris@87: ifftshift : Undoes `fftshift`, shifts zero-frequency terms to beginning Chris@87: of array. Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: See `numpy.fft` for definitions and conventions used. Chris@87: Chris@87: Zero-padding, analogously with `ifft`, is performed by appending zeros to Chris@87: the input along the specified dimension. Although this is the common Chris@87: approach, it might lead to surprising results. If another form of zero Chris@87: padding is desired, it must be performed before `ifftn` is called. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> a = np.eye(4) Chris@87: >>> np.fft.ifftn(np.fft.fftn(a, axes=(0,)), axes=(1,)) Chris@87: array([[ 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], Chris@87: [ 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j], Chris@87: [ 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j], Chris@87: [ 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j]]) Chris@87: Chris@87: Chris@87: Create and plot an image with band-limited frequency content: Chris@87: Chris@87: >>> import matplotlib.pyplot as plt Chris@87: >>> n = np.zeros((200,200), dtype=complex) Chris@87: >>> n[60:80, 20:40] = np.exp(1j*np.random.uniform(0, 2*np.pi, (20, 20))) Chris@87: >>> im = np.fft.ifftn(n).real Chris@87: >>> plt.imshow(im) Chris@87: Chris@87: >>> plt.show() Chris@87: Chris@87: """ Chris@87: Chris@87: return _raw_fftnd(a, s, axes, ifft) Chris@87: Chris@87: Chris@87: def fft2(a, s=None, axes=(-2, -1)): Chris@87: """ Chris@87: Compute the 2-dimensional discrete Fourier Transform Chris@87: Chris@87: This function computes the *n*-dimensional discrete Fourier Transform Chris@87: over any axes in an *M*-dimensional array by means of the Chris@87: Fast Fourier Transform (FFT). By default, the transform is computed over Chris@87: the last two axes of the input array, i.e., a 2-dimensional FFT. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: a : array_like Chris@87: Input array, can be complex Chris@87: s : sequence of ints, optional Chris@87: Shape (length of each transformed axis) of the output Chris@87: (`s[0]` refers to axis 0, `s[1]` to axis 1, etc.). Chris@87: This corresponds to `n` for `fft(x, n)`. Chris@87: Along each axis, if the given shape is smaller than that of the input, Chris@87: the input is cropped. If it is larger, the input is padded with zeros. Chris@87: if `s` is not given, the shape of the input along the axes specified Chris@87: by `axes` is used. Chris@87: axes : sequence of ints, optional Chris@87: Axes over which to compute the FFT. If not given, the last two Chris@87: axes are used. A repeated index in `axes` means the transform over Chris@87: that axis is performed multiple times. A one-element sequence means Chris@87: that a one-dimensional FFT is performed. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : complex ndarray Chris@87: The truncated or zero-padded input, transformed along the axes Chris@87: indicated by `axes`, or the last two axes if `axes` is not given. Chris@87: Chris@87: Raises Chris@87: ------ Chris@87: ValueError Chris@87: If `s` and `axes` have different length, or `axes` not given and Chris@87: ``len(s) != 2``. Chris@87: IndexError Chris@87: If an element of `axes` is larger than than the number of axes of `a`. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: numpy.fft : Overall view of discrete Fourier transforms, with definitions Chris@87: and conventions used. Chris@87: ifft2 : The inverse two-dimensional FFT. Chris@87: fft : The one-dimensional FFT. Chris@87: fftn : The *n*-dimensional FFT. Chris@87: fftshift : Shifts zero-frequency terms to the center of the array. Chris@87: For two-dimensional input, swaps first and third quadrants, and second Chris@87: and fourth quadrants. Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: `fft2` is just `fftn` with a different default for `axes`. Chris@87: Chris@87: The output, analogously to `fft`, contains the term for zero frequency in Chris@87: the low-order corner of the transformed axes, the positive frequency terms Chris@87: in the first half of these axes, the term for the Nyquist frequency in the Chris@87: middle of the axes and the negative frequency terms in the second half of Chris@87: the axes, in order of decreasingly negative frequency. Chris@87: Chris@87: See `fftn` for details and a plotting example, and `numpy.fft` for Chris@87: definitions and conventions used. Chris@87: Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> a = np.mgrid[:5, :5][0] Chris@87: >>> np.fft.fft2(a) Chris@87: array([[ 50.0 +0.j , 0.0 +0.j , 0.0 +0.j , Chris@87: 0.0 +0.j , 0.0 +0.j ], Chris@87: [-12.5+17.20477401j, 0.0 +0.j , 0.0 +0.j , Chris@87: 0.0 +0.j , 0.0 +0.j ], Chris@87: [-12.5 +4.0614962j , 0.0 +0.j , 0.0 +0.j , Chris@87: 0.0 +0.j , 0.0 +0.j ], Chris@87: [-12.5 -4.0614962j , 0.0 +0.j , 0.0 +0.j , Chris@87: 0.0 +0.j , 0.0 +0.j ], Chris@87: [-12.5-17.20477401j, 0.0 +0.j , 0.0 +0.j , Chris@87: 0.0 +0.j , 0.0 +0.j ]]) Chris@87: Chris@87: """ Chris@87: Chris@87: return _raw_fftnd(a, s, axes, fft) Chris@87: Chris@87: Chris@87: def ifft2(a, s=None, axes=(-2, -1)): Chris@87: """ Chris@87: Compute the 2-dimensional inverse discrete Fourier Transform. Chris@87: Chris@87: This function computes the inverse of the 2-dimensional discrete Fourier Chris@87: Transform over any number of axes in an M-dimensional array by means of Chris@87: the Fast Fourier Transform (FFT). In other words, ``ifft2(fft2(a)) == a`` Chris@87: to within numerical accuracy. By default, the inverse transform is Chris@87: computed over the last two axes of the input array. Chris@87: Chris@87: The input, analogously to `ifft`, should be ordered in the same way as is Chris@87: returned by `fft2`, i.e. it should have the term for zero frequency Chris@87: in the low-order corner of the two axes, the positive frequency terms in Chris@87: the first half of these axes, the term for the Nyquist frequency in the Chris@87: middle of the axes and the negative frequency terms in the second half of Chris@87: both axes, in order of decreasingly negative frequency. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: a : array_like Chris@87: Input array, can be complex. Chris@87: s : sequence of ints, optional Chris@87: Shape (length of each axis) of the output (``s[0]`` refers to axis 0, Chris@87: ``s[1]`` to axis 1, etc.). This corresponds to `n` for ``ifft(x, n)``. Chris@87: Along each axis, if the given shape is smaller than that of the input, Chris@87: the input is cropped. If it is larger, the input is padded with zeros. Chris@87: if `s` is not given, the shape of the input along the axes specified Chris@87: by `axes` is used. See notes for issue on `ifft` zero padding. Chris@87: axes : sequence of ints, optional Chris@87: Axes over which to compute the FFT. If not given, the last two Chris@87: axes are used. A repeated index in `axes` means the transform over Chris@87: that axis is performed multiple times. A one-element sequence means Chris@87: that a one-dimensional FFT is performed. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : complex ndarray Chris@87: The truncated or zero-padded input, transformed along the axes Chris@87: indicated by `axes`, or the last two axes if `axes` is not given. Chris@87: Chris@87: Raises Chris@87: ------ Chris@87: ValueError Chris@87: If `s` and `axes` have different length, or `axes` not given and Chris@87: ``len(s) != 2``. Chris@87: IndexError Chris@87: If an element of `axes` is larger than than the number of axes of `a`. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: numpy.fft : Overall view of discrete Fourier transforms, with definitions Chris@87: and conventions used. Chris@87: fft2 : The forward 2-dimensional FFT, of which `ifft2` is the inverse. Chris@87: ifftn : The inverse of the *n*-dimensional FFT. Chris@87: fft : The one-dimensional FFT. Chris@87: ifft : The one-dimensional inverse FFT. Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: `ifft2` is just `ifftn` with a different default for `axes`. Chris@87: Chris@87: See `ifftn` for details and a plotting example, and `numpy.fft` for Chris@87: definition and conventions used. Chris@87: Chris@87: Zero-padding, analogously with `ifft`, is performed by appending zeros to Chris@87: the input along the specified dimension. Although this is the common Chris@87: approach, it might lead to surprising results. If another form of zero Chris@87: padding is desired, it must be performed before `ifft2` is called. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> a = 4 * np.eye(4) Chris@87: >>> np.fft.ifft2(a) Chris@87: array([[ 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], Chris@87: [ 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j], Chris@87: [ 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j], Chris@87: [ 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j]]) Chris@87: Chris@87: """ Chris@87: Chris@87: return _raw_fftnd(a, s, axes, ifft) Chris@87: Chris@87: Chris@87: def rfftn(a, s=None, axes=None): Chris@87: """ Chris@87: Compute the N-dimensional discrete Fourier Transform for real input. Chris@87: Chris@87: This function computes the N-dimensional discrete Fourier Transform over Chris@87: any number of axes in an M-dimensional real array by means of the Fast Chris@87: Fourier Transform (FFT). By default, all axes are transformed, with the Chris@87: real transform performed over the last axis, while the remaining Chris@87: transforms are complex. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: a : array_like Chris@87: Input array, taken to be real. Chris@87: s : sequence of ints, optional Chris@87: Shape (length along each transformed axis) to use from the input. Chris@87: (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.). Chris@87: The final element of `s` corresponds to `n` for ``rfft(x, n)``, while Chris@87: for the remaining axes, it corresponds to `n` for ``fft(x, n)``. Chris@87: Along any axis, if the given shape is smaller than that of the input, Chris@87: the input is cropped. If it is larger, the input is padded with zeros. Chris@87: if `s` is not given, the shape of the input along the axes specified Chris@87: by `axes` is used. Chris@87: axes : sequence of ints, optional Chris@87: Axes over which to compute the FFT. If not given, the last ``len(s)`` Chris@87: axes are used, or all axes if `s` is also not specified. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : complex ndarray Chris@87: The truncated or zero-padded input, transformed along the axes Chris@87: indicated by `axes`, or by a combination of `s` and `a`, Chris@87: as explained in the parameters section above. Chris@87: The length of the last axis transformed will be ``s[-1]//2+1``, Chris@87: while the remaining transformed axes will have lengths according to Chris@87: `s`, or unchanged from the input. Chris@87: Chris@87: Raises Chris@87: ------ Chris@87: ValueError Chris@87: If `s` and `axes` have different length. Chris@87: IndexError Chris@87: If an element of `axes` is larger than than the number of axes of `a`. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: irfftn : The inverse of `rfftn`, i.e. the inverse of the n-dimensional FFT Chris@87: of real input. Chris@87: fft : The one-dimensional FFT, with definitions and conventions used. Chris@87: rfft : The one-dimensional FFT of real input. Chris@87: fftn : The n-dimensional FFT. Chris@87: rfft2 : The two-dimensional FFT of real input. Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: The transform for real input is performed over the last transformation Chris@87: axis, as by `rfft`, then the transform over the remaining axes is Chris@87: performed as by `fftn`. The order of the output is as for `rfft` for the Chris@87: final transformation axis, and as for `fftn` for the remaining Chris@87: transformation axes. Chris@87: Chris@87: See `fft` for details, definitions and conventions used. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> a = np.ones((2, 2, 2)) Chris@87: >>> np.fft.rfftn(a) Chris@87: array([[[ 8.+0.j, 0.+0.j], Chris@87: [ 0.+0.j, 0.+0.j]], Chris@87: [[ 0.+0.j, 0.+0.j], Chris@87: [ 0.+0.j, 0.+0.j]]]) Chris@87: Chris@87: >>> np.fft.rfftn(a, axes=(2, 0)) Chris@87: array([[[ 4.+0.j, 0.+0.j], Chris@87: [ 4.+0.j, 0.+0.j]], Chris@87: [[ 0.+0.j, 0.+0.j], Chris@87: [ 0.+0.j, 0.+0.j]]]) Chris@87: Chris@87: """ Chris@87: Chris@87: a = asarray(a).astype(float) Chris@87: s, axes = _cook_nd_args(a, s, axes) Chris@87: a = rfft(a, s[-1], axes[-1]) Chris@87: for ii in range(len(axes)-1): Chris@87: a = fft(a, s[ii], axes[ii]) Chris@87: return a Chris@87: Chris@87: def rfft2(a, s=None, axes=(-2, -1)): Chris@87: """ Chris@87: Compute the 2-dimensional FFT of a real array. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: a : array Chris@87: Input array, taken to be real. Chris@87: s : sequence of ints, optional Chris@87: Shape of the FFT. Chris@87: axes : sequence of ints, optional Chris@87: Axes over which to compute the FFT. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : ndarray Chris@87: The result of the real 2-D FFT. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: rfftn : Compute the N-dimensional discrete Fourier Transform for real Chris@87: input. Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: This is really just `rfftn` with different default behavior. Chris@87: For more details see `rfftn`. Chris@87: Chris@87: """ Chris@87: Chris@87: return rfftn(a, s, axes) Chris@87: Chris@87: def irfftn(a, s=None, axes=None): Chris@87: """ Chris@87: Compute the inverse of the N-dimensional FFT of real input. Chris@87: Chris@87: This function computes the inverse of the N-dimensional discrete Chris@87: Fourier Transform for real input over any number of axes in an Chris@87: M-dimensional array by means of the Fast Fourier Transform (FFT). In Chris@87: other words, ``irfftn(rfftn(a), a.shape) == a`` to within numerical Chris@87: accuracy. (The ``a.shape`` is necessary like ``len(a)`` is for `irfft`, Chris@87: and for the same reason.) Chris@87: Chris@87: The input should be ordered in the same way as is returned by `rfftn`, Chris@87: i.e. as for `irfft` for the final transformation axis, and as for `ifftn` Chris@87: along all the other axes. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: a : array_like Chris@87: Input array. Chris@87: s : sequence of ints, optional Chris@87: Shape (length of each transformed axis) of the output Chris@87: (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.). `s` is also the Chris@87: number of input points used along this axis, except for the last axis, Chris@87: where ``s[-1]//2+1`` points of the input are used. Chris@87: Along any axis, if the shape indicated by `s` is smaller than that of Chris@87: the input, the input is cropped. If it is larger, the input is padded Chris@87: with zeros. If `s` is not given, the shape of the input along the Chris@87: axes specified by `axes` is used. Chris@87: axes : sequence of ints, optional Chris@87: Axes over which to compute the inverse FFT. If not given, the last Chris@87: `len(s)` axes are used, or all axes if `s` is also not specified. Chris@87: Repeated indices in `axes` means that the inverse transform over that Chris@87: axis is performed multiple times. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : ndarray Chris@87: The truncated or zero-padded input, transformed along the axes Chris@87: indicated by `axes`, or by a combination of `s` or `a`, Chris@87: as explained in the parameters section above. Chris@87: The length of each transformed axis is as given by the corresponding Chris@87: element of `s`, or the length of the input in every axis except for the Chris@87: last one if `s` is not given. In the final transformed axis the length Chris@87: of the output when `s` is not given is ``2*(m-1)`` where ``m`` is the Chris@87: length of the final transformed axis of the input. To get an odd Chris@87: number of output points in the final axis, `s` must be specified. Chris@87: Chris@87: Raises Chris@87: ------ Chris@87: ValueError Chris@87: If `s` and `axes` have different length. Chris@87: IndexError Chris@87: If an element of `axes` is larger than than the number of axes of `a`. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: rfftn : The forward n-dimensional FFT of real input, Chris@87: of which `ifftn` is the inverse. Chris@87: fft : The one-dimensional FFT, with definitions and conventions used. Chris@87: irfft : The inverse of the one-dimensional FFT of real input. Chris@87: irfft2 : The inverse of the two-dimensional FFT of real input. Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: See `fft` for definitions and conventions used. Chris@87: Chris@87: See `rfft` for definitions and conventions used for real input. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> a = np.zeros((3, 2, 2)) Chris@87: >>> a[0, 0, 0] = 3 * 2 * 2 Chris@87: >>> np.fft.irfftn(a) Chris@87: array([[[ 1., 1.], Chris@87: [ 1., 1.]], Chris@87: [[ 1., 1.], Chris@87: [ 1., 1.]], Chris@87: [[ 1., 1.], Chris@87: [ 1., 1.]]]) Chris@87: Chris@87: """ Chris@87: Chris@87: a = asarray(a).astype(complex) Chris@87: s, axes = _cook_nd_args(a, s, axes, invreal=1) Chris@87: for ii in range(len(axes)-1): Chris@87: a = ifft(a, s[ii], axes[ii]) Chris@87: a = irfft(a, s[-1], axes[-1]) Chris@87: return a Chris@87: Chris@87: def irfft2(a, s=None, axes=(-2, -1)): Chris@87: """ Chris@87: Compute the 2-dimensional inverse FFT of a real array. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: a : array_like Chris@87: The input array Chris@87: s : sequence of ints, optional Chris@87: Shape of the inverse FFT. Chris@87: axes : sequence of ints, optional Chris@87: The axes over which to compute the inverse fft. Chris@87: Default is the last two axes. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : ndarray Chris@87: The result of the inverse real 2-D FFT. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: irfftn : Compute the inverse of the N-dimensional FFT of real input. Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: This is really `irfftn` with different defaults. Chris@87: For more details see `irfftn`. Chris@87: Chris@87: """ Chris@87: Chris@87: return irfftn(a, s, axes)