annotate DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/fft/fftpack.py @ 118:770eb830ec19 emscripten

Typo fix
author Chris Cannam
date Wed, 18 May 2016 16:14:08 +0100
parents 2a2c65a20a8b
children
rev   line source
Chris@87 1 """
Chris@87 2 Discrete Fourier Transforms
Chris@87 3
Chris@87 4 Routines in this module:
Chris@87 5
Chris@87 6 fft(a, n=None, axis=-1)
Chris@87 7 ifft(a, n=None, axis=-1)
Chris@87 8 rfft(a, n=None, axis=-1)
Chris@87 9 irfft(a, n=None, axis=-1)
Chris@87 10 hfft(a, n=None, axis=-1)
Chris@87 11 ihfft(a, n=None, axis=-1)
Chris@87 12 fftn(a, s=None, axes=None)
Chris@87 13 ifftn(a, s=None, axes=None)
Chris@87 14 rfftn(a, s=None, axes=None)
Chris@87 15 irfftn(a, s=None, axes=None)
Chris@87 16 fft2(a, s=None, axes=(-2,-1))
Chris@87 17 ifft2(a, s=None, axes=(-2, -1))
Chris@87 18 rfft2(a, s=None, axes=(-2,-1))
Chris@87 19 irfft2(a, s=None, axes=(-2, -1))
Chris@87 20
Chris@87 21 i = inverse transform
Chris@87 22 r = transform of purely real data
Chris@87 23 h = Hermite transform
Chris@87 24 n = n-dimensional transform
Chris@87 25 2 = 2-dimensional transform
Chris@87 26 (Note: 2D routines are just nD routines with different default
Chris@87 27 behavior.)
Chris@87 28
Chris@87 29 The underlying code for these functions is an f2c-translated and modified
Chris@87 30 version of the FFTPACK routines.
Chris@87 31
Chris@87 32 """
Chris@87 33 from __future__ import division, absolute_import, print_function
Chris@87 34
Chris@87 35 __all__ = ['fft', 'ifft', 'rfft', 'irfft', 'hfft', 'ihfft', 'rfftn',
Chris@87 36 'irfftn', 'rfft2', 'irfft2', 'fft2', 'ifft2', 'fftn', 'ifftn']
Chris@87 37
Chris@87 38 from numpy.core import asarray, zeros, swapaxes, shape, conjugate, \
Chris@87 39 take
Chris@87 40 from . import fftpack_lite as fftpack
Chris@87 41
Chris@87 42 _fft_cache = {}
Chris@87 43 _real_fft_cache = {}
Chris@87 44
Chris@87 45 def _raw_fft(a, n=None, axis=-1, init_function=fftpack.cffti,
Chris@87 46 work_function=fftpack.cfftf, fft_cache = _fft_cache ):
Chris@87 47 a = asarray(a)
Chris@87 48
Chris@87 49 if n is None:
Chris@87 50 n = a.shape[axis]
Chris@87 51
Chris@87 52 if n < 1:
Chris@87 53 raise ValueError("Invalid number of FFT data points (%d) specified." % n)
Chris@87 54
Chris@87 55 try:
Chris@87 56 # Thread-safety note: We rely on list.pop() here to atomically
Chris@87 57 # retrieve-and-remove a wsave from the cache. This ensures that no
Chris@87 58 # other thread can get the same wsave while we're using it.
Chris@87 59 wsave = fft_cache.setdefault(n, []).pop()
Chris@87 60 except (IndexError):
Chris@87 61 wsave = init_function(n)
Chris@87 62
Chris@87 63 if a.shape[axis] != n:
Chris@87 64 s = list(a.shape)
Chris@87 65 if s[axis] > n:
Chris@87 66 index = [slice(None)]*len(s)
Chris@87 67 index[axis] = slice(0, n)
Chris@87 68 a = a[index]
Chris@87 69 else:
Chris@87 70 index = [slice(None)]*len(s)
Chris@87 71 index[axis] = slice(0, s[axis])
Chris@87 72 s[axis] = n
Chris@87 73 z = zeros(s, a.dtype.char)
Chris@87 74 z[index] = a
Chris@87 75 a = z
Chris@87 76
Chris@87 77 if axis != -1:
Chris@87 78 a = swapaxes(a, axis, -1)
Chris@87 79 r = work_function(a, wsave)
Chris@87 80 if axis != -1:
Chris@87 81 r = swapaxes(r, axis, -1)
Chris@87 82
Chris@87 83 # As soon as we put wsave back into the cache, another thread could pick it
Chris@87 84 # up and start using it, so we must not do this until after we're
Chris@87 85 # completely done using it ourselves.
Chris@87 86 fft_cache[n].append(wsave)
Chris@87 87
Chris@87 88 return r
Chris@87 89
Chris@87 90
Chris@87 91 def fft(a, n=None, axis=-1):
Chris@87 92 """
Chris@87 93 Compute the one-dimensional discrete Fourier Transform.
Chris@87 94
Chris@87 95 This function computes the one-dimensional *n*-point discrete Fourier
Chris@87 96 Transform (DFT) with the efficient Fast Fourier Transform (FFT)
Chris@87 97 algorithm [CT].
Chris@87 98
Chris@87 99 Parameters
Chris@87 100 ----------
Chris@87 101 a : array_like
Chris@87 102 Input array, can be complex.
Chris@87 103 n : int, optional
Chris@87 104 Length of the transformed axis of the output.
Chris@87 105 If `n` is smaller than the length of the input, the input is cropped.
Chris@87 106 If it is larger, the input is padded with zeros. If `n` is not given,
Chris@87 107 the length of the input along the axis specified by `axis` is used.
Chris@87 108 axis : int, optional
Chris@87 109 Axis over which to compute the FFT. If not given, the last axis is
Chris@87 110 used.
Chris@87 111
Chris@87 112 Returns
Chris@87 113 -------
Chris@87 114 out : complex ndarray
Chris@87 115 The truncated or zero-padded input, transformed along the axis
Chris@87 116 indicated by `axis`, or the last one if `axis` is not specified.
Chris@87 117
Chris@87 118 Raises
Chris@87 119 ------
Chris@87 120 IndexError
Chris@87 121 if `axes` is larger than the last axis of `a`.
Chris@87 122
Chris@87 123 See Also
Chris@87 124 --------
Chris@87 125 numpy.fft : for definition of the DFT and conventions used.
Chris@87 126 ifft : The inverse of `fft`.
Chris@87 127 fft2 : The two-dimensional FFT.
Chris@87 128 fftn : The *n*-dimensional FFT.
Chris@87 129 rfftn : The *n*-dimensional FFT of real input.
Chris@87 130 fftfreq : Frequency bins for given FFT parameters.
Chris@87 131
Chris@87 132 Notes
Chris@87 133 -----
Chris@87 134 FFT (Fast Fourier Transform) refers to a way the discrete Fourier
Chris@87 135 Transform (DFT) can be calculated efficiently, by using symmetries in the
Chris@87 136 calculated terms. The symmetry is highest when `n` is a power of 2, and
Chris@87 137 the transform is therefore most efficient for these sizes.
Chris@87 138
Chris@87 139 The DFT is defined, with the conventions used in this implementation, in
Chris@87 140 the documentation for the `numpy.fft` module.
Chris@87 141
Chris@87 142 References
Chris@87 143 ----------
Chris@87 144 .. [CT] Cooley, James W., and John W. Tukey, 1965, "An algorithm for the
Chris@87 145 machine calculation of complex Fourier series," *Math. Comput.*
Chris@87 146 19: 297-301.
Chris@87 147
Chris@87 148 Examples
Chris@87 149 --------
Chris@87 150 >>> np.fft.fft(np.exp(2j * np.pi * np.arange(8) / 8))
Chris@87 151 array([ -3.44505240e-16 +1.14383329e-17j,
Chris@87 152 8.00000000e+00 -5.71092652e-15j,
Chris@87 153 2.33482938e-16 +1.22460635e-16j,
Chris@87 154 1.64863782e-15 +1.77635684e-15j,
Chris@87 155 9.95839695e-17 +2.33482938e-16j,
Chris@87 156 0.00000000e+00 +1.66837030e-15j,
Chris@87 157 1.14383329e-17 +1.22460635e-16j,
Chris@87 158 -1.64863782e-15 +1.77635684e-15j])
Chris@87 159
Chris@87 160 >>> import matplotlib.pyplot as plt
Chris@87 161 >>> t = np.arange(256)
Chris@87 162 >>> sp = np.fft.fft(np.sin(t))
Chris@87 163 >>> freq = np.fft.fftfreq(t.shape[-1])
Chris@87 164 >>> plt.plot(freq, sp.real, freq, sp.imag)
Chris@87 165 [<matplotlib.lines.Line2D object at 0x...>, <matplotlib.lines.Line2D object at 0x...>]
Chris@87 166 >>> plt.show()
Chris@87 167
Chris@87 168 In this example, real input has an FFT which is Hermitian, i.e., symmetric
Chris@87 169 in the real part and anti-symmetric in the imaginary part, as described in
Chris@87 170 the `numpy.fft` documentation.
Chris@87 171
Chris@87 172 """
Chris@87 173
Chris@87 174 return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftf, _fft_cache)
Chris@87 175
Chris@87 176
Chris@87 177 def ifft(a, n=None, axis=-1):
Chris@87 178 """
Chris@87 179 Compute the one-dimensional inverse discrete Fourier Transform.
Chris@87 180
Chris@87 181 This function computes the inverse of the one-dimensional *n*-point
Chris@87 182 discrete Fourier transform computed by `fft`. In other words,
Chris@87 183 ``ifft(fft(a)) == a`` to within numerical accuracy.
Chris@87 184 For a general description of the algorithm and definitions,
Chris@87 185 see `numpy.fft`.
Chris@87 186
Chris@87 187 The input should be ordered in the same way as is returned by `fft`,
Chris@87 188 i.e., ``a[0]`` should contain the zero frequency term,
Chris@87 189 ``a[1:n/2+1]`` should contain the positive-frequency terms, and
Chris@87 190 ``a[n/2+1:]`` should contain the negative-frequency terms, in order of
Chris@87 191 decreasingly negative frequency. See `numpy.fft` for details.
Chris@87 192
Chris@87 193 Parameters
Chris@87 194 ----------
Chris@87 195 a : array_like
Chris@87 196 Input array, can be complex.
Chris@87 197 n : int, optional
Chris@87 198 Length of the transformed axis of the output.
Chris@87 199 If `n` is smaller than the length of the input, the input is cropped.
Chris@87 200 If it is larger, the input is padded with zeros. If `n` is not given,
Chris@87 201 the length of the input along the axis specified by `axis` is used.
Chris@87 202 See notes about padding issues.
Chris@87 203 axis : int, optional
Chris@87 204 Axis over which to compute the inverse DFT. If not given, the last
Chris@87 205 axis is used.
Chris@87 206
Chris@87 207 Returns
Chris@87 208 -------
Chris@87 209 out : complex ndarray
Chris@87 210 The truncated or zero-padded input, transformed along the axis
Chris@87 211 indicated by `axis`, or the last one if `axis` is not specified.
Chris@87 212
Chris@87 213 Raises
Chris@87 214 ------
Chris@87 215 IndexError
Chris@87 216 If `axes` is larger than the last axis of `a`.
Chris@87 217
Chris@87 218 See Also
Chris@87 219 --------
Chris@87 220 numpy.fft : An introduction, with definitions and general explanations.
Chris@87 221 fft : The one-dimensional (forward) FFT, of which `ifft` is the inverse
Chris@87 222 ifft2 : The two-dimensional inverse FFT.
Chris@87 223 ifftn : The n-dimensional inverse FFT.
Chris@87 224
Chris@87 225 Notes
Chris@87 226 -----
Chris@87 227 If the input parameter `n` is larger than the size of the input, the input
Chris@87 228 is padded by appending zeros at the end. Even though this is the common
Chris@87 229 approach, it might lead to surprising results. If a different padding is
Chris@87 230 desired, it must be performed before calling `ifft`.
Chris@87 231
Chris@87 232 Examples
Chris@87 233 --------
Chris@87 234 >>> np.fft.ifft([0, 4, 0, 0])
Chris@87 235 array([ 1.+0.j, 0.+1.j, -1.+0.j, 0.-1.j])
Chris@87 236
Chris@87 237 Create and plot a band-limited signal with random phases:
Chris@87 238
Chris@87 239 >>> import matplotlib.pyplot as plt
Chris@87 240 >>> t = np.arange(400)
Chris@87 241 >>> n = np.zeros((400,), dtype=complex)
Chris@87 242 >>> n[40:60] = np.exp(1j*np.random.uniform(0, 2*np.pi, (20,)))
Chris@87 243 >>> s = np.fft.ifft(n)
Chris@87 244 >>> plt.plot(t, s.real, 'b-', t, s.imag, 'r--')
Chris@87 245 [<matplotlib.lines.Line2D object at 0x...>, <matplotlib.lines.Line2D object at 0x...>]
Chris@87 246 >>> plt.legend(('real', 'imaginary'))
Chris@87 247 <matplotlib.legend.Legend object at 0x...>
Chris@87 248 >>> plt.show()
Chris@87 249
Chris@87 250 """
Chris@87 251
Chris@87 252 a = asarray(a).astype(complex)
Chris@87 253 if n is None:
Chris@87 254 n = shape(a)[axis]
Chris@87 255 return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftb, _fft_cache) / n
Chris@87 256
Chris@87 257
Chris@87 258 def rfft(a, n=None, axis=-1):
Chris@87 259 """
Chris@87 260 Compute the one-dimensional discrete Fourier Transform for real input.
Chris@87 261
Chris@87 262 This function computes the one-dimensional *n*-point discrete Fourier
Chris@87 263 Transform (DFT) of a real-valued array by means of an efficient algorithm
Chris@87 264 called the Fast Fourier Transform (FFT).
Chris@87 265
Chris@87 266 Parameters
Chris@87 267 ----------
Chris@87 268 a : array_like
Chris@87 269 Input array
Chris@87 270 n : int, optional
Chris@87 271 Number of points along transformation axis in the input to use.
Chris@87 272 If `n` is smaller than the length of the input, the input is cropped.
Chris@87 273 If it is larger, the input is padded with zeros. If `n` is not given,
Chris@87 274 the length of the input along the axis specified by `axis` is used.
Chris@87 275 axis : int, optional
Chris@87 276 Axis over which to compute the FFT. If not given, the last axis is
Chris@87 277 used.
Chris@87 278
Chris@87 279 Returns
Chris@87 280 -------
Chris@87 281 out : complex ndarray
Chris@87 282 The truncated or zero-padded input, transformed along the axis
Chris@87 283 indicated by `axis`, or the last one if `axis` is not specified.
Chris@87 284 If `n` is even, the length of the transformed axis is ``(n/2)+1``.
Chris@87 285 If `n` is odd, the length is ``(n+1)/2``.
Chris@87 286
Chris@87 287 Raises
Chris@87 288 ------
Chris@87 289 IndexError
Chris@87 290 If `axis` is larger than the last axis of `a`.
Chris@87 291
Chris@87 292 See Also
Chris@87 293 --------
Chris@87 294 numpy.fft : For definition of the DFT and conventions used.
Chris@87 295 irfft : The inverse of `rfft`.
Chris@87 296 fft : The one-dimensional FFT of general (complex) input.
Chris@87 297 fftn : The *n*-dimensional FFT.
Chris@87 298 rfftn : The *n*-dimensional FFT of real input.
Chris@87 299
Chris@87 300 Notes
Chris@87 301 -----
Chris@87 302 When the DFT is computed for purely real input, the output is
Chris@87 303 Hermitian-symmetric, i.e. the negative frequency terms are just the complex
Chris@87 304 conjugates of the corresponding positive-frequency terms, and the
Chris@87 305 negative-frequency terms are therefore redundant. This function does not
Chris@87 306 compute the negative frequency terms, and the length of the transformed
Chris@87 307 axis of the output is therefore ``n//2 + 1``.
Chris@87 308
Chris@87 309 When ``A = rfft(a)`` and fs is the sampling frequency, ``A[0]`` contains
Chris@87 310 the zero-frequency term 0*fs, which is real due to Hermitian symmetry.
Chris@87 311
Chris@87 312 If `n` is even, ``A[-1]`` contains the term representing both positive
Chris@87 313 and negative Nyquist frequency (+fs/2 and -fs/2), and must also be purely
Chris@87 314 real. If `n` is odd, there is no term at fs/2; ``A[-1]`` contains
Chris@87 315 the largest positive frequency (fs/2*(n-1)/n), and is complex in the
Chris@87 316 general case.
Chris@87 317
Chris@87 318 If the input `a` contains an imaginary part, it is silently discarded.
Chris@87 319
Chris@87 320 Examples
Chris@87 321 --------
Chris@87 322 >>> np.fft.fft([0, 1, 0, 0])
Chris@87 323 array([ 1.+0.j, 0.-1.j, -1.+0.j, 0.+1.j])
Chris@87 324 >>> np.fft.rfft([0, 1, 0, 0])
Chris@87 325 array([ 1.+0.j, 0.-1.j, -1.+0.j])
Chris@87 326
Chris@87 327 Notice how the final element of the `fft` output is the complex conjugate
Chris@87 328 of the second element, for real input. For `rfft`, this symmetry is
Chris@87 329 exploited to compute only the non-negative frequency terms.
Chris@87 330
Chris@87 331 """
Chris@87 332
Chris@87 333 a = asarray(a).astype(float)
Chris@87 334 return _raw_fft(a, n, axis, fftpack.rffti, fftpack.rfftf, _real_fft_cache)
Chris@87 335
Chris@87 336
Chris@87 337 def irfft(a, n=None, axis=-1):
Chris@87 338 """
Chris@87 339 Compute the inverse of the n-point DFT for real input.
Chris@87 340
Chris@87 341 This function computes the inverse of the one-dimensional *n*-point
Chris@87 342 discrete Fourier Transform of real input computed by `rfft`.
Chris@87 343 In other words, ``irfft(rfft(a), len(a)) == a`` to within numerical
Chris@87 344 accuracy. (See Notes below for why ``len(a)`` is necessary here.)
Chris@87 345
Chris@87 346 The input is expected to be in the form returned by `rfft`, i.e. the
Chris@87 347 real zero-frequency term followed by the complex positive frequency terms
Chris@87 348 in order of increasing frequency. Since the discrete Fourier Transform of
Chris@87 349 real input is Hermitian-symmetric, the negative frequency terms are taken
Chris@87 350 to be the complex conjugates of the corresponding positive frequency terms.
Chris@87 351
Chris@87 352 Parameters
Chris@87 353 ----------
Chris@87 354 a : array_like
Chris@87 355 The input array.
Chris@87 356 n : int, optional
Chris@87 357 Length of the transformed axis of the output.
Chris@87 358 For `n` output points, ``n//2+1`` input points are necessary. If the
Chris@87 359 input is longer than this, it is cropped. If it is shorter than this,
Chris@87 360 it is padded with zeros. If `n` is not given, it is determined from
Chris@87 361 the length of the input along the axis specified by `axis`.
Chris@87 362 axis : int, optional
Chris@87 363 Axis over which to compute the inverse FFT. If not given, the last
Chris@87 364 axis is used.
Chris@87 365
Chris@87 366 Returns
Chris@87 367 -------
Chris@87 368 out : ndarray
Chris@87 369 The truncated or zero-padded input, transformed along the axis
Chris@87 370 indicated by `axis`, or the last one if `axis` is not specified.
Chris@87 371 The length of the transformed axis is `n`, or, if `n` is not given,
Chris@87 372 ``2*(m-1)`` where ``m`` is the length of the transformed axis of the
Chris@87 373 input. To get an odd number of output points, `n` must be specified.
Chris@87 374
Chris@87 375 Raises
Chris@87 376 ------
Chris@87 377 IndexError
Chris@87 378 If `axis` is larger than the last axis of `a`.
Chris@87 379
Chris@87 380 See Also
Chris@87 381 --------
Chris@87 382 numpy.fft : For definition of the DFT and conventions used.
Chris@87 383 rfft : The one-dimensional FFT of real input, of which `irfft` is inverse.
Chris@87 384 fft : The one-dimensional FFT.
Chris@87 385 irfft2 : The inverse of the two-dimensional FFT of real input.
Chris@87 386 irfftn : The inverse of the *n*-dimensional FFT of real input.
Chris@87 387
Chris@87 388 Notes
Chris@87 389 -----
Chris@87 390 Returns the real valued `n`-point inverse discrete Fourier transform
Chris@87 391 of `a`, where `a` contains the non-negative frequency terms of a
Chris@87 392 Hermitian-symmetric sequence. `n` is the length of the result, not the
Chris@87 393 input.
Chris@87 394
Chris@87 395 If you specify an `n` such that `a` must be zero-padded or truncated, the
Chris@87 396 extra/removed values will be added/removed at high frequencies. One can
Chris@87 397 thus resample a series to `m` points via Fourier interpolation by:
Chris@87 398 ``a_resamp = irfft(rfft(a), m)``.
Chris@87 399
Chris@87 400 Examples
Chris@87 401 --------
Chris@87 402 >>> np.fft.ifft([1, -1j, -1, 1j])
Chris@87 403 array([ 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j])
Chris@87 404 >>> np.fft.irfft([1, -1j, -1])
Chris@87 405 array([ 0., 1., 0., 0.])
Chris@87 406
Chris@87 407 Notice how the last term in the input to the ordinary `ifft` is the
Chris@87 408 complex conjugate of the second term, and the output has zero imaginary
Chris@87 409 part everywhere. When calling `irfft`, the negative frequencies are not
Chris@87 410 specified, and the output array is purely real.
Chris@87 411
Chris@87 412 """
Chris@87 413
Chris@87 414 a = asarray(a).astype(complex)
Chris@87 415 if n is None:
Chris@87 416 n = (shape(a)[axis] - 1) * 2
Chris@87 417 return _raw_fft(a, n, axis, fftpack.rffti, fftpack.rfftb,
Chris@87 418 _real_fft_cache) / n
Chris@87 419
Chris@87 420
Chris@87 421 def hfft(a, n=None, axis=-1):
Chris@87 422 """
Chris@87 423 Compute the FFT of a signal which has Hermitian symmetry (real spectrum).
Chris@87 424
Chris@87 425 Parameters
Chris@87 426 ----------
Chris@87 427 a : array_like
Chris@87 428 The input array.
Chris@87 429 n : int, optional
Chris@87 430 Length of the transformed axis of the output.
Chris@87 431 For `n` output points, ``n//2+1`` input points are necessary. If the
Chris@87 432 input is longer than this, it is cropped. If it is shorter than this,
Chris@87 433 it is padded with zeros. If `n` is not given, it is determined from
Chris@87 434 the length of the input along the axis specified by `axis`.
Chris@87 435 axis : int, optional
Chris@87 436 Axis over which to compute the FFT. If not given, the last
Chris@87 437 axis is used.
Chris@87 438
Chris@87 439 Returns
Chris@87 440 -------
Chris@87 441 out : ndarray
Chris@87 442 The truncated or zero-padded input, transformed along the axis
Chris@87 443 indicated by `axis`, or the last one if `axis` is not specified.
Chris@87 444 The length of the transformed axis is `n`, or, if `n` is not given,
Chris@87 445 ``2*(m-1)`` where ``m`` is the length of the transformed axis of the
Chris@87 446 input. To get an odd number of output points, `n` must be specified.
Chris@87 447
Chris@87 448 Raises
Chris@87 449 ------
Chris@87 450 IndexError
Chris@87 451 If `axis` is larger than the last axis of `a`.
Chris@87 452
Chris@87 453 See also
Chris@87 454 --------
Chris@87 455 rfft : Compute the one-dimensional FFT for real input.
Chris@87 456 ihfft : The inverse of `hfft`.
Chris@87 457
Chris@87 458 Notes
Chris@87 459 -----
Chris@87 460 `hfft`/`ihfft` are a pair analogous to `rfft`/`irfft`, but for the
Chris@87 461 opposite case: here the signal has Hermitian symmetry in the time domain
Chris@87 462 and is real in the frequency domain. So here it's `hfft` for which
Chris@87 463 you must supply the length of the result if it is to be odd:
Chris@87 464 ``ihfft(hfft(a), len(a)) == a``, within numerical accuracy.
Chris@87 465
Chris@87 466 Examples
Chris@87 467 --------
Chris@87 468 >>> signal = np.array([1, 2, 3, 4, 3, 2])
Chris@87 469 >>> np.fft.fft(signal)
Chris@87 470 array([ 15.+0.j, -4.+0.j, 0.+0.j, -1.-0.j, 0.+0.j, -4.+0.j])
Chris@87 471 >>> np.fft.hfft(signal[:4]) # Input first half of signal
Chris@87 472 array([ 15., -4., 0., -1., 0., -4.])
Chris@87 473 >>> np.fft.hfft(signal, 6) # Input entire signal and truncate
Chris@87 474 array([ 15., -4., 0., -1., 0., -4.])
Chris@87 475
Chris@87 476
Chris@87 477 >>> signal = np.array([[1, 1.j], [-1.j, 2]])
Chris@87 478 >>> np.conj(signal.T) - signal # check Hermitian symmetry
Chris@87 479 array([[ 0.-0.j, 0.+0.j],
Chris@87 480 [ 0.+0.j, 0.-0.j]])
Chris@87 481 >>> freq_spectrum = np.fft.hfft(signal)
Chris@87 482 >>> freq_spectrum
Chris@87 483 array([[ 1., 1.],
Chris@87 484 [ 2., -2.]])
Chris@87 485
Chris@87 486 """
Chris@87 487
Chris@87 488 a = asarray(a).astype(complex)
Chris@87 489 if n is None:
Chris@87 490 n = (shape(a)[axis] - 1) * 2
Chris@87 491 return irfft(conjugate(a), n, axis) * n
Chris@87 492
Chris@87 493
Chris@87 494 def ihfft(a, n=None, axis=-1):
Chris@87 495 """
Chris@87 496 Compute the inverse FFT of a signal which has Hermitian symmetry.
Chris@87 497
Chris@87 498 Parameters
Chris@87 499 ----------
Chris@87 500 a : array_like
Chris@87 501 Input array.
Chris@87 502 n : int, optional
Chris@87 503 Length of the inverse FFT.
Chris@87 504 Number of points along transformation axis in the input to use.
Chris@87 505 If `n` is smaller than the length of the input, the input is cropped.
Chris@87 506 If it is larger, the input is padded with zeros. If `n` is not given,
Chris@87 507 the length of the input along the axis specified by `axis` is used.
Chris@87 508 axis : int, optional
Chris@87 509 Axis over which to compute the inverse FFT. If not given, the last
Chris@87 510 axis is used.
Chris@87 511
Chris@87 512 Returns
Chris@87 513 -------
Chris@87 514 out : complex ndarray
Chris@87 515 The truncated or zero-padded input, transformed along the axis
Chris@87 516 indicated by `axis`, or the last one if `axis` is not specified.
Chris@87 517 If `n` is even, the length of the transformed axis is ``(n/2)+1``.
Chris@87 518 If `n` is odd, the length is ``(n+1)/2``.
Chris@87 519
Chris@87 520 See also
Chris@87 521 --------
Chris@87 522 hfft, irfft
Chris@87 523
Chris@87 524 Notes
Chris@87 525 -----
Chris@87 526 `hfft`/`ihfft` are a pair analogous to `rfft`/`irfft`, but for the
Chris@87 527 opposite case: here the signal has Hermitian symmetry in the time domain
Chris@87 528 and is real in the frequency domain. So here it's `hfft` for which
Chris@87 529 you must supply the length of the result if it is to be odd:
Chris@87 530 ``ihfft(hfft(a), len(a)) == a``, within numerical accuracy.
Chris@87 531
Chris@87 532 Examples
Chris@87 533 --------
Chris@87 534 >>> spectrum = np.array([ 15, -4, 0, -1, 0, -4])
Chris@87 535 >>> np.fft.ifft(spectrum)
Chris@87 536 array([ 1.+0.j, 2.-0.j, 3.+0.j, 4.+0.j, 3.+0.j, 2.-0.j])
Chris@87 537 >>> np.fft.ihfft(spectrum)
Chris@87 538 array([ 1.-0.j, 2.-0.j, 3.-0.j, 4.-0.j])
Chris@87 539
Chris@87 540 """
Chris@87 541
Chris@87 542 a = asarray(a).astype(float)
Chris@87 543 if n is None:
Chris@87 544 n = shape(a)[axis]
Chris@87 545 return conjugate(rfft(a, n, axis))/n
Chris@87 546
Chris@87 547
Chris@87 548 def _cook_nd_args(a, s=None, axes=None, invreal=0):
Chris@87 549 if s is None:
Chris@87 550 shapeless = 1
Chris@87 551 if axes is None:
Chris@87 552 s = list(a.shape)
Chris@87 553 else:
Chris@87 554 s = take(a.shape, axes)
Chris@87 555 else:
Chris@87 556 shapeless = 0
Chris@87 557 s = list(s)
Chris@87 558 if axes is None:
Chris@87 559 axes = list(range(-len(s), 0))
Chris@87 560 if len(s) != len(axes):
Chris@87 561 raise ValueError("Shape and axes have different lengths.")
Chris@87 562 if invreal and shapeless:
Chris@87 563 s[-1] = (a.shape[axes[-1]] - 1) * 2
Chris@87 564 return s, axes
Chris@87 565
Chris@87 566
Chris@87 567 def _raw_fftnd(a, s=None, axes=None, function=fft):
Chris@87 568 a = asarray(a)
Chris@87 569 s, axes = _cook_nd_args(a, s, axes)
Chris@87 570 itl = list(range(len(axes)))
Chris@87 571 itl.reverse()
Chris@87 572 for ii in itl:
Chris@87 573 a = function(a, n=s[ii], axis=axes[ii])
Chris@87 574 return a
Chris@87 575
Chris@87 576
Chris@87 577 def fftn(a, s=None, axes=None):
Chris@87 578 """
Chris@87 579 Compute the N-dimensional discrete Fourier Transform.
Chris@87 580
Chris@87 581 This function computes the *N*-dimensional discrete Fourier Transform over
Chris@87 582 any number of axes in an *M*-dimensional array by means of the Fast Fourier
Chris@87 583 Transform (FFT).
Chris@87 584
Chris@87 585 Parameters
Chris@87 586 ----------
Chris@87 587 a : array_like
Chris@87 588 Input array, can be complex.
Chris@87 589 s : sequence of ints, optional
Chris@87 590 Shape (length of each transformed axis) of the output
Chris@87 591 (`s[0]` refers to axis 0, `s[1]` to axis 1, etc.).
Chris@87 592 This corresponds to `n` for `fft(x, n)`.
Chris@87 593 Along any axis, if the given shape is smaller than that of the input,
Chris@87 594 the input is cropped. If it is larger, the input is padded with zeros.
Chris@87 595 if `s` is not given, the shape of the input along the axes specified
Chris@87 596 by `axes` is used.
Chris@87 597 axes : sequence of ints, optional
Chris@87 598 Axes over which to compute the FFT. If not given, the last ``len(s)``
Chris@87 599 axes are used, or all axes if `s` is also not specified.
Chris@87 600 Repeated indices in `axes` means that the transform over that axis is
Chris@87 601 performed multiple times.
Chris@87 602
Chris@87 603 Returns
Chris@87 604 -------
Chris@87 605 out : complex ndarray
Chris@87 606 The truncated or zero-padded input, transformed along the axes
Chris@87 607 indicated by `axes`, or by a combination of `s` and `a`,
Chris@87 608 as explained in the parameters section above.
Chris@87 609
Chris@87 610 Raises
Chris@87 611 ------
Chris@87 612 ValueError
Chris@87 613 If `s` and `axes` have different length.
Chris@87 614 IndexError
Chris@87 615 If an element of `axes` is larger than than the number of axes of `a`.
Chris@87 616
Chris@87 617 See Also
Chris@87 618 --------
Chris@87 619 numpy.fft : Overall view of discrete Fourier transforms, with definitions
Chris@87 620 and conventions used.
Chris@87 621 ifftn : The inverse of `fftn`, the inverse *n*-dimensional FFT.
Chris@87 622 fft : The one-dimensional FFT, with definitions and conventions used.
Chris@87 623 rfftn : The *n*-dimensional FFT of real input.
Chris@87 624 fft2 : The two-dimensional FFT.
Chris@87 625 fftshift : Shifts zero-frequency terms to centre of array
Chris@87 626
Chris@87 627 Notes
Chris@87 628 -----
Chris@87 629 The output, analogously to `fft`, contains the term for zero frequency in
Chris@87 630 the low-order corner of all axes, the positive frequency terms in the
Chris@87 631 first half of all axes, the term for the Nyquist frequency in the middle
Chris@87 632 of all axes and the negative frequency terms in the second half of all
Chris@87 633 axes, in order of decreasingly negative frequency.
Chris@87 634
Chris@87 635 See `numpy.fft` for details, definitions and conventions used.
Chris@87 636
Chris@87 637 Examples
Chris@87 638 --------
Chris@87 639 >>> a = np.mgrid[:3, :3, :3][0]
Chris@87 640 >>> np.fft.fftn(a, axes=(1, 2))
Chris@87 641 array([[[ 0.+0.j, 0.+0.j, 0.+0.j],
Chris@87 642 [ 0.+0.j, 0.+0.j, 0.+0.j],
Chris@87 643 [ 0.+0.j, 0.+0.j, 0.+0.j]],
Chris@87 644 [[ 9.+0.j, 0.+0.j, 0.+0.j],
Chris@87 645 [ 0.+0.j, 0.+0.j, 0.+0.j],
Chris@87 646 [ 0.+0.j, 0.+0.j, 0.+0.j]],
Chris@87 647 [[ 18.+0.j, 0.+0.j, 0.+0.j],
Chris@87 648 [ 0.+0.j, 0.+0.j, 0.+0.j],
Chris@87 649 [ 0.+0.j, 0.+0.j, 0.+0.j]]])
Chris@87 650 >>> np.fft.fftn(a, (2, 2), axes=(0, 1))
Chris@87 651 array([[[ 2.+0.j, 2.+0.j, 2.+0.j],
Chris@87 652 [ 0.+0.j, 0.+0.j, 0.+0.j]],
Chris@87 653 [[-2.+0.j, -2.+0.j, -2.+0.j],
Chris@87 654 [ 0.+0.j, 0.+0.j, 0.+0.j]]])
Chris@87 655
Chris@87 656 >>> import matplotlib.pyplot as plt
Chris@87 657 >>> [X, Y] = np.meshgrid(2 * np.pi * np.arange(200) / 12,
Chris@87 658 ... 2 * np.pi * np.arange(200) / 34)
Chris@87 659 >>> S = np.sin(X) + np.cos(Y) + np.random.uniform(0, 1, X.shape)
Chris@87 660 >>> FS = np.fft.fftn(S)
Chris@87 661 >>> plt.imshow(np.log(np.abs(np.fft.fftshift(FS))**2))
Chris@87 662 <matplotlib.image.AxesImage object at 0x...>
Chris@87 663 >>> plt.show()
Chris@87 664
Chris@87 665 """
Chris@87 666
Chris@87 667 return _raw_fftnd(a, s, axes, fft)
Chris@87 668
Chris@87 669 def ifftn(a, s=None, axes=None):
Chris@87 670 """
Chris@87 671 Compute the N-dimensional inverse discrete Fourier Transform.
Chris@87 672
Chris@87 673 This function computes the inverse of the N-dimensional discrete
Chris@87 674 Fourier Transform over any number of axes in an M-dimensional array by
Chris@87 675 means of the Fast Fourier Transform (FFT). In other words,
Chris@87 676 ``ifftn(fftn(a)) == a`` to within numerical accuracy.
Chris@87 677 For a description of the definitions and conventions used, see `numpy.fft`.
Chris@87 678
Chris@87 679 The input, analogously to `ifft`, should be ordered in the same way as is
Chris@87 680 returned by `fftn`, i.e. it should have the term for zero frequency
Chris@87 681 in all axes in the low-order corner, the positive frequency terms in the
Chris@87 682 first half of all axes, the term for the Nyquist frequency in the middle
Chris@87 683 of all axes and the negative frequency terms in the second half of all
Chris@87 684 axes, in order of decreasingly negative frequency.
Chris@87 685
Chris@87 686 Parameters
Chris@87 687 ----------
Chris@87 688 a : array_like
Chris@87 689 Input array, can be complex.
Chris@87 690 s : sequence of ints, optional
Chris@87 691 Shape (length of each transformed axis) of the output
Chris@87 692 (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.).
Chris@87 693 This corresponds to ``n`` for ``ifft(x, n)``.
Chris@87 694 Along any axis, if the given shape is smaller than that of the input,
Chris@87 695 the input is cropped. If it is larger, the input is padded with zeros.
Chris@87 696 if `s` is not given, the shape of the input along the axes specified
Chris@87 697 by `axes` is used. See notes for issue on `ifft` zero padding.
Chris@87 698 axes : sequence of ints, optional
Chris@87 699 Axes over which to compute the IFFT. If not given, the last ``len(s)``
Chris@87 700 axes are used, or all axes if `s` is also not specified.
Chris@87 701 Repeated indices in `axes` means that the inverse transform over that
Chris@87 702 axis is performed multiple times.
Chris@87 703
Chris@87 704 Returns
Chris@87 705 -------
Chris@87 706 out : complex ndarray
Chris@87 707 The truncated or zero-padded input, transformed along the axes
Chris@87 708 indicated by `axes`, or by a combination of `s` or `a`,
Chris@87 709 as explained in the parameters section above.
Chris@87 710
Chris@87 711 Raises
Chris@87 712 ------
Chris@87 713 ValueError
Chris@87 714 If `s` and `axes` have different length.
Chris@87 715 IndexError
Chris@87 716 If an element of `axes` is larger than than the number of axes of `a`.
Chris@87 717
Chris@87 718 See Also
Chris@87 719 --------
Chris@87 720 numpy.fft : Overall view of discrete Fourier transforms, with definitions
Chris@87 721 and conventions used.
Chris@87 722 fftn : The forward *n*-dimensional FFT, of which `ifftn` is the inverse.
Chris@87 723 ifft : The one-dimensional inverse FFT.
Chris@87 724 ifft2 : The two-dimensional inverse FFT.
Chris@87 725 ifftshift : Undoes `fftshift`, shifts zero-frequency terms to beginning
Chris@87 726 of array.
Chris@87 727
Chris@87 728 Notes
Chris@87 729 -----
Chris@87 730 See `numpy.fft` for definitions and conventions used.
Chris@87 731
Chris@87 732 Zero-padding, analogously with `ifft`, is performed by appending zeros to
Chris@87 733 the input along the specified dimension. Although this is the common
Chris@87 734 approach, it might lead to surprising results. If another form of zero
Chris@87 735 padding is desired, it must be performed before `ifftn` is called.
Chris@87 736
Chris@87 737 Examples
Chris@87 738 --------
Chris@87 739 >>> a = np.eye(4)
Chris@87 740 >>> np.fft.ifftn(np.fft.fftn(a, axes=(0,)), axes=(1,))
Chris@87 741 array([[ 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
Chris@87 742 [ 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
Chris@87 743 [ 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
Chris@87 744 [ 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j]])
Chris@87 745
Chris@87 746
Chris@87 747 Create and plot an image with band-limited frequency content:
Chris@87 748
Chris@87 749 >>> import matplotlib.pyplot as plt
Chris@87 750 >>> n = np.zeros((200,200), dtype=complex)
Chris@87 751 >>> n[60:80, 20:40] = np.exp(1j*np.random.uniform(0, 2*np.pi, (20, 20)))
Chris@87 752 >>> im = np.fft.ifftn(n).real
Chris@87 753 >>> plt.imshow(im)
Chris@87 754 <matplotlib.image.AxesImage object at 0x...>
Chris@87 755 >>> plt.show()
Chris@87 756
Chris@87 757 """
Chris@87 758
Chris@87 759 return _raw_fftnd(a, s, axes, ifft)
Chris@87 760
Chris@87 761
Chris@87 762 def fft2(a, s=None, axes=(-2, -1)):
Chris@87 763 """
Chris@87 764 Compute the 2-dimensional discrete Fourier Transform
Chris@87 765
Chris@87 766 This function computes the *n*-dimensional discrete Fourier Transform
Chris@87 767 over any axes in an *M*-dimensional array by means of the
Chris@87 768 Fast Fourier Transform (FFT). By default, the transform is computed over
Chris@87 769 the last two axes of the input array, i.e., a 2-dimensional FFT.
Chris@87 770
Chris@87 771 Parameters
Chris@87 772 ----------
Chris@87 773 a : array_like
Chris@87 774 Input array, can be complex
Chris@87 775 s : sequence of ints, optional
Chris@87 776 Shape (length of each transformed axis) of the output
Chris@87 777 (`s[0]` refers to axis 0, `s[1]` to axis 1, etc.).
Chris@87 778 This corresponds to `n` for `fft(x, n)`.
Chris@87 779 Along each axis, if the given shape is smaller than that of the input,
Chris@87 780 the input is cropped. If it is larger, the input is padded with zeros.
Chris@87 781 if `s` is not given, the shape of the input along the axes specified
Chris@87 782 by `axes` is used.
Chris@87 783 axes : sequence of ints, optional
Chris@87 784 Axes over which to compute the FFT. If not given, the last two
Chris@87 785 axes are used. A repeated index in `axes` means the transform over
Chris@87 786 that axis is performed multiple times. A one-element sequence means
Chris@87 787 that a one-dimensional FFT is performed.
Chris@87 788
Chris@87 789 Returns
Chris@87 790 -------
Chris@87 791 out : complex ndarray
Chris@87 792 The truncated or zero-padded input, transformed along the axes
Chris@87 793 indicated by `axes`, or the last two axes if `axes` is not given.
Chris@87 794
Chris@87 795 Raises
Chris@87 796 ------
Chris@87 797 ValueError
Chris@87 798 If `s` and `axes` have different length, or `axes` not given and
Chris@87 799 ``len(s) != 2``.
Chris@87 800 IndexError
Chris@87 801 If an element of `axes` is larger than than the number of axes of `a`.
Chris@87 802
Chris@87 803 See Also
Chris@87 804 --------
Chris@87 805 numpy.fft : Overall view of discrete Fourier transforms, with definitions
Chris@87 806 and conventions used.
Chris@87 807 ifft2 : The inverse two-dimensional FFT.
Chris@87 808 fft : The one-dimensional FFT.
Chris@87 809 fftn : The *n*-dimensional FFT.
Chris@87 810 fftshift : Shifts zero-frequency terms to the center of the array.
Chris@87 811 For two-dimensional input, swaps first and third quadrants, and second
Chris@87 812 and fourth quadrants.
Chris@87 813
Chris@87 814 Notes
Chris@87 815 -----
Chris@87 816 `fft2` is just `fftn` with a different default for `axes`.
Chris@87 817
Chris@87 818 The output, analogously to `fft`, contains the term for zero frequency in
Chris@87 819 the low-order corner of the transformed axes, the positive frequency terms
Chris@87 820 in the first half of these axes, the term for the Nyquist frequency in the
Chris@87 821 middle of the axes and the negative frequency terms in the second half of
Chris@87 822 the axes, in order of decreasingly negative frequency.
Chris@87 823
Chris@87 824 See `fftn` for details and a plotting example, and `numpy.fft` for
Chris@87 825 definitions and conventions used.
Chris@87 826
Chris@87 827
Chris@87 828 Examples
Chris@87 829 --------
Chris@87 830 >>> a = np.mgrid[:5, :5][0]
Chris@87 831 >>> np.fft.fft2(a)
Chris@87 832 array([[ 50.0 +0.j , 0.0 +0.j , 0.0 +0.j ,
Chris@87 833 0.0 +0.j , 0.0 +0.j ],
Chris@87 834 [-12.5+17.20477401j, 0.0 +0.j , 0.0 +0.j ,
Chris@87 835 0.0 +0.j , 0.0 +0.j ],
Chris@87 836 [-12.5 +4.0614962j , 0.0 +0.j , 0.0 +0.j ,
Chris@87 837 0.0 +0.j , 0.0 +0.j ],
Chris@87 838 [-12.5 -4.0614962j , 0.0 +0.j , 0.0 +0.j ,
Chris@87 839 0.0 +0.j , 0.0 +0.j ],
Chris@87 840 [-12.5-17.20477401j, 0.0 +0.j , 0.0 +0.j ,
Chris@87 841 0.0 +0.j , 0.0 +0.j ]])
Chris@87 842
Chris@87 843 """
Chris@87 844
Chris@87 845 return _raw_fftnd(a, s, axes, fft)
Chris@87 846
Chris@87 847
Chris@87 848 def ifft2(a, s=None, axes=(-2, -1)):
Chris@87 849 """
Chris@87 850 Compute the 2-dimensional inverse discrete Fourier Transform.
Chris@87 851
Chris@87 852 This function computes the inverse of the 2-dimensional discrete Fourier
Chris@87 853 Transform over any number of axes in an M-dimensional array by means of
Chris@87 854 the Fast Fourier Transform (FFT). In other words, ``ifft2(fft2(a)) == a``
Chris@87 855 to within numerical accuracy. By default, the inverse transform is
Chris@87 856 computed over the last two axes of the input array.
Chris@87 857
Chris@87 858 The input, analogously to `ifft`, should be ordered in the same way as is
Chris@87 859 returned by `fft2`, i.e. it should have the term for zero frequency
Chris@87 860 in the low-order corner of the two axes, the positive frequency terms in
Chris@87 861 the first half of these axes, the term for the Nyquist frequency in the
Chris@87 862 middle of the axes and the negative frequency terms in the second half of
Chris@87 863 both axes, in order of decreasingly negative frequency.
Chris@87 864
Chris@87 865 Parameters
Chris@87 866 ----------
Chris@87 867 a : array_like
Chris@87 868 Input array, can be complex.
Chris@87 869 s : sequence of ints, optional
Chris@87 870 Shape (length of each axis) of the output (``s[0]`` refers to axis 0,
Chris@87 871 ``s[1]`` to axis 1, etc.). This corresponds to `n` for ``ifft(x, n)``.
Chris@87 872 Along each axis, if the given shape is smaller than that of the input,
Chris@87 873 the input is cropped. If it is larger, the input is padded with zeros.
Chris@87 874 if `s` is not given, the shape of the input along the axes specified
Chris@87 875 by `axes` is used. See notes for issue on `ifft` zero padding.
Chris@87 876 axes : sequence of ints, optional
Chris@87 877 Axes over which to compute the FFT. If not given, the last two
Chris@87 878 axes are used. A repeated index in `axes` means the transform over
Chris@87 879 that axis is performed multiple times. A one-element sequence means
Chris@87 880 that a one-dimensional FFT is performed.
Chris@87 881
Chris@87 882 Returns
Chris@87 883 -------
Chris@87 884 out : complex ndarray
Chris@87 885 The truncated or zero-padded input, transformed along the axes
Chris@87 886 indicated by `axes`, or the last two axes if `axes` is not given.
Chris@87 887
Chris@87 888 Raises
Chris@87 889 ------
Chris@87 890 ValueError
Chris@87 891 If `s` and `axes` have different length, or `axes` not given and
Chris@87 892 ``len(s) != 2``.
Chris@87 893 IndexError
Chris@87 894 If an element of `axes` is larger than than the number of axes of `a`.
Chris@87 895
Chris@87 896 See Also
Chris@87 897 --------
Chris@87 898 numpy.fft : Overall view of discrete Fourier transforms, with definitions
Chris@87 899 and conventions used.
Chris@87 900 fft2 : The forward 2-dimensional FFT, of which `ifft2` is the inverse.
Chris@87 901 ifftn : The inverse of the *n*-dimensional FFT.
Chris@87 902 fft : The one-dimensional FFT.
Chris@87 903 ifft : The one-dimensional inverse FFT.
Chris@87 904
Chris@87 905 Notes
Chris@87 906 -----
Chris@87 907 `ifft2` is just `ifftn` with a different default for `axes`.
Chris@87 908
Chris@87 909 See `ifftn` for details and a plotting example, and `numpy.fft` for
Chris@87 910 definition and conventions used.
Chris@87 911
Chris@87 912 Zero-padding, analogously with `ifft`, is performed by appending zeros to
Chris@87 913 the input along the specified dimension. Although this is the common
Chris@87 914 approach, it might lead to surprising results. If another form of zero
Chris@87 915 padding is desired, it must be performed before `ifft2` is called.
Chris@87 916
Chris@87 917 Examples
Chris@87 918 --------
Chris@87 919 >>> a = 4 * np.eye(4)
Chris@87 920 >>> np.fft.ifft2(a)
Chris@87 921 array([[ 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
Chris@87 922 [ 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],
Chris@87 923 [ 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
Chris@87 924 [ 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j]])
Chris@87 925
Chris@87 926 """
Chris@87 927
Chris@87 928 return _raw_fftnd(a, s, axes, ifft)
Chris@87 929
Chris@87 930
Chris@87 931 def rfftn(a, s=None, axes=None):
Chris@87 932 """
Chris@87 933 Compute the N-dimensional discrete Fourier Transform for real input.
Chris@87 934
Chris@87 935 This function computes the N-dimensional discrete Fourier Transform over
Chris@87 936 any number of axes in an M-dimensional real array by means of the Fast
Chris@87 937 Fourier Transform (FFT). By default, all axes are transformed, with the
Chris@87 938 real transform performed over the last axis, while the remaining
Chris@87 939 transforms are complex.
Chris@87 940
Chris@87 941 Parameters
Chris@87 942 ----------
Chris@87 943 a : array_like
Chris@87 944 Input array, taken to be real.
Chris@87 945 s : sequence of ints, optional
Chris@87 946 Shape (length along each transformed axis) to use from the input.
Chris@87 947 (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.).
Chris@87 948 The final element of `s` corresponds to `n` for ``rfft(x, n)``, while
Chris@87 949 for the remaining axes, it corresponds to `n` for ``fft(x, n)``.
Chris@87 950 Along any axis, if the given shape is smaller than that of the input,
Chris@87 951 the input is cropped. If it is larger, the input is padded with zeros.
Chris@87 952 if `s` is not given, the shape of the input along the axes specified
Chris@87 953 by `axes` is used.
Chris@87 954 axes : sequence of ints, optional
Chris@87 955 Axes over which to compute the FFT. If not given, the last ``len(s)``
Chris@87 956 axes are used, or all axes if `s` is also not specified.
Chris@87 957
Chris@87 958 Returns
Chris@87 959 -------
Chris@87 960 out : complex ndarray
Chris@87 961 The truncated or zero-padded input, transformed along the axes
Chris@87 962 indicated by `axes`, or by a combination of `s` and `a`,
Chris@87 963 as explained in the parameters section above.
Chris@87 964 The length of the last axis transformed will be ``s[-1]//2+1``,
Chris@87 965 while the remaining transformed axes will have lengths according to
Chris@87 966 `s`, or unchanged from the input.
Chris@87 967
Chris@87 968 Raises
Chris@87 969 ------
Chris@87 970 ValueError
Chris@87 971 If `s` and `axes` have different length.
Chris@87 972 IndexError
Chris@87 973 If an element of `axes` is larger than than the number of axes of `a`.
Chris@87 974
Chris@87 975 See Also
Chris@87 976 --------
Chris@87 977 irfftn : The inverse of `rfftn`, i.e. the inverse of the n-dimensional FFT
Chris@87 978 of real input.
Chris@87 979 fft : The one-dimensional FFT, with definitions and conventions used.
Chris@87 980 rfft : The one-dimensional FFT of real input.
Chris@87 981 fftn : The n-dimensional FFT.
Chris@87 982 rfft2 : The two-dimensional FFT of real input.
Chris@87 983
Chris@87 984 Notes
Chris@87 985 -----
Chris@87 986 The transform for real input is performed over the last transformation
Chris@87 987 axis, as by `rfft`, then the transform over the remaining axes is
Chris@87 988 performed as by `fftn`. The order of the output is as for `rfft` for the
Chris@87 989 final transformation axis, and as for `fftn` for the remaining
Chris@87 990 transformation axes.
Chris@87 991
Chris@87 992 See `fft` for details, definitions and conventions used.
Chris@87 993
Chris@87 994 Examples
Chris@87 995 --------
Chris@87 996 >>> a = np.ones((2, 2, 2))
Chris@87 997 >>> np.fft.rfftn(a)
Chris@87 998 array([[[ 8.+0.j, 0.+0.j],
Chris@87 999 [ 0.+0.j, 0.+0.j]],
Chris@87 1000 [[ 0.+0.j, 0.+0.j],
Chris@87 1001 [ 0.+0.j, 0.+0.j]]])
Chris@87 1002
Chris@87 1003 >>> np.fft.rfftn(a, axes=(2, 0))
Chris@87 1004 array([[[ 4.+0.j, 0.+0.j],
Chris@87 1005 [ 4.+0.j, 0.+0.j]],
Chris@87 1006 [[ 0.+0.j, 0.+0.j],
Chris@87 1007 [ 0.+0.j, 0.+0.j]]])
Chris@87 1008
Chris@87 1009 """
Chris@87 1010
Chris@87 1011 a = asarray(a).astype(float)
Chris@87 1012 s, axes = _cook_nd_args(a, s, axes)
Chris@87 1013 a = rfft(a, s[-1], axes[-1])
Chris@87 1014 for ii in range(len(axes)-1):
Chris@87 1015 a = fft(a, s[ii], axes[ii])
Chris@87 1016 return a
Chris@87 1017
Chris@87 1018 def rfft2(a, s=None, axes=(-2, -1)):
Chris@87 1019 """
Chris@87 1020 Compute the 2-dimensional FFT of a real array.
Chris@87 1021
Chris@87 1022 Parameters
Chris@87 1023 ----------
Chris@87 1024 a : array
Chris@87 1025 Input array, taken to be real.
Chris@87 1026 s : sequence of ints, optional
Chris@87 1027 Shape of the FFT.
Chris@87 1028 axes : sequence of ints, optional
Chris@87 1029 Axes over which to compute the FFT.
Chris@87 1030
Chris@87 1031 Returns
Chris@87 1032 -------
Chris@87 1033 out : ndarray
Chris@87 1034 The result of the real 2-D FFT.
Chris@87 1035
Chris@87 1036 See Also
Chris@87 1037 --------
Chris@87 1038 rfftn : Compute the N-dimensional discrete Fourier Transform for real
Chris@87 1039 input.
Chris@87 1040
Chris@87 1041 Notes
Chris@87 1042 -----
Chris@87 1043 This is really just `rfftn` with different default behavior.
Chris@87 1044 For more details see `rfftn`.
Chris@87 1045
Chris@87 1046 """
Chris@87 1047
Chris@87 1048 return rfftn(a, s, axes)
Chris@87 1049
Chris@87 1050 def irfftn(a, s=None, axes=None):
Chris@87 1051 """
Chris@87 1052 Compute the inverse of the N-dimensional FFT of real input.
Chris@87 1053
Chris@87 1054 This function computes the inverse of the N-dimensional discrete
Chris@87 1055 Fourier Transform for real input over any number of axes in an
Chris@87 1056 M-dimensional array by means of the Fast Fourier Transform (FFT). In
Chris@87 1057 other words, ``irfftn(rfftn(a), a.shape) == a`` to within numerical
Chris@87 1058 accuracy. (The ``a.shape`` is necessary like ``len(a)`` is for `irfft`,
Chris@87 1059 and for the same reason.)
Chris@87 1060
Chris@87 1061 The input should be ordered in the same way as is returned by `rfftn`,
Chris@87 1062 i.e. as for `irfft` for the final transformation axis, and as for `ifftn`
Chris@87 1063 along all the other axes.
Chris@87 1064
Chris@87 1065 Parameters
Chris@87 1066 ----------
Chris@87 1067 a : array_like
Chris@87 1068 Input array.
Chris@87 1069 s : sequence of ints, optional
Chris@87 1070 Shape (length of each transformed axis) of the output
Chris@87 1071 (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.). `s` is also the
Chris@87 1072 number of input points used along this axis, except for the last axis,
Chris@87 1073 where ``s[-1]//2+1`` points of the input are used.
Chris@87 1074 Along any axis, if the shape indicated by `s` is smaller than that of
Chris@87 1075 the input, the input is cropped. If it is larger, the input is padded
Chris@87 1076 with zeros. If `s` is not given, the shape of the input along the
Chris@87 1077 axes specified by `axes` is used.
Chris@87 1078 axes : sequence of ints, optional
Chris@87 1079 Axes over which to compute the inverse FFT. If not given, the last
Chris@87 1080 `len(s)` axes are used, or all axes if `s` is also not specified.
Chris@87 1081 Repeated indices in `axes` means that the inverse transform over that
Chris@87 1082 axis is performed multiple times.
Chris@87 1083
Chris@87 1084 Returns
Chris@87 1085 -------
Chris@87 1086 out : ndarray
Chris@87 1087 The truncated or zero-padded input, transformed along the axes
Chris@87 1088 indicated by `axes`, or by a combination of `s` or `a`,
Chris@87 1089 as explained in the parameters section above.
Chris@87 1090 The length of each transformed axis is as given by the corresponding
Chris@87 1091 element of `s`, or the length of the input in every axis except for the
Chris@87 1092 last one if `s` is not given. In the final transformed axis the length
Chris@87 1093 of the output when `s` is not given is ``2*(m-1)`` where ``m`` is the
Chris@87 1094 length of the final transformed axis of the input. To get an odd
Chris@87 1095 number of output points in the final axis, `s` must be specified.
Chris@87 1096
Chris@87 1097 Raises
Chris@87 1098 ------
Chris@87 1099 ValueError
Chris@87 1100 If `s` and `axes` have different length.
Chris@87 1101 IndexError
Chris@87 1102 If an element of `axes` is larger than than the number of axes of `a`.
Chris@87 1103
Chris@87 1104 See Also
Chris@87 1105 --------
Chris@87 1106 rfftn : The forward n-dimensional FFT of real input,
Chris@87 1107 of which `ifftn` is the inverse.
Chris@87 1108 fft : The one-dimensional FFT, with definitions and conventions used.
Chris@87 1109 irfft : The inverse of the one-dimensional FFT of real input.
Chris@87 1110 irfft2 : The inverse of the two-dimensional FFT of real input.
Chris@87 1111
Chris@87 1112 Notes
Chris@87 1113 -----
Chris@87 1114 See `fft` for definitions and conventions used.
Chris@87 1115
Chris@87 1116 See `rfft` for definitions and conventions used for real input.
Chris@87 1117
Chris@87 1118 Examples
Chris@87 1119 --------
Chris@87 1120 >>> a = np.zeros((3, 2, 2))
Chris@87 1121 >>> a[0, 0, 0] = 3 * 2 * 2
Chris@87 1122 >>> np.fft.irfftn(a)
Chris@87 1123 array([[[ 1., 1.],
Chris@87 1124 [ 1., 1.]],
Chris@87 1125 [[ 1., 1.],
Chris@87 1126 [ 1., 1.]],
Chris@87 1127 [[ 1., 1.],
Chris@87 1128 [ 1., 1.]]])
Chris@87 1129
Chris@87 1130 """
Chris@87 1131
Chris@87 1132 a = asarray(a).astype(complex)
Chris@87 1133 s, axes = _cook_nd_args(a, s, axes, invreal=1)
Chris@87 1134 for ii in range(len(axes)-1):
Chris@87 1135 a = ifft(a, s[ii], axes[ii])
Chris@87 1136 a = irfft(a, s[-1], axes[-1])
Chris@87 1137 return a
Chris@87 1138
Chris@87 1139 def irfft2(a, s=None, axes=(-2, -1)):
Chris@87 1140 """
Chris@87 1141 Compute the 2-dimensional inverse FFT of a real array.
Chris@87 1142
Chris@87 1143 Parameters
Chris@87 1144 ----------
Chris@87 1145 a : array_like
Chris@87 1146 The input array
Chris@87 1147 s : sequence of ints, optional
Chris@87 1148 Shape of the inverse FFT.
Chris@87 1149 axes : sequence of ints, optional
Chris@87 1150 The axes over which to compute the inverse fft.
Chris@87 1151 Default is the last two axes.
Chris@87 1152
Chris@87 1153 Returns
Chris@87 1154 -------
Chris@87 1155 out : ndarray
Chris@87 1156 The result of the inverse real 2-D FFT.
Chris@87 1157
Chris@87 1158 See Also
Chris@87 1159 --------
Chris@87 1160 irfftn : Compute the inverse of the N-dimensional FFT of real input.
Chris@87 1161
Chris@87 1162 Notes
Chris@87 1163 -----
Chris@87 1164 This is really `irfftn` with different defaults.
Chris@87 1165 For more details see `irfftn`.
Chris@87 1166
Chris@87 1167 """
Chris@87 1168
Chris@87 1169 return irfftn(a, s, axes)