fftpack.py 45 KB

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