_basic.py 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  1. from scipy._lib.uarray import generate_multimethod, Dispatchable
  2. from scipy._lib._array_api import xp_capabilities
  3. import numpy as np
  4. def _x_replacer(args, kwargs, dispatchables):
  5. """
  6. uarray argument replacer to replace the transform input array (``x``)
  7. """
  8. if len(args) > 0:
  9. return (dispatchables[0],) + args[1:], kwargs
  10. kw = kwargs.copy()
  11. kw['x'] = dispatchables[0]
  12. return args, kw
  13. def _dispatch(func):
  14. """
  15. Function annotation that creates a uarray multimethod from the function
  16. """
  17. return generate_multimethod(func, _x_replacer, domain="numpy.scipy.fft")
  18. @xp_capabilities(allow_dask_compute=True)
  19. @_dispatch
  20. def fft(x, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, *,
  21. plan=None):
  22. """
  23. Compute the 1-D discrete Fourier Transform.
  24. This function computes the 1-D *n*-point discrete Fourier
  25. Transform (DFT) with the efficient Fast Fourier Transform (FFT)
  26. algorithm [1]_.
  27. Parameters
  28. ----------
  29. x : array_like
  30. Input array, can be complex.
  31. n : int, optional
  32. Length of the transformed axis of the output.
  33. If `n` is smaller than the length of the input, the input is cropped.
  34. If it is larger, the input is padded with zeros. If `n` is not given,
  35. the length of the input along the axis specified by `axis` is used.
  36. axis : int, optional
  37. Axis over which to compute the FFT. If not given, the last axis is
  38. used.
  39. norm : {"backward", "ortho", "forward"}, optional
  40. Normalization mode. Default is "backward", meaning no normalization on
  41. the forward transforms and scaling by ``1/n`` on the `ifft`.
  42. "forward" instead applies the ``1/n`` factor on the forward transform.
  43. For ``norm="ortho"``, both directions are scaled by ``1/sqrt(n)``.
  44. .. versionadded:: 1.6.0
  45. ``norm={"forward", "backward"}`` options were added
  46. overwrite_x : bool, optional
  47. If True, the contents of `x` can be destroyed; the default is False.
  48. See the notes below for more details.
  49. workers : int, optional
  50. Maximum number of workers to use for parallel computation. If negative,
  51. the value wraps around from ``os.cpu_count()``. See below for more
  52. details.
  53. plan : object, optional
  54. This argument is reserved for passing in a precomputed plan provided
  55. by downstream FFT vendors. It is currently not used in SciPy.
  56. .. versionadded:: 1.5.0
  57. Returns
  58. -------
  59. out : complex ndarray
  60. The truncated or zero-padded input, transformed along the axis
  61. indicated by `axis`, or the last one if `axis` is not specified.
  62. Raises
  63. ------
  64. IndexError
  65. if `axes` is larger than the last axis of `x`.
  66. See Also
  67. --------
  68. ifft : The inverse of `fft`.
  69. fft2 : The 2-D FFT.
  70. fftn : The N-D FFT.
  71. rfftn : The N-D FFT of real input.
  72. fftfreq : Frequency bins for given FFT parameters.
  73. next_fast_len : Size to pad input to for most efficient transforms
  74. Notes
  75. -----
  76. FFT (Fast Fourier Transform) refers to a way the discrete Fourier Transform
  77. (DFT) can be calculated efficiently, by using symmetries in the calculated
  78. terms. The symmetry is highest when `n` is a power of 2, and the transform
  79. is therefore most efficient for these sizes. For poorly factorizable sizes,
  80. `scipy.fft` uses Bluestein's algorithm [2]_ and so is never worse than
  81. O(`n` log `n`). Further performance improvements may be seen by zero-padding
  82. the input using `next_fast_len`.
  83. If ``x`` is a 1d array, then the `fft` is equivalent to ::
  84. y[k] = np.sum(x * np.exp(-2j * np.pi * k * np.arange(n)/n))
  85. The frequency term ``f=k/n`` is found at ``y[k]``. At ``y[n/2]`` we reach
  86. the Nyquist frequency and wrap around to the negative-frequency terms. So,
  87. for an 8-point transform, the frequencies of the result are
  88. [0, 1, 2, 3, -4, -3, -2, -1]. To rearrange the fft output so that the
  89. zero-frequency component is centered, like [-4, -3, -2, -1, 0, 1, 2, 3],
  90. use `fftshift`.
  91. Transforms can be done in single, double, or extended precision (long
  92. double) floating point. Half precision inputs will be converted to single
  93. precision and non-floating-point inputs will be converted to double
  94. precision.
  95. If the data type of ``x`` is real, a "real FFT" algorithm is automatically
  96. used, which roughly halves the computation time. To increase efficiency
  97. a little further, use `rfft`, which does the same calculation, but only
  98. outputs half of the symmetrical spectrum. If the data are both real and
  99. symmetrical, the `dct` can again double the efficiency, by generating
  100. half of the spectrum from half of the signal.
  101. When ``overwrite_x=True`` is specified, the memory referenced by ``x`` may
  102. be used by the implementation in any way. This may include reusing the
  103. memory for the result, but this is in no way guaranteed. You should not
  104. rely on the contents of ``x`` after the transform as this may change in
  105. future without warning.
  106. The ``workers`` argument specifies the maximum number of parallel jobs to
  107. split the FFT computation into. This will execute independent 1-D
  108. FFTs within ``x``. So, ``x`` must be at least 2-D and the
  109. non-transformed axes must be large enough to split into chunks. If ``x`` is
  110. too small, fewer jobs may be used than requested.
  111. References
  112. ----------
  113. .. [1] Cooley, James W., and John W. Tukey, 1965, "An algorithm for the
  114. machine calculation of complex Fourier series," *Math. Comput.*
  115. 19: 297-301.
  116. .. [2] Bluestein, L., 1970, "A linear filtering approach to the
  117. computation of discrete Fourier transform". *IEEE Transactions on
  118. Audio and Electroacoustics.* 18 (4): 451-455.
  119. Examples
  120. --------
  121. >>> import scipy.fft
  122. >>> import numpy as np
  123. >>> scipy.fft.fft(np.exp(2j * np.pi * np.arange(8) / 8))
  124. array([-2.33486982e-16+1.14423775e-17j, 8.00000000e+00-1.25557246e-15j,
  125. 2.33486982e-16+2.33486982e-16j, 0.00000000e+00+1.22464680e-16j,
  126. -1.14423775e-17+2.33486982e-16j, 0.00000000e+00+5.20784380e-16j,
  127. 1.14423775e-17+1.14423775e-17j, 0.00000000e+00+1.22464680e-16j])
  128. In this example, real input has an FFT which is Hermitian, i.e., symmetric
  129. in the real part and anti-symmetric in the imaginary part:
  130. >>> from scipy.fft import fft, fftfreq, fftshift
  131. >>> import matplotlib.pyplot as plt
  132. >>> t = np.arange(256)
  133. >>> sp = fftshift(fft(np.sin(t)))
  134. >>> freq = fftshift(fftfreq(t.shape[-1]))
  135. >>> plt.plot(freq, sp.real, freq, sp.imag)
  136. [<matplotlib.lines.Line2D object at 0x...>,
  137. <matplotlib.lines.Line2D object at 0x...>]
  138. >>> plt.show()
  139. """
  140. return (Dispatchable(x, np.ndarray),)
  141. @xp_capabilities(allow_dask_compute=True)
  142. @_dispatch
  143. def ifft(x, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, *,
  144. plan=None):
  145. """
  146. Compute the 1-D inverse discrete Fourier Transform.
  147. This function computes the inverse of the 1-D *n*-point
  148. discrete Fourier transform computed by `fft`. In other words,
  149. ``ifft(fft(x)) == x`` to within numerical accuracy.
  150. The input should be ordered in the same way as is returned by `fft`,
  151. i.e.,
  152. * ``x[0]`` should contain the zero frequency term,
  153. * ``x[1:n//2]`` should contain the positive-frequency terms,
  154. * ``x[n//2 + 1:]`` should contain the negative-frequency terms, in
  155. increasing order starting from the most negative frequency.
  156. For an even number of input points, ``x[n//2]`` represents the sum of
  157. the values at the positive and negative Nyquist frequencies, as the two
  158. are aliased together. See `fft` for details.
  159. Parameters
  160. ----------
  161. x : array_like
  162. Input array, can be complex.
  163. n : int, optional
  164. Length of the transformed axis of the output.
  165. If `n` is smaller than the length of the input, the input is cropped.
  166. If it is larger, the input is padded with zeros. If `n` is not given,
  167. the length of the input along the axis specified by `axis` is used.
  168. See notes about padding issues.
  169. axis : int, optional
  170. Axis over which to compute the inverse DFT. If not given, the last
  171. axis is used.
  172. norm : {"backward", "ortho", "forward"}, optional
  173. Normalization mode (see `fft`). Default is "backward".
  174. overwrite_x : bool, optional
  175. If True, the contents of `x` can be destroyed; the default is False.
  176. See :func:`fft` for more details.
  177. workers : int, optional
  178. Maximum number of workers to use for parallel computation. If negative,
  179. the value wraps around from ``os.cpu_count()``.
  180. See :func:`~scipy.fft.fft` for more details.
  181. plan : object, optional
  182. This argument is reserved for passing in a precomputed plan provided
  183. by downstream FFT vendors. It is currently not used in SciPy.
  184. .. versionadded:: 1.5.0
  185. Returns
  186. -------
  187. out : complex ndarray
  188. The truncated or zero-padded input, transformed along the axis
  189. indicated by `axis`, or the last one if `axis` is not specified.
  190. Raises
  191. ------
  192. IndexError
  193. If `axes` is larger than the last axis of `x`.
  194. See Also
  195. --------
  196. fft : The 1-D (forward) FFT, of which `ifft` is the inverse.
  197. ifft2 : The 2-D inverse FFT.
  198. ifftn : The N-D inverse FFT.
  199. Notes
  200. -----
  201. If the input parameter `n` is larger than the size of the input, the input
  202. is padded by appending zeros at the end. Even though this is the common
  203. approach, it might lead to surprising results. If a different padding is
  204. desired, it must be performed before calling `ifft`.
  205. If ``x`` is a 1-D array, then the `ifft` is equivalent to ::
  206. y[k] = np.sum(x * np.exp(2j * np.pi * k * np.arange(n)/n)) / len(x)
  207. As with `fft`, `ifft` has support for all floating point types and is
  208. optimized for real input.
  209. Examples
  210. --------
  211. >>> import scipy.fft
  212. >>> import numpy as np
  213. >>> scipy.fft.ifft([0, 4, 0, 0])
  214. array([ 1.+0.j, 0.+1.j, -1.+0.j, 0.-1.j]) # may vary
  215. Create and plot a band-limited signal with random phases:
  216. >>> import matplotlib.pyplot as plt
  217. >>> rng = np.random.default_rng()
  218. >>> t = np.arange(400)
  219. >>> n = np.zeros((400,), dtype=complex)
  220. >>> n[40:60] = np.exp(1j*rng.uniform(0, 2*np.pi, (20,)))
  221. >>> s = scipy.fft.ifft(n)
  222. >>> plt.plot(t, s.real, 'b-', t, s.imag, 'r--')
  223. [<matplotlib.lines.Line2D object at ...>, <matplotlib.lines.Line2D object at ...>]
  224. >>> plt.legend(('real', 'imaginary'))
  225. <matplotlib.legend.Legend object at ...>
  226. >>> plt.show()
  227. """
  228. return (Dispatchable(x, np.ndarray),)
  229. @xp_capabilities(allow_dask_compute=True)
  230. @_dispatch
  231. def rfft(x, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, *,
  232. plan=None):
  233. """
  234. Compute the 1-D discrete Fourier Transform for real input.
  235. This function computes the 1-D *n*-point discrete Fourier
  236. Transform (DFT) of a real-valued array by means of an efficient algorithm
  237. called the Fast Fourier Transform (FFT).
  238. Parameters
  239. ----------
  240. x : array_like
  241. Input array
  242. n : int, optional
  243. Number of points along transformation axis in the input to use.
  244. If `n` is smaller than the length of the input, the input is cropped.
  245. If it is larger, the input is padded with zeros. If `n` is not given,
  246. the length of the input along the axis specified by `axis` is used.
  247. axis : int, optional
  248. Axis over which to compute the FFT. If not given, the last axis is
  249. used.
  250. norm : {"backward", "ortho", "forward"}, optional
  251. Normalization mode (see `fft`). Default is "backward".
  252. overwrite_x : bool, optional
  253. If True, the contents of `x` can be destroyed; the default is False.
  254. See :func:`fft` for more details.
  255. workers : int, optional
  256. Maximum number of workers to use for parallel computation. If negative,
  257. the value wraps around from ``os.cpu_count()``.
  258. See :func:`~scipy.fft.fft` for more details.
  259. plan : object, optional
  260. This argument is reserved for passing in a precomputed plan provided
  261. by downstream FFT vendors. It is currently not used in SciPy.
  262. .. versionadded:: 1.5.0
  263. Returns
  264. -------
  265. out : complex ndarray
  266. The truncated or zero-padded input, transformed along the axis
  267. indicated by `axis`, or the last one if `axis` is not specified.
  268. If `n` is even, the length of the transformed axis is ``(n/2)+1``.
  269. If `n` is odd, the length is ``(n+1)/2``.
  270. Raises
  271. ------
  272. IndexError
  273. If `axis` is larger than the last axis of `a`.
  274. See Also
  275. --------
  276. irfft : The inverse of `rfft`.
  277. fft : The 1-D FFT of general (complex) input.
  278. fftn : The N-D FFT.
  279. rfft2 : The 2-D FFT of real input.
  280. rfftn : The N-D FFT of real input.
  281. Notes
  282. -----
  283. When the DFT is computed for purely real input, the output is
  284. Hermitian-symmetric, i.e., the negative frequency terms are just the complex
  285. conjugates of the corresponding positive-frequency terms, and the
  286. negative-frequency terms are therefore redundant. This function does not
  287. compute the negative frequency terms, and the length of the transformed
  288. axis of the output is therefore ``n//2 + 1``.
  289. When ``X = rfft(x)`` and fs is the sampling frequency, ``X[0]`` contains
  290. the zero-frequency term 0*fs, which is real due to Hermitian symmetry.
  291. If `n` is even, ``A[-1]`` contains the term representing both positive
  292. and negative Nyquist frequency (+fs/2 and -fs/2), and must also be purely
  293. real. If `n` is odd, there is no term at fs/2; ``A[-1]`` contains
  294. the largest positive frequency (fs/2*(n-1)/n), and is complex in the
  295. general case.
  296. If the input `a` contains an imaginary part, it is silently discarded.
  297. Examples
  298. --------
  299. >>> import scipy.fft
  300. >>> scipy.fft.fft([0, 1, 0, 0])
  301. array([ 1.+0.j, 0.-1.j, -1.+0.j, 0.+1.j]) # may vary
  302. >>> scipy.fft.rfft([0, 1, 0, 0])
  303. array([ 1.+0.j, 0.-1.j, -1.+0.j]) # may vary
  304. Notice how the final element of the `fft` output is the complex conjugate
  305. of the second element, for real input. For `rfft`, this symmetry is
  306. exploited to compute only the non-negative frequency terms.
  307. """
  308. return (Dispatchable(x, np.ndarray),)
  309. @xp_capabilities(allow_dask_compute=True)
  310. @_dispatch
  311. def irfft(x, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, *,
  312. plan=None):
  313. """
  314. Computes the inverse of `rfft`.
  315. This function computes the inverse of the 1-D *n*-point
  316. discrete Fourier Transform of real input computed by `rfft`.
  317. In other words, ``irfft(rfft(x), len(x)) == x`` to within numerical
  318. accuracy. (See Notes below for why ``len(a)`` is necessary here.)
  319. The input is expected to be in the form returned by `rfft`, i.e., the
  320. real zero-frequency term followed by the complex positive frequency terms
  321. in order of increasing frequency. Since the discrete Fourier Transform of
  322. real input is Hermitian-symmetric, the negative frequency terms are taken
  323. to be the complex conjugates of the corresponding positive frequency terms.
  324. Parameters
  325. ----------
  326. x : array_like
  327. The input array.
  328. n : int, optional
  329. Length of the transformed axis of the output.
  330. For `n` output points, ``n//2+1`` input points are necessary. If the
  331. input is longer than this, it is cropped. If it is shorter than this,
  332. it is padded with zeros. If `n` is not given, it is taken to be
  333. ``2*(m-1)``, where ``m`` is the length of the input along the axis
  334. specified by `axis`.
  335. axis : int, optional
  336. Axis over which to compute the inverse FFT. If not given, the last
  337. axis is used.
  338. norm : {"backward", "ortho", "forward"}, optional
  339. Normalization mode (see `fft`). Default is "backward".
  340. overwrite_x : bool, optional
  341. If True, the contents of `x` can be destroyed; the default is False.
  342. See :func:`fft` for more details.
  343. workers : int, optional
  344. Maximum number of workers to use for parallel computation. If negative,
  345. the value wraps around from ``os.cpu_count()``.
  346. See :func:`~scipy.fft.fft` for more details.
  347. plan : object, optional
  348. This argument is reserved for passing in a precomputed plan provided
  349. by downstream FFT vendors. It is currently not used in SciPy.
  350. .. versionadded:: 1.5.0
  351. Returns
  352. -------
  353. out : ndarray
  354. The truncated or zero-padded input, transformed along the axis
  355. indicated by `axis`, or the last one if `axis` is not specified.
  356. The length of the transformed axis is `n`, or, if `n` is not given,
  357. ``2*(m-1)`` where ``m`` is the length of the transformed axis of the
  358. input. To get an odd number of output points, `n` must be specified.
  359. Raises
  360. ------
  361. IndexError
  362. If `axis` is larger than the last axis of `x`.
  363. See Also
  364. --------
  365. rfft : The 1-D FFT of real input, of which `irfft` is inverse.
  366. fft : The 1-D FFT.
  367. irfft2 : The inverse of the 2-D FFT of real input.
  368. irfftn : The inverse of the N-D FFT of real input.
  369. Notes
  370. -----
  371. Returns the real valued `n`-point inverse discrete Fourier transform
  372. of `x`, where `x` contains the non-negative frequency terms of a
  373. Hermitian-symmetric sequence. `n` is the length of the result, not the
  374. input.
  375. If you specify an `n` such that `a` must be zero-padded or truncated, the
  376. extra/removed values will be added/removed at high frequencies. One can
  377. thus resample a series to `m` points via Fourier interpolation by:
  378. ``a_resamp = irfft(rfft(a), m)``.
  379. The default value of `n` assumes an even output length. By the Hermitian
  380. symmetry, the last imaginary component must be 0 and so is ignored. To
  381. avoid losing information, the correct length of the real input *must* be
  382. given.
  383. Examples
  384. --------
  385. >>> import scipy.fft
  386. >>> scipy.fft.ifft([1, -1j, -1, 1j])
  387. array([0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j]) # may vary
  388. >>> scipy.fft.irfft([1, -1j, -1])
  389. array([0., 1., 0., 0.])
  390. Notice how the last term in the input to the ordinary `ifft` is the
  391. complex conjugate of the second term, and the output has zero imaginary
  392. part everywhere. When calling `irfft`, the negative frequencies are not
  393. specified, and the output array is purely real.
  394. """
  395. return (Dispatchable(x, np.ndarray),)
  396. @xp_capabilities(allow_dask_compute=True)
  397. @_dispatch
  398. def hfft(x, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, *,
  399. plan=None):
  400. """
  401. Compute the FFT of a signal that has Hermitian symmetry, i.e., a real
  402. spectrum.
  403. Parameters
  404. ----------
  405. x : array_like
  406. The input array.
  407. n : int, optional
  408. Length of the transformed axis of the output. For `n` output
  409. points, ``n//2 + 1`` input points are necessary. If the input is
  410. longer than this, it is cropped. If it is shorter than this, it is
  411. padded with zeros. If `n` is not given, it is taken to be ``2*(m-1)``,
  412. where ``m`` is the length of the input along the axis specified by
  413. `axis`.
  414. axis : int, optional
  415. Axis over which to compute the FFT. If not given, the last
  416. axis is used.
  417. norm : {"backward", "ortho", "forward"}, optional
  418. Normalization mode (see `fft`). Default is "backward".
  419. overwrite_x : bool, optional
  420. If True, the contents of `x` can be destroyed; the default is False.
  421. See `fft` for more details.
  422. workers : int, optional
  423. Maximum number of workers to use for parallel computation. If negative,
  424. the value wraps around from ``os.cpu_count()``.
  425. See :func:`~scipy.fft.fft` for more details.
  426. plan : object, optional
  427. This argument is reserved for passing in a precomputed plan provided
  428. by downstream FFT vendors. It is currently not used in SciPy.
  429. .. versionadded:: 1.5.0
  430. Returns
  431. -------
  432. out : ndarray
  433. The truncated or zero-padded input, transformed along the axis
  434. indicated by `axis`, or the last one if `axis` is not specified.
  435. The length of the transformed axis is `n`, or, if `n` is not given,
  436. ``2*m - 2``, where ``m`` is the length of the transformed axis of
  437. the input. To get an odd number of output points, `n` must be
  438. specified, for instance, as ``2*m - 1`` in the typical case,
  439. Raises
  440. ------
  441. IndexError
  442. If `axis` is larger than the last axis of `a`.
  443. See Also
  444. --------
  445. rfft : Compute the 1-D FFT for real input.
  446. ihfft : The inverse of `hfft`.
  447. hfftn : Compute the N-D FFT of a Hermitian signal.
  448. Notes
  449. -----
  450. `hfft`/`ihfft` are a pair analogous to `rfft`/`irfft`, but for the
  451. opposite case: here the signal has Hermitian symmetry in the time
  452. domain and is real in the frequency domain. So, here, it's `hfft`, for
  453. which you must supply the length of the result if it is to be odd.
  454. * even: ``ihfft(hfft(a, 2*len(a) - 2) == a``, within roundoff error,
  455. * odd: ``ihfft(hfft(a, 2*len(a) - 1) == a``, within roundoff error.
  456. Examples
  457. --------
  458. >>> from scipy.fft import fft, hfft
  459. >>> import numpy as np
  460. >>> a = 2 * np.pi * np.arange(10) / 10
  461. >>> signal = np.cos(a) + 3j * np.sin(3 * a)
  462. >>> fft(signal).round(10)
  463. array([ -0.+0.j, 5.+0.j, -0.+0.j, 15.-0.j, 0.+0.j, 0.+0.j,
  464. -0.+0.j, -15.-0.j, 0.+0.j, 5.+0.j])
  465. >>> hfft(signal[:6]).round(10) # Input first half of signal
  466. array([ 0., 5., 0., 15., -0., 0., 0., -15., -0., 5.])
  467. >>> hfft(signal, 10) # Input entire signal and truncate
  468. array([ 0., 5., 0., 15., -0., 0., 0., -15., -0., 5.])
  469. """
  470. return (Dispatchable(x, np.ndarray),)
  471. @xp_capabilities(allow_dask_compute=True)
  472. @_dispatch
  473. def ihfft(x, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, *,
  474. plan=None):
  475. """
  476. Compute the inverse FFT of a signal that has Hermitian symmetry.
  477. Parameters
  478. ----------
  479. x : array_like
  480. Input array.
  481. n : int, optional
  482. Length of the inverse FFT, the number of points along
  483. transformation axis in the input to use. If `n` is smaller than
  484. the length of the input, the input is cropped. If it is larger,
  485. the input is padded with zeros. If `n` is not given, the length of
  486. the input along the axis specified by `axis` is used.
  487. axis : int, optional
  488. Axis over which to compute the inverse FFT. If not given, the last
  489. axis is used.
  490. norm : {"backward", "ortho", "forward"}, optional
  491. Normalization mode (see `fft`). Default is "backward".
  492. overwrite_x : bool, optional
  493. If True, the contents of `x` can be destroyed; the default is False.
  494. See `fft` for more details.
  495. workers : int, optional
  496. Maximum number of workers to use for parallel computation. If negative,
  497. the value wraps around from ``os.cpu_count()``.
  498. See :func:`~scipy.fft.fft` for more details.
  499. plan : object, optional
  500. This argument is reserved for passing in a precomputed plan provided
  501. by downstream FFT vendors. It is currently not used in SciPy.
  502. .. versionadded:: 1.5.0
  503. Returns
  504. -------
  505. out : complex ndarray
  506. The truncated or zero-padded input, transformed along the axis
  507. indicated by `axis`, or the last one if `axis` is not specified.
  508. The length of the transformed axis is ``n//2 + 1``.
  509. See Also
  510. --------
  511. hfft, irfft
  512. Notes
  513. -----
  514. `hfft`/`ihfft` are a pair analogous to `rfft`/`irfft`, but for the
  515. opposite case: here, the signal has Hermitian symmetry in the time
  516. domain and is real in the frequency domain. So, here, it's `hfft`, for
  517. which you must supply the length of the result if it is to be odd:
  518. * even: ``ihfft(hfft(a, 2*len(a) - 2) == a``, within roundoff error,
  519. * odd: ``ihfft(hfft(a, 2*len(a) - 1) == a``, within roundoff error.
  520. Examples
  521. --------
  522. >>> from scipy.fft import ifft, ihfft
  523. >>> import numpy as np
  524. >>> spectrum = np.array([ 15, -4, 0, -1, 0, -4])
  525. >>> ifft(spectrum)
  526. array([1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j, 3.+0.j, 2.+0.j]) # may vary
  527. >>> ihfft(spectrum)
  528. array([ 1.-0.j, 2.-0.j, 3.-0.j, 4.-0.j]) # may vary
  529. """
  530. return (Dispatchable(x, np.ndarray),)
  531. @xp_capabilities(allow_dask_compute=True)
  532. @_dispatch
  533. def fftn(x, s=None, axes=None, norm=None, overwrite_x=False, workers=None, *,
  534. plan=None):
  535. """
  536. Compute the N-D discrete Fourier Transform.
  537. This function computes the N-D discrete Fourier Transform over
  538. any number of axes in an M-D array by means of the Fast Fourier
  539. Transform (FFT).
  540. Parameters
  541. ----------
  542. x : array_like
  543. Input array, can be complex.
  544. s : sequence of ints, optional
  545. Shape (length of each transformed axis) of the output
  546. (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.).
  547. This corresponds to ``n`` for ``fft(x, n)``.
  548. Along any axis, if the given shape is smaller than that of the input,
  549. the input is cropped. If it is larger, the input is padded with zeros.
  550. if `s` is not given, the shape of the input along the axes specified
  551. by `axes` is used.
  552. axes : sequence of ints, optional
  553. Axes over which to compute the FFT. If not given, the last ``len(s)``
  554. axes are used, or all axes if `s` is also not specified.
  555. norm : {"backward", "ortho", "forward"}, optional
  556. Normalization mode (see `fft`). Default is "backward".
  557. overwrite_x : bool, optional
  558. If True, the contents of `x` can be destroyed; the default is False.
  559. See :func:`fft` for more details.
  560. workers : int, optional
  561. Maximum number of workers to use for parallel computation. If negative,
  562. the value wraps around from ``os.cpu_count()``.
  563. See :func:`~scipy.fft.fft` for more details.
  564. plan : object, optional
  565. This argument is reserved for passing in a precomputed plan provided
  566. by downstream FFT vendors. It is currently not used in SciPy.
  567. .. versionadded:: 1.5.0
  568. Returns
  569. -------
  570. out : complex ndarray
  571. The truncated or zero-padded input, transformed along the axes
  572. indicated by `axes`, or by a combination of `s` and `x`,
  573. as explained in the parameters section above.
  574. Raises
  575. ------
  576. ValueError
  577. If `s` and `axes` have different length.
  578. IndexError
  579. If an element of `axes` is larger than the number of axes of `x`.
  580. See Also
  581. --------
  582. ifftn : The inverse of `fftn`, the inverse N-D FFT.
  583. fft : The 1-D FFT, with definitions and conventions used.
  584. rfftn : The N-D FFT of real input.
  585. fft2 : The 2-D FFT.
  586. fftshift : Shifts zero-frequency terms to centre of array.
  587. Notes
  588. -----
  589. The output, analogously to `fft`, contains the term for zero frequency in
  590. the low-order corner of all axes, the positive frequency terms in the
  591. first half of all axes, the term for the Nyquist frequency in the middle
  592. of all axes and the negative frequency terms in the second half of all
  593. axes, in order of decreasingly negative frequency.
  594. Examples
  595. --------
  596. >>> import scipy.fft
  597. >>> import numpy as np
  598. >>> x = np.mgrid[:3, :3, :3][0]
  599. >>> scipy.fft.fftn(x, axes=(1, 2))
  600. array([[[ 0.+0.j, 0.+0.j, 0.+0.j], # may vary
  601. [ 0.+0.j, 0.+0.j, 0.+0.j],
  602. [ 0.+0.j, 0.+0.j, 0.+0.j]],
  603. [[ 9.+0.j, 0.+0.j, 0.+0.j],
  604. [ 0.+0.j, 0.+0.j, 0.+0.j],
  605. [ 0.+0.j, 0.+0.j, 0.+0.j]],
  606. [[18.+0.j, 0.+0.j, 0.+0.j],
  607. [ 0.+0.j, 0.+0.j, 0.+0.j],
  608. [ 0.+0.j, 0.+0.j, 0.+0.j]]])
  609. >>> scipy.fft.fftn(x, (2, 2), axes=(0, 1))
  610. array([[[ 2.+0.j, 2.+0.j, 2.+0.j], # may vary
  611. [ 0.+0.j, 0.+0.j, 0.+0.j]],
  612. [[-2.+0.j, -2.+0.j, -2.+0.j],
  613. [ 0.+0.j, 0.+0.j, 0.+0.j]]])
  614. >>> import matplotlib.pyplot as plt
  615. >>> rng = np.random.default_rng()
  616. >>> [X, Y] = np.meshgrid(2 * np.pi * np.arange(200) / 12,
  617. ... 2 * np.pi * np.arange(200) / 34)
  618. >>> S = np.sin(X) + np.cos(Y) + rng.uniform(0, 1, X.shape)
  619. >>> FS = scipy.fft.fftn(S)
  620. >>> plt.imshow(np.log(np.abs(scipy.fft.fftshift(FS))**2))
  621. <matplotlib.image.AxesImage object at 0x...>
  622. >>> plt.show()
  623. """
  624. return (Dispatchable(x, np.ndarray),)
  625. @xp_capabilities(allow_dask_compute=True)
  626. @_dispatch
  627. def ifftn(x, s=None, axes=None, norm=None, overwrite_x=False, workers=None, *,
  628. plan=None):
  629. """
  630. Compute the N-D inverse discrete Fourier Transform.
  631. This function computes the inverse of the N-D discrete
  632. Fourier Transform over any number of axes in an M-D array by
  633. means of the Fast Fourier Transform (FFT). In other words,
  634. ``ifftn(fftn(x)) == x`` to within numerical accuracy.
  635. The input, analogously to `ifft`, should be ordered in the same way as is
  636. returned by `fftn`, i.e., it should have the term for zero frequency
  637. in all axes in the low-order corner, the positive frequency terms in the
  638. first half of all axes, the term for the Nyquist frequency in the middle
  639. of all axes and the negative frequency terms in the second half of all
  640. axes, in order of decreasingly negative frequency.
  641. Parameters
  642. ----------
  643. x : array_like
  644. Input array, can be complex.
  645. s : sequence of ints, optional
  646. Shape (length of each transformed axis) of the output
  647. (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.).
  648. This corresponds to ``n`` for ``ifft(x, n)``.
  649. Along any axis, if the given shape is smaller than that of the input,
  650. the input is cropped. If it is larger, the input is padded with zeros.
  651. if `s` is not given, the shape of the input along the axes specified
  652. by `axes` is used. See notes for issue on `ifft` zero padding.
  653. axes : sequence of ints, optional
  654. Axes over which to compute the IFFT. If not given, the last ``len(s)``
  655. axes are used, or all axes if `s` is also not specified.
  656. norm : {"backward", "ortho", "forward"}, optional
  657. Normalization mode (see `fft`). Default is "backward".
  658. overwrite_x : bool, optional
  659. If True, the contents of `x` can be destroyed; the default is False.
  660. See :func:`fft` for more details.
  661. workers : int, optional
  662. Maximum number of workers to use for parallel computation. If negative,
  663. the value wraps around from ``os.cpu_count()``.
  664. See :func:`~scipy.fft.fft` for more details.
  665. plan : object, optional
  666. This argument is reserved for passing in a precomputed plan provided
  667. by downstream FFT vendors. It is currently not used in SciPy.
  668. .. versionadded:: 1.5.0
  669. Returns
  670. -------
  671. out : complex ndarray
  672. The truncated or zero-padded input, transformed along the axes
  673. indicated by `axes`, or by a combination of `s` or `x`,
  674. as explained in the parameters section above.
  675. Raises
  676. ------
  677. ValueError
  678. If `s` and `axes` have different length.
  679. IndexError
  680. If an element of `axes` is larger than the number of axes of `x`.
  681. See Also
  682. --------
  683. fftn : The forward N-D FFT, of which `ifftn` is the inverse.
  684. ifft : The 1-D inverse FFT.
  685. ifft2 : The 2-D inverse FFT.
  686. ifftshift : Undoes `fftshift`, shifts zero-frequency terms to beginning
  687. of array.
  688. Notes
  689. -----
  690. Zero-padding, analogously with `ifft`, is performed by appending zeros to
  691. the input along the specified dimension. Although this is the common
  692. approach, it might lead to surprising results. If another form of zero
  693. padding is desired, it must be performed before `ifftn` is called.
  694. Examples
  695. --------
  696. >>> import scipy.fft
  697. >>> import numpy as np
  698. >>> x = np.eye(4)
  699. >>> scipy.fft.ifftn(scipy.fft.fftn(x, axes=(0,)), axes=(1,))
  700. array([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], # may vary
  701. [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
  702. [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
  703. [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j]])
  704. Create and plot an image with band-limited frequency content:
  705. >>> import matplotlib.pyplot as plt
  706. >>> rng = np.random.default_rng()
  707. >>> n = np.zeros((200,200), dtype=complex)
  708. >>> n[60:80, 20:40] = np.exp(1j*rng.uniform(0, 2*np.pi, (20, 20)))
  709. >>> im = scipy.fft.ifftn(n).real
  710. >>> plt.imshow(im)
  711. <matplotlib.image.AxesImage object at 0x...>
  712. >>> plt.show()
  713. """
  714. return (Dispatchable(x, np.ndarray),)
  715. @xp_capabilities(allow_dask_compute=True)
  716. @_dispatch
  717. def fft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, workers=None, *,
  718. plan=None):
  719. """
  720. Compute the 2-D discrete Fourier Transform
  721. This function computes the N-D discrete Fourier Transform
  722. over any axes in an M-D array by means of the
  723. Fast Fourier Transform (FFT). By default, the transform is computed over
  724. the last two axes of the input array, i.e., a 2-dimensional FFT.
  725. Parameters
  726. ----------
  727. x : array_like
  728. Input array, can be complex
  729. s : sequence of ints, optional
  730. Shape (length of each transformed axis) of the output
  731. (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.).
  732. This corresponds to ``n`` for ``fft(x, n)``.
  733. Along each axis, if the given shape is smaller than that of the input,
  734. the input is cropped. If it is larger, the input is padded with zeros.
  735. if `s` is not given, the shape of the input along the axes specified
  736. by `axes` is used.
  737. axes : sequence of ints, optional
  738. Axes over which to compute the FFT. If not given, the last two axes are
  739. used.
  740. norm : {"backward", "ortho", "forward"}, optional
  741. Normalization mode (see `fft`). Default is "backward".
  742. overwrite_x : bool, optional
  743. If True, the contents of `x` can be destroyed; the default is False.
  744. See :func:`fft` for more details.
  745. workers : int, optional
  746. Maximum number of workers to use for parallel computation. If negative,
  747. the value wraps around from ``os.cpu_count()``.
  748. See :func:`~scipy.fft.fft` for more details.
  749. plan : object, optional
  750. This argument is reserved for passing in a precomputed plan provided
  751. by downstream FFT vendors. It is currently not used in SciPy.
  752. .. versionadded:: 1.5.0
  753. Returns
  754. -------
  755. out : complex ndarray
  756. The truncated or zero-padded input, transformed along the axes
  757. indicated by `axes`, or the last two axes if `axes` is not given.
  758. Raises
  759. ------
  760. ValueError
  761. If `s` and `axes` have different length, or `axes` not given and
  762. ``len(s) != 2``.
  763. IndexError
  764. If an element of `axes` is larger than the number of axes of `x`.
  765. See Also
  766. --------
  767. ifft2 : The inverse 2-D FFT.
  768. fft : The 1-D FFT.
  769. fftn : The N-D FFT.
  770. fftshift : Shifts zero-frequency terms to the center of the array.
  771. For 2-D input, swaps first and third quadrants, and second
  772. and fourth quadrants.
  773. Notes
  774. -----
  775. `fft2` is just `fftn` with a different default for `axes`.
  776. The output, analogously to `fft`, contains the term for zero frequency in
  777. the low-order corner of the transformed axes, the positive frequency terms
  778. in the first half of these axes, the term for the Nyquist frequency in the
  779. middle of the axes and the negative frequency terms in the second half of
  780. the axes, in order of decreasingly negative frequency.
  781. See `fftn` for details and a plotting example, and `fft` for
  782. definitions and conventions used.
  783. Examples
  784. --------
  785. >>> import scipy.fft
  786. >>> import numpy as np
  787. >>> x = np.mgrid[:5, :5][0]
  788. >>> scipy.fft.fft2(x)
  789. array([[ 50. +0.j , 0. +0.j , 0. +0.j , # may vary
  790. 0. +0.j , 0. +0.j ],
  791. [-12.5+17.20477401j, 0. +0.j , 0. +0.j ,
  792. 0. +0.j , 0. +0.j ],
  793. [-12.5 +4.0614962j , 0. +0.j , 0. +0.j ,
  794. 0. +0.j , 0. +0.j ],
  795. [-12.5 -4.0614962j , 0. +0.j , 0. +0.j ,
  796. 0. +0.j , 0. +0.j ],
  797. [-12.5-17.20477401j, 0. +0.j , 0. +0.j ,
  798. 0. +0.j , 0. +0.j ]])
  799. """
  800. return (Dispatchable(x, np.ndarray),)
  801. @xp_capabilities(allow_dask_compute=True)
  802. @_dispatch
  803. def ifft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, workers=None, *,
  804. plan=None):
  805. """
  806. Compute the 2-D inverse discrete Fourier Transform.
  807. This function computes the inverse of the 2-D discrete Fourier
  808. Transform over any number of axes in an M-D array by means of
  809. the Fast Fourier Transform (FFT). In other words, ``ifft2(fft2(x)) == x``
  810. to within numerical accuracy. By default, the inverse transform is
  811. computed over the last two axes of the input array.
  812. The input, analogously to `ifft`, should be ordered in the same way as is
  813. returned by `fft2`, i.e., it should have the term for zero frequency
  814. in the low-order corner of the two axes, the positive frequency terms in
  815. the first half of these axes, the term for the Nyquist frequency in the
  816. middle of the axes and the negative frequency terms in the second half of
  817. both axes, in order of decreasingly negative frequency.
  818. Parameters
  819. ----------
  820. x : array_like
  821. Input array, can be complex.
  822. s : sequence of ints, optional
  823. Shape (length of each axis) of the output (``s[0]`` refers to axis 0,
  824. ``s[1]`` to axis 1, etc.). This corresponds to `n` for ``ifft(x, n)``.
  825. Along each axis, if the given shape is smaller than that of the input,
  826. the input is cropped. If it is larger, the input is padded with zeros.
  827. if `s` is not given, the shape of the input along the axes specified
  828. by `axes` is used. See notes for issue on `ifft` zero padding.
  829. axes : sequence of ints, optional
  830. Axes over which to compute the FFT. If not given, the last two
  831. axes are used.
  832. norm : {"backward", "ortho", "forward"}, optional
  833. Normalization mode (see `fft`). Default is "backward".
  834. overwrite_x : bool, optional
  835. If True, the contents of `x` can be destroyed; the default is False.
  836. See :func:`fft` for more details.
  837. workers : int, optional
  838. Maximum number of workers to use for parallel computation. If negative,
  839. the value wraps around from ``os.cpu_count()``.
  840. See :func:`~scipy.fft.fft` for more details.
  841. plan : object, optional
  842. This argument is reserved for passing in a precomputed plan provided
  843. by downstream FFT vendors. It is currently not used in SciPy.
  844. .. versionadded:: 1.5.0
  845. Returns
  846. -------
  847. out : complex ndarray
  848. The truncated or zero-padded input, transformed along the axes
  849. indicated by `axes`, or the last two axes if `axes` is not given.
  850. Raises
  851. ------
  852. ValueError
  853. If `s` and `axes` have different length, or `axes` not given and
  854. ``len(s) != 2``.
  855. IndexError
  856. If an element of `axes` is larger than the number of axes of `x`.
  857. See Also
  858. --------
  859. fft2 : The forward 2-D FFT, of which `ifft2` is the inverse.
  860. ifftn : The inverse of the N-D FFT.
  861. fft : The 1-D FFT.
  862. ifft : The 1-D inverse FFT.
  863. Notes
  864. -----
  865. `ifft2` is just `ifftn` with a different default for `axes`.
  866. See `ifftn` for details and a plotting example, and `fft` for
  867. definition and conventions used.
  868. Zero-padding, analogously with `ifft`, is performed by appending zeros to
  869. the input along the specified dimension. Although this is the common
  870. approach, it might lead to surprising results. If another form of zero
  871. padding is desired, it must be performed before `ifft2` is called.
  872. Examples
  873. --------
  874. >>> import scipy.fft
  875. >>> import numpy as np
  876. >>> x = 4 * np.eye(4)
  877. >>> scipy.fft.ifft2(x)
  878. array([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], # may vary
  879. [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],
  880. [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
  881. [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j]])
  882. """
  883. return (Dispatchable(x, np.ndarray),)
  884. @xp_capabilities(allow_dask_compute=True)
  885. @_dispatch
  886. def rfftn(x, s=None, axes=None, norm=None, overwrite_x=False, workers=None, *,
  887. plan=None):
  888. """
  889. Compute the N-D discrete Fourier Transform for real input.
  890. This function computes the N-D discrete Fourier Transform over
  891. any number of axes in an M-D real array by means of the Fast
  892. Fourier Transform (FFT). By default, all axes are transformed, with the
  893. real transform performed over the last axis, while the remaining
  894. transforms are complex.
  895. Parameters
  896. ----------
  897. x : array_like
  898. Input array, taken to be real.
  899. s : sequence of ints, optional
  900. Shape (length along each transformed axis) to use from the input.
  901. (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.).
  902. The final element of `s` corresponds to `n` for ``rfft(x, n)``, while
  903. for the remaining axes, it corresponds to `n` for ``fft(x, n)``.
  904. Along any axis, if the given shape is smaller than that of the input,
  905. the input is cropped. If it is larger, the input is padded with zeros.
  906. if `s` is not given, the shape of the input along the axes specified
  907. by `axes` is used.
  908. axes : sequence of ints, optional
  909. Axes over which to compute the FFT. If not given, the last ``len(s)``
  910. axes are used, or all axes if `s` is also not specified.
  911. norm : {"backward", "ortho", "forward"}, optional
  912. Normalization mode (see `fft`). Default is "backward".
  913. overwrite_x : bool, optional
  914. If True, the contents of `x` can be destroyed; the default is False.
  915. See :func:`fft` for more details.
  916. workers : int, optional
  917. Maximum number of workers to use for parallel computation. If negative,
  918. the value wraps around from ``os.cpu_count()``.
  919. See :func:`~scipy.fft.fft` for more details.
  920. plan : object, optional
  921. This argument is reserved for passing in a precomputed plan provided
  922. by downstream FFT vendors. It is currently not used in SciPy.
  923. .. versionadded:: 1.5.0
  924. Returns
  925. -------
  926. out : complex ndarray
  927. The truncated or zero-padded input, transformed along the axes
  928. indicated by `axes`, or by a combination of `s` and `x`,
  929. as explained in the parameters section above.
  930. The length of the last axis transformed will be ``s[-1]//2+1``,
  931. while the remaining transformed axes will have lengths according to
  932. `s`, or unchanged from the input.
  933. Raises
  934. ------
  935. ValueError
  936. If `s` and `axes` have different length.
  937. IndexError
  938. If an element of `axes` is larger than the number of axes of `x`.
  939. See Also
  940. --------
  941. irfftn : The inverse of `rfftn`, i.e., the inverse of the N-D FFT
  942. of real input.
  943. fft : The 1-D FFT, with definitions and conventions used.
  944. rfft : The 1-D FFT of real input.
  945. fftn : The N-D FFT.
  946. rfft2 : The 2-D FFT of real input.
  947. Notes
  948. -----
  949. The transform for real input is performed over the last transformation
  950. axis, as by `rfft`, then the transform over the remaining axes is
  951. performed as by `fftn`. The order of the output is as for `rfft` for the
  952. final transformation axis, and as for `fftn` for the remaining
  953. transformation axes.
  954. See `fft` for details, definitions and conventions used.
  955. Examples
  956. --------
  957. >>> import scipy.fft
  958. >>> import numpy as np
  959. >>> x = np.ones((2, 2, 2))
  960. >>> scipy.fft.rfftn(x)
  961. array([[[8.+0.j, 0.+0.j], # may vary
  962. [0.+0.j, 0.+0.j]],
  963. [[0.+0.j, 0.+0.j],
  964. [0.+0.j, 0.+0.j]]])
  965. >>> scipy.fft.rfftn(x, axes=(2, 0))
  966. array([[[4.+0.j, 0.+0.j], # may vary
  967. [4.+0.j, 0.+0.j]],
  968. [[0.+0.j, 0.+0.j],
  969. [0.+0.j, 0.+0.j]]])
  970. """
  971. return (Dispatchable(x, np.ndarray),)
  972. @xp_capabilities(allow_dask_compute=True)
  973. @_dispatch
  974. def rfft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, workers=None, *,
  975. plan=None):
  976. """
  977. Compute the 2-D FFT of a real array.
  978. Parameters
  979. ----------
  980. x : array
  981. Input array, taken to be real.
  982. s : sequence of ints, optional
  983. Shape of the FFT.
  984. axes : sequence of ints, optional
  985. Axes over which to compute the FFT.
  986. norm : {"backward", "ortho", "forward"}, optional
  987. Normalization mode (see `fft`). Default is "backward".
  988. overwrite_x : bool, optional
  989. If True, the contents of `x` can be destroyed; the default is False.
  990. See :func:`fft` for more details.
  991. workers : int, optional
  992. Maximum number of workers to use for parallel computation. If negative,
  993. the value wraps around from ``os.cpu_count()``.
  994. See :func:`~scipy.fft.fft` for more details.
  995. plan : object, optional
  996. This argument is reserved for passing in a precomputed plan provided
  997. by downstream FFT vendors. It is currently not used in SciPy.
  998. .. versionadded:: 1.5.0
  999. Returns
  1000. -------
  1001. out : ndarray
  1002. The result of the real 2-D FFT.
  1003. See Also
  1004. --------
  1005. irfft2 : The inverse of the 2-D FFT of real input.
  1006. rfft : The 1-D FFT of real input.
  1007. rfftn : Compute the N-D discrete Fourier Transform for real
  1008. input.
  1009. Notes
  1010. -----
  1011. This is really just `rfftn` with different default behavior.
  1012. For more details see `rfftn`.
  1013. Examples
  1014. --------
  1015. >>> import scipy.fft
  1016. >>> import numpy as np
  1017. >>> x = np.broadcast_to([1, 0, -1, 0], (4, 4))
  1018. >>> scipy.fft.rfft2(x)
  1019. array([[0.+0.j, 8.+0.j, 0.+0.j],
  1020. [0.+0.j, 0.+0.j, 0.+0.j],
  1021. [0.+0.j, 0.+0.j, 0.+0.j],
  1022. [0.+0.j, 0.+0.j, 0.+0.j]])
  1023. """
  1024. return (Dispatchable(x, np.ndarray),)
  1025. @xp_capabilities(allow_dask_compute=True)
  1026. @_dispatch
  1027. def irfftn(x, s=None, axes=None, norm=None, overwrite_x=False, workers=None, *,
  1028. plan=None):
  1029. """
  1030. Computes the inverse of `rfftn`
  1031. This function computes the inverse of the N-D discrete
  1032. Fourier Transform for real input over any number of axes in an
  1033. M-D array by means of the Fast Fourier Transform (FFT). In
  1034. other words, ``irfftn(rfftn(x), x.shape) == x`` to within numerical
  1035. accuracy. (The ``a.shape`` is necessary like ``len(a)`` is for `irfft`,
  1036. and for the same reason.)
  1037. The input should be ordered in the same way as is returned by `rfftn`,
  1038. i.e., as for `irfft` for the final transformation axis, and as for `ifftn`
  1039. along all the other axes.
  1040. Parameters
  1041. ----------
  1042. x : array_like
  1043. Input array.
  1044. s : sequence of ints, optional
  1045. Shape (length of each transformed axis) of the output
  1046. (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.). `s` is also the
  1047. number of input points used along this axis, except for the last axis,
  1048. where ``s[-1]//2+1`` points of the input are used.
  1049. Along any axis, if the shape indicated by `s` is smaller than that of
  1050. the input, the input is cropped. If it is larger, the input is padded
  1051. with zeros. If `s` is not given, the shape of the input along the axes
  1052. specified by axes is used. Except for the last axis which is taken to be
  1053. ``2*(m-1)``, where ``m`` is the length of the input along that axis.
  1054. axes : sequence of ints, optional
  1055. Axes over which to compute the inverse FFT. If not given, the last
  1056. `len(s)` axes are used, or all axes if `s` is also not specified.
  1057. norm : {"backward", "ortho", "forward"}, optional
  1058. Normalization mode (see `fft`). Default is "backward".
  1059. overwrite_x : bool, optional
  1060. If True, the contents of `x` can be destroyed; the default is False.
  1061. See :func:`fft` for more details.
  1062. workers : int, optional
  1063. Maximum number of workers to use for parallel computation. If negative,
  1064. the value wraps around from ``os.cpu_count()``.
  1065. See :func:`~scipy.fft.fft` for more details.
  1066. plan : object, optional
  1067. This argument is reserved for passing in a precomputed plan provided
  1068. by downstream FFT vendors. It is currently not used in SciPy.
  1069. .. versionadded:: 1.5.0
  1070. Returns
  1071. -------
  1072. out : ndarray
  1073. The truncated or zero-padded input, transformed along the axes
  1074. indicated by `axes`, or by a combination of `s` or `x`,
  1075. as explained in the parameters section above.
  1076. The length of each transformed axis is as given by the corresponding
  1077. element of `s`, or the length of the input in every axis except for the
  1078. last one if `s` is not given. In the final transformed axis the length
  1079. of the output when `s` is not given is ``2*(m-1)``, where ``m`` is the
  1080. length of the final transformed axis of the input. To get an odd
  1081. number of output points in the final axis, `s` must be specified.
  1082. Raises
  1083. ------
  1084. ValueError
  1085. If `s` and `axes` have different length.
  1086. IndexError
  1087. If an element of `axes` is larger than the number of axes of `x`.
  1088. See Also
  1089. --------
  1090. rfftn : The forward N-D FFT of real input,
  1091. of which `ifftn` is the inverse.
  1092. fft : The 1-D FFT, with definitions and conventions used.
  1093. irfft : The inverse of the 1-D FFT of real input.
  1094. irfft2 : The inverse of the 2-D FFT of real input.
  1095. Notes
  1096. -----
  1097. See `fft` for definitions and conventions used.
  1098. See `rfft` for definitions and conventions used for real input.
  1099. The default value of `s` assumes an even output length in the final
  1100. transformation axis. When performing the final complex to real
  1101. transformation, the Hermitian symmetry requires that the last imaginary
  1102. component along that axis must be 0 and so it is ignored. To avoid losing
  1103. information, the correct length of the real input *must* be given.
  1104. Examples
  1105. --------
  1106. >>> import scipy.fft
  1107. >>> import numpy as np
  1108. >>> x = np.zeros((3, 2, 2))
  1109. >>> x[0, 0, 0] = 3 * 2 * 2
  1110. >>> scipy.fft.irfftn(x)
  1111. array([[[1., 1.],
  1112. [1., 1.]],
  1113. [[1., 1.],
  1114. [1., 1.]],
  1115. [[1., 1.],
  1116. [1., 1.]]])
  1117. """
  1118. return (Dispatchable(x, np.ndarray),)
  1119. @xp_capabilities(allow_dask_compute=True)
  1120. @_dispatch
  1121. def irfft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, workers=None, *,
  1122. plan=None):
  1123. """
  1124. Computes the inverse of `rfft2`
  1125. Parameters
  1126. ----------
  1127. x : array_like
  1128. The input array
  1129. s : sequence of ints, optional
  1130. Shape of the real output to the inverse FFT.
  1131. axes : sequence of ints, optional
  1132. The axes over which to compute the inverse fft.
  1133. Default is the last two axes.
  1134. norm : {"backward", "ortho", "forward"}, optional
  1135. Normalization mode (see `fft`). Default is "backward".
  1136. overwrite_x : bool, optional
  1137. If True, the contents of `x` can be destroyed; the default is False.
  1138. See :func:`fft` for more details.
  1139. workers : int, optional
  1140. Maximum number of workers to use for parallel computation. If negative,
  1141. the value wraps around from ``os.cpu_count()``.
  1142. See :func:`~scipy.fft.fft` for more details.
  1143. plan : object, optional
  1144. This argument is reserved for passing in a precomputed plan provided
  1145. by downstream FFT vendors. It is currently not used in SciPy.
  1146. .. versionadded:: 1.5.0
  1147. Returns
  1148. -------
  1149. out : ndarray
  1150. The result of the inverse real 2-D FFT.
  1151. See Also
  1152. --------
  1153. rfft2 : The 2-D FFT of real input.
  1154. irfft : The inverse of the 1-D FFT of real input.
  1155. irfftn : The inverse of the N-D FFT of real input.
  1156. Notes
  1157. -----
  1158. This is really `irfftn` with different defaults.
  1159. For more details see `irfftn`.
  1160. """
  1161. return (Dispatchable(x, np.ndarray),)
  1162. @xp_capabilities(allow_dask_compute=True)
  1163. @_dispatch
  1164. def hfftn(x, s=None, axes=None, norm=None, overwrite_x=False, workers=None, *,
  1165. plan=None):
  1166. """
  1167. Compute the N-D FFT of Hermitian symmetric complex input, i.e., a
  1168. signal with a real spectrum.
  1169. This function computes the N-D discrete Fourier Transform for a
  1170. Hermitian symmetric complex input over any number of axes in an
  1171. M-D array by means of the Fast Fourier Transform (FFT). In other
  1172. words, ``ihfftn(hfftn(x, s)) == x`` to within numerical accuracy. (``s``
  1173. here is ``x.shape`` with ``s[-1] = x.shape[-1] * 2 - 1``, this is necessary
  1174. for the same reason ``x.shape`` would be necessary for `irfft`.)
  1175. Parameters
  1176. ----------
  1177. x : array_like
  1178. Input array.
  1179. s : sequence of ints, optional
  1180. Shape (length of each transformed axis) of the output
  1181. (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.). `s` is also the
  1182. number of input points used along this axis, except for the last axis,
  1183. where ``s[-1]//2+1`` points of the input are used.
  1184. Along any axis, if the shape indicated by `s` is smaller than that of
  1185. the input, the input is cropped. If it is larger, the input is padded
  1186. with zeros. If `s` is not given, the shape of the input along the axes
  1187. specified by axes is used. Except for the last axis which is taken to be
  1188. ``2*(m-1)`` where ``m`` is the length of the input along that axis.
  1189. axes : sequence of ints, optional
  1190. Axes over which to compute the inverse FFT. If not given, the last
  1191. `len(s)` axes are used, or all axes if `s` is also not specified.
  1192. norm : {"backward", "ortho", "forward"}, optional
  1193. Normalization mode (see `fft`). Default is "backward".
  1194. overwrite_x : bool, optional
  1195. If True, the contents of `x` can be destroyed; the default is False.
  1196. See :func:`fft` for more details.
  1197. workers : int, optional
  1198. Maximum number of workers to use for parallel computation. If negative,
  1199. the value wraps around from ``os.cpu_count()``.
  1200. See :func:`~scipy.fft.fft` for more details.
  1201. plan : object, optional
  1202. This argument is reserved for passing in a precomputed plan provided
  1203. by downstream FFT vendors. It is currently not used in SciPy.
  1204. .. versionadded:: 1.5.0
  1205. Returns
  1206. -------
  1207. out : ndarray
  1208. The truncated or zero-padded input, transformed along the axes
  1209. indicated by `axes`, or by a combination of `s` or `x`,
  1210. as explained in the parameters section above.
  1211. The length of each transformed axis is as given by the corresponding
  1212. element of `s`, or the length of the input in every axis except for the
  1213. last one if `s` is not given. In the final transformed axis the length
  1214. of the output when `s` is not given is ``2*(m-1)`` where ``m`` is the
  1215. length of the final transformed axis of the input. To get an odd
  1216. number of output points in the final axis, `s` must be specified.
  1217. Raises
  1218. ------
  1219. ValueError
  1220. If `s` and `axes` have different length.
  1221. IndexError
  1222. If an element of `axes` is larger than the number of axes of `x`.
  1223. See Also
  1224. --------
  1225. ihfftn : The inverse N-D FFT with real spectrum. Inverse of `hfftn`.
  1226. fft : The 1-D FFT, with definitions and conventions used.
  1227. rfft : Forward FFT of real input.
  1228. Notes
  1229. -----
  1230. For a 1-D signal ``x`` to have a real spectrum, it must satisfy
  1231. the Hermitian property::
  1232. x[i] == np.conj(x[-i]) for all i
  1233. This generalizes into higher dimensions by reflecting over each axis in
  1234. turn::
  1235. x[i, j, k, ...] == np.conj(x[-i, -j, -k, ...]) for all i, j, k, ...
  1236. This should not be confused with a Hermitian matrix, for which the
  1237. transpose is its own conjugate::
  1238. x[i, j] == np.conj(x[j, i]) for all i, j
  1239. The default value of `s` assumes an even output length in the final
  1240. transformation axis. When performing the final complex to real
  1241. transformation, the Hermitian symmetry requires that the last imaginary
  1242. component along that axis must be 0 and so it is ignored. To avoid losing
  1243. information, the correct length of the real input *must* be given.
  1244. Examples
  1245. --------
  1246. >>> import scipy.fft
  1247. >>> import numpy as np
  1248. >>> x = np.ones((3, 2, 2))
  1249. >>> scipy.fft.hfftn(x)
  1250. array([[[12., 0.],
  1251. [ 0., 0.]],
  1252. [[ 0., 0.],
  1253. [ 0., 0.]],
  1254. [[ 0., 0.],
  1255. [ 0., 0.]]])
  1256. """
  1257. return (Dispatchable(x, np.ndarray),)
  1258. @xp_capabilities(allow_dask_compute=True)
  1259. @_dispatch
  1260. def hfft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, workers=None, *,
  1261. plan=None):
  1262. """
  1263. Compute the 2-D FFT of a Hermitian complex array.
  1264. Parameters
  1265. ----------
  1266. x : array
  1267. Input array, taken to be Hermitian complex.
  1268. s : sequence of ints, optional
  1269. Shape of the real output.
  1270. axes : sequence of ints, optional
  1271. Axes over which to compute the FFT.
  1272. norm : {"backward", "ortho", "forward"}, optional
  1273. Normalization mode (see `fft`). Default is "backward".
  1274. overwrite_x : bool, optional
  1275. If True, the contents of `x` can be destroyed; the default is False.
  1276. See `fft` for more details.
  1277. workers : int, optional
  1278. Maximum number of workers to use for parallel computation. If negative,
  1279. the value wraps around from ``os.cpu_count()``.
  1280. See :func:`~scipy.fft.fft` for more details.
  1281. plan : object, optional
  1282. This argument is reserved for passing in a precomputed plan provided
  1283. by downstream FFT vendors. It is currently not used in SciPy.
  1284. .. versionadded:: 1.5.0
  1285. Returns
  1286. -------
  1287. out : ndarray
  1288. The real result of the 2-D Hermitian complex real FFT.
  1289. See Also
  1290. --------
  1291. hfftn : Compute the N-D discrete Fourier Transform for Hermitian
  1292. complex input.
  1293. Notes
  1294. -----
  1295. This is really just `hfftn` with different default behavior.
  1296. For more details see `hfftn`.
  1297. Examples
  1298. --------
  1299. >>> import scipy.fft
  1300. >>> import numpy as np
  1301. >>> x = np.array([[1+0j, 2+0j], [2+0j, 1+0j]]) # Hermitian-symmetric input
  1302. >>> scipy.fft.hfft2(x, s=(2, 2))
  1303. array([[ 6., 0.],
  1304. [ 0., -2.]])
  1305. """
  1306. return (Dispatchable(x, np.ndarray),)
  1307. @xp_capabilities(allow_dask_compute=True)
  1308. @_dispatch
  1309. def ihfftn(x, s=None, axes=None, norm=None, overwrite_x=False, workers=None, *,
  1310. plan=None):
  1311. """
  1312. Compute the N-D inverse discrete Fourier Transform for a real
  1313. spectrum.
  1314. This function computes the N-D inverse discrete Fourier Transform
  1315. over any number of axes in an M-D real array by means of the Fast
  1316. Fourier Transform (FFT). By default, all axes are transformed, with the
  1317. real transform performed over the last axis, while the remaining transforms
  1318. are complex.
  1319. Parameters
  1320. ----------
  1321. x : array_like
  1322. Input array, taken to be real.
  1323. s : sequence of ints, optional
  1324. Shape (length along each transformed axis) to use from the input.
  1325. (``s[0]`` refers to axis 0, ``s[1]`` to axis 1, etc.).
  1326. Along any axis, if the given shape is smaller than that of the input,
  1327. the input is cropped. If it is larger, the input is padded with zeros.
  1328. if `s` is not given, the shape of the input along the axes specified
  1329. by `axes` is used.
  1330. axes : sequence of ints, optional
  1331. Axes over which to compute the FFT. If not given, the last ``len(s)``
  1332. axes are used, or all axes if `s` is also not specified.
  1333. norm : {"backward", "ortho", "forward"}, optional
  1334. Normalization mode (see `fft`). Default is "backward".
  1335. overwrite_x : bool, optional
  1336. If True, the contents of `x` can be destroyed; the default is False.
  1337. See :func:`fft` for more details.
  1338. workers : int, optional
  1339. Maximum number of workers to use for parallel computation. If negative,
  1340. the value wraps around from ``os.cpu_count()``.
  1341. See :func:`~scipy.fft.fft` for more details.
  1342. plan : object, optional
  1343. This argument is reserved for passing in a precomputed plan provided
  1344. by downstream FFT vendors. It is currently not used in SciPy.
  1345. .. versionadded:: 1.5.0
  1346. Returns
  1347. -------
  1348. out : complex ndarray
  1349. The truncated or zero-padded input, transformed along the axes
  1350. indicated by `axes`, or by a combination of `s` and `x`,
  1351. as explained in the parameters section above.
  1352. The length of the last axis transformed will be ``s[-1]//2+1``,
  1353. while the remaining transformed axes will have lengths according to
  1354. `s`, or unchanged from the input.
  1355. Raises
  1356. ------
  1357. ValueError
  1358. If `s` and `axes` have different length.
  1359. IndexError
  1360. If an element of `axes` is larger than the number of axes of `x`.
  1361. See Also
  1362. --------
  1363. hfftn : The forward N-D FFT of Hermitian input.
  1364. hfft : The 1-D FFT of Hermitian input.
  1365. fft : The 1-D FFT, with definitions and conventions used.
  1366. fftn : The N-D FFT.
  1367. hfft2 : The 2-D FFT of Hermitian input.
  1368. Notes
  1369. -----
  1370. The transform for real input is performed over the last transformation
  1371. axis, as by `ihfft`, then the transform over the remaining axes is
  1372. performed as by `ifftn`. The order of the output is the positive part of
  1373. the Hermitian output signal, in the same format as `rfft`.
  1374. Examples
  1375. --------
  1376. >>> import scipy.fft
  1377. >>> import numpy as np
  1378. >>> x = np.ones((2, 2, 2))
  1379. >>> scipy.fft.ihfftn(x)
  1380. array([[[1.+0.j, 0.+0.j], # may vary
  1381. [0.+0.j, 0.+0.j]],
  1382. [[0.+0.j, 0.+0.j],
  1383. [0.+0.j, 0.+0.j]]])
  1384. >>> scipy.fft.ihfftn(x, axes=(2, 0))
  1385. array([[[1.+0.j, 0.+0.j], # may vary
  1386. [1.+0.j, 0.+0.j]],
  1387. [[0.+0.j, 0.+0.j],
  1388. [0.+0.j, 0.+0.j]]])
  1389. """
  1390. return (Dispatchable(x, np.ndarray),)
  1391. @xp_capabilities(allow_dask_compute=True)
  1392. @_dispatch
  1393. def ihfft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, workers=None, *,
  1394. plan=None):
  1395. """
  1396. Compute the 2-D inverse FFT of a real spectrum.
  1397. Parameters
  1398. ----------
  1399. x : array_like
  1400. The input array
  1401. s : sequence of ints, optional
  1402. Shape of the real input to the inverse FFT.
  1403. axes : sequence of ints, optional
  1404. The axes over which to compute the inverse fft.
  1405. Default is the last two axes.
  1406. norm : {"backward", "ortho", "forward"}, optional
  1407. Normalization mode (see `fft`). Default is "backward".
  1408. overwrite_x : bool, optional
  1409. If True, the contents of `x` can be destroyed; the default is False.
  1410. See :func:`fft` for more details.
  1411. workers : int, optional
  1412. Maximum number of workers to use for parallel computation. If negative,
  1413. the value wraps around from ``os.cpu_count()``.
  1414. See :func:`~scipy.fft.fft` for more details.
  1415. plan : object, optional
  1416. This argument is reserved for passing in a precomputed plan provided
  1417. by downstream FFT vendors. It is currently not used in SciPy.
  1418. .. versionadded:: 1.5.0
  1419. Returns
  1420. -------
  1421. out : ndarray
  1422. The result of the inverse real 2-D FFT.
  1423. See Also
  1424. --------
  1425. ihfftn : Compute the inverse of the N-D FFT of Hermitian input.
  1426. Notes
  1427. -----
  1428. This is really `ihfftn` with different defaults.
  1429. For more details see `ihfftn`.
  1430. """
  1431. return (Dispatchable(x, np.ndarray),)