__init__.py 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. import sys
  2. import torch
  3. from torch._C import _add_docstr, _fft # type: ignore[attr-defined]
  4. from torch._torch_docs import factory_common_args, common_args
  5. __all__ = ['fft', 'ifft', 'fft2', 'ifft2', 'fftn', 'ifftn',
  6. 'rfft', 'irfft', 'rfft2', 'irfft2', 'rfftn', 'irfftn',
  7. 'hfft', 'ihfft', 'fftfreq', 'rfftfreq', 'fftshift', 'ifftshift',
  8. 'Tensor']
  9. Tensor = torch.Tensor
  10. # Note: This not only adds the doc strings for the spectral ops, but
  11. # connects the torch.fft Python namespace to the torch._C._fft builtins.
  12. fft = _add_docstr(_fft.fft_fft, r"""
  13. fft(input, n=None, dim=-1, norm=None, *, out=None) -> Tensor
  14. Computes the one dimensional discrete Fourier transform of :attr:`input`.
  15. Note:
  16. The Fourier domain representation of any real signal satisfies the
  17. Hermitian property: `X[i] = conj(X[-i])`. This function always returns both
  18. the positive and negative frequency terms even though, for real inputs, the
  19. negative frequencies are redundant. :func:`~torch.fft.rfft` returns the
  20. more compact one-sided representation where only the positive frequencies
  21. are returned.
  22. Note:
  23. Supports torch.half and torch.chalf on CUDA with GPU Architecture SM53 or greater.
  24. However it only supports powers of 2 signal length in every transformed dimension.
  25. Args:
  26. input (Tensor): the input tensor
  27. n (int, optional): Signal length. If given, the input will either be zero-padded
  28. or trimmed to this length before computing the FFT.
  29. dim (int, optional): The dimension along which to take the one dimensional FFT.
  30. norm (str, optional): Normalization mode. For the forward transform
  31. (:func:`~torch.fft.fft`), these correspond to:
  32. * ``"forward"`` - normalize by ``1/n``
  33. * ``"backward"`` - no normalization
  34. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the FFT orthonormal)
  35. Calling the backward transform (:func:`~torch.fft.ifft`) with the same
  36. normalization mode will apply an overall normalization of ``1/n`` between
  37. the two transforms. This is required to make :func:`~torch.fft.ifft`
  38. the exact inverse.
  39. Default is ``"backward"`` (no normalization).
  40. Keyword args:
  41. {out}
  42. Example:
  43. >>> t = torch.arange(4)
  44. >>> t
  45. tensor([0, 1, 2, 3])
  46. >>> torch.fft.fft(t)
  47. tensor([ 6.+0.j, -2.+2.j, -2.+0.j, -2.-2.j])
  48. >>> t = torch.tensor([0.+1.j, 2.+3.j, 4.+5.j, 6.+7.j])
  49. >>> torch.fft.fft(t)
  50. tensor([12.+16.j, -8.+0.j, -4.-4.j, 0.-8.j])
  51. """.format(**common_args))
  52. ifft = _add_docstr(_fft.fft_ifft, r"""
  53. ifft(input, n=None, dim=-1, norm=None, *, out=None) -> Tensor
  54. Computes the one dimensional inverse discrete Fourier transform of :attr:`input`.
  55. Note:
  56. Supports torch.half and torch.chalf on CUDA with GPU Architecture SM53 or greater.
  57. However it only supports powers of 2 signal length in every transformed dimension.
  58. Args:
  59. input (Tensor): the input tensor
  60. n (int, optional): Signal length. If given, the input will either be zero-padded
  61. or trimmed to this length before computing the IFFT.
  62. dim (int, optional): The dimension along which to take the one dimensional IFFT.
  63. norm (str, optional): Normalization mode. For the backward transform
  64. (:func:`~torch.fft.ifft`), these correspond to:
  65. * ``"forward"`` - no normalization
  66. * ``"backward"`` - normalize by ``1/n``
  67. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the IFFT orthonormal)
  68. Calling the forward transform (:func:`~torch.fft.fft`) with the same
  69. normalization mode will apply an overall normalization of ``1/n`` between
  70. the two transforms. This is required to make :func:`~torch.fft.ifft`
  71. the exact inverse.
  72. Default is ``"backward"`` (normalize by ``1/n``).
  73. Keyword args:
  74. {out}
  75. Example:
  76. >>> t = torch.tensor([ 6.+0.j, -2.+2.j, -2.+0.j, -2.-2.j])
  77. >>> torch.fft.ifft(t)
  78. tensor([0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j])
  79. """.format(**common_args))
  80. fft2 = _add_docstr(_fft.fft_fft2, r"""
  81. fft2(input, s=None, dim=(-2, -1), norm=None, *, out=None) -> Tensor
  82. Computes the 2 dimensional discrete Fourier transform of :attr:`input`.
  83. Equivalent to :func:`~torch.fft.fftn` but FFTs only the last two dimensions by default.
  84. Note:
  85. The Fourier domain representation of any real signal satisfies the
  86. Hermitian property: ``X[i, j] = conj(X[-i, -j])``. This
  87. function always returns all positive and negative frequency terms even
  88. though, for real inputs, half of these values are redundant.
  89. :func:`~torch.fft.rfft2` returns the more compact one-sided representation
  90. where only the positive frequencies of the last dimension are returned.
  91. Note:
  92. Supports torch.half and torch.chalf on CUDA with GPU Architecture SM53 or greater.
  93. However it only supports powers of 2 signal length in every transformed dimensions.
  94. Args:
  95. input (Tensor): the input tensor
  96. s (Tuple[int], optional): Signal size in the transformed dimensions.
  97. If given, each dimension ``dim[i]`` will either be zero-padded or
  98. trimmed to the length ``s[i]`` before computing the FFT.
  99. If a length ``-1`` is specified, no padding is done in that dimension.
  100. Default: ``s = [input.size(d) for d in dim]``
  101. dim (Tuple[int], optional): Dimensions to be transformed.
  102. Default: last two dimensions.
  103. norm (str, optional): Normalization mode. For the forward transform
  104. (:func:`~torch.fft.fft2`), these correspond to:
  105. * ``"forward"`` - normalize by ``1/n``
  106. * ``"backward"`` - no normalization
  107. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the FFT orthonormal)
  108. Where ``n = prod(s)`` is the logical FFT size.
  109. Calling the backward transform (:func:`~torch.fft.ifft2`) with the same
  110. normalization mode will apply an overall normalization of ``1/n``
  111. between the two transforms. This is required to make
  112. :func:`~torch.fft.ifft2` the exact inverse.
  113. Default is ``"backward"`` (no normalization).
  114. Keyword args:
  115. {out}
  116. Example:
  117. >>> x = torch.rand(10, 10, dtype=torch.complex64)
  118. >>> fft2 = torch.fft.fft2(x)
  119. The discrete Fourier transform is separable, so :func:`~torch.fft.fft2`
  120. here is equivalent to two one-dimensional :func:`~torch.fft.fft` calls:
  121. >>> two_ffts = torch.fft.fft(torch.fft.fft(x, dim=0), dim=1)
  122. >>> torch.testing.assert_close(fft2, two_ffts, check_stride=False)
  123. """.format(**common_args))
  124. ifft2 = _add_docstr(_fft.fft_ifft2, r"""
  125. ifft2(input, s=None, dim=(-2, -1), norm=None, *, out=None) -> Tensor
  126. Computes the 2 dimensional inverse discrete Fourier transform of :attr:`input`.
  127. Equivalent to :func:`~torch.fft.ifftn` but IFFTs only the last two dimensions by default.
  128. Note:
  129. Supports torch.half and torch.chalf on CUDA with GPU Architecture SM53 or greater.
  130. However it only supports powers of 2 signal length in every transformed dimensions.
  131. Args:
  132. input (Tensor): the input tensor
  133. s (Tuple[int], optional): Signal size in the transformed dimensions.
  134. If given, each dimension ``dim[i]`` will either be zero-padded or
  135. trimmed to the length ``s[i]`` before computing the IFFT.
  136. If a length ``-1`` is specified, no padding is done in that dimension.
  137. Default: ``s = [input.size(d) for d in dim]``
  138. dim (Tuple[int], optional): Dimensions to be transformed.
  139. Default: last two dimensions.
  140. norm (str, optional): Normalization mode. For the backward transform
  141. (:func:`~torch.fft.ifft2`), these correspond to:
  142. * ``"forward"`` - no normalization
  143. * ``"backward"`` - normalize by ``1/n``
  144. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the IFFT orthonormal)
  145. Where ``n = prod(s)`` is the logical IFFT size.
  146. Calling the forward transform (:func:`~torch.fft.fft2`) with the same
  147. normalization mode will apply an overall normalization of ``1/n`` between
  148. the two transforms. This is required to make :func:`~torch.fft.ifft2`
  149. the exact inverse.
  150. Default is ``"backward"`` (normalize by ``1/n``).
  151. Keyword args:
  152. {out}
  153. Example:
  154. >>> x = torch.rand(10, 10, dtype=torch.complex64)
  155. >>> ifft2 = torch.fft.ifft2(x)
  156. The discrete Fourier transform is separable, so :func:`~torch.fft.ifft2`
  157. here is equivalent to two one-dimensional :func:`~torch.fft.ifft` calls:
  158. >>> two_iffts = torch.fft.ifft(torch.fft.ifft(x, dim=0), dim=1)
  159. >>> torch.testing.assert_close(ifft2, two_iffts, check_stride=False)
  160. """.format(**common_args))
  161. fftn = _add_docstr(_fft.fft_fftn, r"""
  162. fftn(input, s=None, dim=None, norm=None, *, out=None) -> Tensor
  163. Computes the N dimensional discrete Fourier transform of :attr:`input`.
  164. Note:
  165. The Fourier domain representation of any real signal satisfies the
  166. Hermitian property: ``X[i_1, ..., i_n] = conj(X[-i_1, ..., -i_n])``. This
  167. function always returns all positive and negative frequency terms even
  168. though, for real inputs, half of these values are redundant.
  169. :func:`~torch.fft.rfftn` returns the more compact one-sided representation
  170. where only the positive frequencies of the last dimension are returned.
  171. Note:
  172. Supports torch.half and torch.chalf on CUDA with GPU Architecture SM53 or greater.
  173. However it only supports powers of 2 signal length in every transformed dimensions.
  174. Args:
  175. input (Tensor): the input tensor
  176. s (Tuple[int], optional): Signal size in the transformed dimensions.
  177. If given, each dimension ``dim[i]`` will either be zero-padded or
  178. trimmed to the length ``s[i]`` before computing the FFT.
  179. If a length ``-1`` is specified, no padding is done in that dimension.
  180. Default: ``s = [input.size(d) for d in dim]``
  181. dim (Tuple[int], optional): Dimensions to be transformed.
  182. Default: all dimensions, or the last ``len(s)`` dimensions if :attr:`s` is given.
  183. norm (str, optional): Normalization mode. For the forward transform
  184. (:func:`~torch.fft.fftn`), these correspond to:
  185. * ``"forward"`` - normalize by ``1/n``
  186. * ``"backward"`` - no normalization
  187. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the FFT orthonormal)
  188. Where ``n = prod(s)`` is the logical FFT size.
  189. Calling the backward transform (:func:`~torch.fft.ifftn`) with the same
  190. normalization mode will apply an overall normalization of ``1/n``
  191. between the two transforms. This is required to make
  192. :func:`~torch.fft.ifftn` the exact inverse.
  193. Default is ``"backward"`` (no normalization).
  194. Keyword args:
  195. {out}
  196. Example:
  197. >>> x = torch.rand(10, 10, dtype=torch.complex64)
  198. >>> fftn = torch.fft.fftn(x)
  199. The discrete Fourier transform is separable, so :func:`~torch.fft.fftn`
  200. here is equivalent to two one-dimensional :func:`~torch.fft.fft` calls:
  201. >>> two_ffts = torch.fft.fft(torch.fft.fft(x, dim=0), dim=1)
  202. >>> torch.testing.assert_close(fftn, two_ffts, check_stride=False)
  203. """.format(**common_args))
  204. ifftn = _add_docstr(_fft.fft_ifftn, r"""
  205. ifftn(input, s=None, dim=None, norm=None, *, out=None) -> Tensor
  206. Computes the N dimensional inverse discrete Fourier transform of :attr:`input`.
  207. Note:
  208. Supports torch.half and torch.chalf on CUDA with GPU Architecture SM53 or greater.
  209. However it only supports powers of 2 signal length in every transformed dimensions.
  210. Args:
  211. input (Tensor): the input tensor
  212. s (Tuple[int], optional): Signal size in the transformed dimensions.
  213. If given, each dimension ``dim[i]`` will either be zero-padded or
  214. trimmed to the length ``s[i]`` before computing the IFFT.
  215. If a length ``-1`` is specified, no padding is done in that dimension.
  216. Default: ``s = [input.size(d) for d in dim]``
  217. dim (Tuple[int], optional): Dimensions to be transformed.
  218. Default: all dimensions, or the last ``len(s)`` dimensions if :attr:`s` is given.
  219. norm (str, optional): Normalization mode. For the backward transform
  220. (:func:`~torch.fft.ifftn`), these correspond to:
  221. * ``"forward"`` - no normalization
  222. * ``"backward"`` - normalize by ``1/n``
  223. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the IFFT orthonormal)
  224. Where ``n = prod(s)`` is the logical IFFT size.
  225. Calling the forward transform (:func:`~torch.fft.fftn`) with the same
  226. normalization mode will apply an overall normalization of ``1/n`` between
  227. the two transforms. This is required to make :func:`~torch.fft.ifftn`
  228. the exact inverse.
  229. Default is ``"backward"`` (normalize by ``1/n``).
  230. Keyword args:
  231. {out}
  232. Example:
  233. >>> x = torch.rand(10, 10, dtype=torch.complex64)
  234. >>> ifftn = torch.fft.ifftn(x)
  235. The discrete Fourier transform is separable, so :func:`~torch.fft.ifftn`
  236. here is equivalent to two one-dimensional :func:`~torch.fft.ifft` calls:
  237. >>> two_iffts = torch.fft.ifft(torch.fft.ifft(x, dim=0), dim=1)
  238. >>> torch.testing.assert_close(ifftn, two_iffts, check_stride=False)
  239. """.format(**common_args))
  240. rfft = _add_docstr(_fft.fft_rfft, r"""
  241. rfft(input, n=None, dim=-1, norm=None, *, out=None) -> Tensor
  242. Computes the one dimensional Fourier transform of real-valued :attr:`input`.
  243. The FFT of a real signal is Hermitian-symmetric, ``X[i] = conj(X[-i])`` so
  244. the output contains only the positive frequencies below the Nyquist frequency.
  245. To compute the full output, use :func:`~torch.fft.fft`
  246. Note:
  247. Supports torch.half on CUDA with GPU Architecture SM53 or greater.
  248. However it only supports powers of 2 signal length in every transformed dimension.
  249. Args:
  250. input (Tensor): the real input tensor
  251. n (int, optional): Signal length. If given, the input will either be zero-padded
  252. or trimmed to this length before computing the real FFT.
  253. dim (int, optional): The dimension along which to take the one dimensional real FFT.
  254. norm (str, optional): Normalization mode. For the forward transform
  255. (:func:`~torch.fft.rfft`), these correspond to:
  256. * ``"forward"`` - normalize by ``1/n``
  257. * ``"backward"`` - no normalization
  258. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the FFT orthonormal)
  259. Calling the backward transform (:func:`~torch.fft.irfft`) with the same
  260. normalization mode will apply an overall normalization of ``1/n`` between
  261. the two transforms. This is required to make :func:`~torch.fft.irfft`
  262. the exact inverse.
  263. Default is ``"backward"`` (no normalization).
  264. Keyword args:
  265. {out}
  266. Example:
  267. >>> t = torch.arange(4)
  268. >>> t
  269. tensor([0, 1, 2, 3])
  270. >>> torch.fft.rfft(t)
  271. tensor([ 6.+0.j, -2.+2.j, -2.+0.j])
  272. Compare against the full output from :func:`~torch.fft.fft`:
  273. >>> torch.fft.fft(t)
  274. tensor([ 6.+0.j, -2.+2.j, -2.+0.j, -2.-2.j])
  275. Notice that the symmetric element ``T[-1] == T[1].conj()`` is omitted.
  276. At the Nyquist frequency ``T[-2] == T[2]`` is it's own symmetric pair,
  277. and therefore must always be real-valued.
  278. """.format(**common_args))
  279. irfft = _add_docstr(_fft.fft_irfft, r"""
  280. irfft(input, n=None, dim=-1, norm=None, *, out=None) -> Tensor
  281. Computes the inverse of :func:`~torch.fft.rfft`.
  282. :attr:`input` is interpreted as a one-sided Hermitian signal in the Fourier
  283. domain, as produced by :func:`~torch.fft.rfft`. By the Hermitian property, the
  284. output will be real-valued.
  285. Note:
  286. Some input frequencies must be real-valued to satisfy the Hermitian
  287. property. In these cases the imaginary component will be ignored.
  288. For example, any imaginary component in the zero-frequency term cannot
  289. be represented in a real output and so will always be ignored.
  290. Note:
  291. The correct interpretation of the Hermitian input depends on the length of
  292. the original data, as given by :attr:`n`. This is because each input shape
  293. could correspond to either an odd or even length signal. By default, the
  294. signal is assumed to be even length and odd signals will not round-trip
  295. properly. So, it is recommended to always pass the signal length :attr:`n`.
  296. Note:
  297. Supports torch.half and torch.chalf on CUDA with GPU Architecture SM53 or greater.
  298. However it only supports powers of 2 signal length in every transformed dimension.
  299. With default arguments, size of the transformed dimension should be (2^n + 1) as argument
  300. `n` defaults to even output size = 2 * (transformed_dim_size - 1)
  301. Args:
  302. input (Tensor): the input tensor representing a half-Hermitian signal
  303. n (int, optional): Output signal length. This determines the length of the
  304. output signal. If given, the input will either be zero-padded or trimmed to this
  305. length before computing the real IFFT.
  306. Defaults to even output: ``n=2*(input.size(dim) - 1)``.
  307. dim (int, optional): The dimension along which to take the one dimensional real IFFT.
  308. norm (str, optional): Normalization mode. For the backward transform
  309. (:func:`~torch.fft.irfft`), these correspond to:
  310. * ``"forward"`` - no normalization
  311. * ``"backward"`` - normalize by ``1/n``
  312. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the real IFFT orthonormal)
  313. Calling the forward transform (:func:`~torch.fft.rfft`) with the same
  314. normalization mode will apply an overall normalization of ``1/n`` between
  315. the two transforms. This is required to make :func:`~torch.fft.irfft`
  316. the exact inverse.
  317. Default is ``"backward"`` (normalize by ``1/n``).
  318. Keyword args:
  319. {out}
  320. Example:
  321. >>> t = torch.linspace(0, 1, 5)
  322. >>> t
  323. tensor([0.0000, 0.2500, 0.5000, 0.7500, 1.0000])
  324. >>> T = torch.fft.rfft(t)
  325. >>> T
  326. tensor([ 2.5000+0.0000j, -0.6250+0.8602j, -0.6250+0.2031j])
  327. Without specifying the output length to :func:`~torch.fft.irfft`, the output
  328. will not round-trip properly because the input is odd-length:
  329. >>> torch.fft.irfft(T)
  330. tensor([0.1562, 0.3511, 0.7812, 1.2114])
  331. So, it is recommended to always pass the signal length :attr:`n`:
  332. >>> roundtrip = torch.fft.irfft(T, t.numel())
  333. >>> torch.testing.assert_close(roundtrip, t, check_stride=False)
  334. """.format(**common_args))
  335. rfft2 = _add_docstr(_fft.fft_rfft2, r"""
  336. rfft2(input, s=None, dim=(-2, -1), norm=None, *, out=None) -> Tensor
  337. Computes the 2-dimensional discrete Fourier transform of real :attr:`input`.
  338. Equivalent to :func:`~torch.fft.rfftn` but FFTs only the last two dimensions by default.
  339. The FFT of a real signal is Hermitian-symmetric, ``X[i, j] = conj(X[-i, -j])``,
  340. so the full :func:`~torch.fft.fft2` output contains redundant information.
  341. :func:`~torch.fft.rfft2` instead omits the negative frequencies in the last
  342. dimension.
  343. Note:
  344. Supports torch.half on CUDA with GPU Architecture SM53 or greater.
  345. However it only supports powers of 2 signal length in every transformed dimensions.
  346. Args:
  347. input (Tensor): the input tensor
  348. s (Tuple[int], optional): Signal size in the transformed dimensions.
  349. If given, each dimension ``dim[i]`` will either be zero-padded or
  350. trimmed to the length ``s[i]`` before computing the real FFT.
  351. If a length ``-1`` is specified, no padding is done in that dimension.
  352. Default: ``s = [input.size(d) for d in dim]``
  353. dim (Tuple[int], optional): Dimensions to be transformed.
  354. Default: last two dimensions.
  355. norm (str, optional): Normalization mode. For the forward transform
  356. (:func:`~torch.fft.rfft2`), these correspond to:
  357. * ``"forward"`` - normalize by ``1/n``
  358. * ``"backward"`` - no normalization
  359. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the real FFT orthonormal)
  360. Where ``n = prod(s)`` is the logical FFT size.
  361. Calling the backward transform (:func:`~torch.fft.irfft2`) with the same
  362. normalization mode will apply an overall normalization of ``1/n`` between
  363. the two transforms. This is required to make :func:`~torch.fft.irfft2`
  364. the exact inverse.
  365. Default is ``"backward"`` (no normalization).
  366. Keyword args:
  367. {out}
  368. Example:
  369. >>> t = torch.rand(10, 10)
  370. >>> rfft2 = torch.fft.rfft2(t)
  371. >>> rfft2.size()
  372. torch.Size([10, 6])
  373. Compared against the full output from :func:`~torch.fft.fft2`, we have all
  374. elements up to the Nyquist frequency.
  375. >>> fft2 = torch.fft.fft2(t)
  376. >>> torch.testing.assert_close(fft2[..., :6], rfft2, check_stride=False)
  377. The discrete Fourier transform is separable, so :func:`~torch.fft.rfft2`
  378. here is equivalent to a combination of :func:`~torch.fft.fft` and
  379. :func:`~torch.fft.rfft`:
  380. >>> two_ffts = torch.fft.fft(torch.fft.rfft(t, dim=1), dim=0)
  381. >>> torch.testing.assert_close(rfft2, two_ffts, check_stride=False)
  382. """.format(**common_args))
  383. irfft2 = _add_docstr(_fft.fft_irfft2, r"""
  384. irfft2(input, s=None, dim=(-2, -1), norm=None, *, out=None) -> Tensor
  385. Computes the inverse of :func:`~torch.fft.rfft2`.
  386. Equivalent to :func:`~torch.fft.irfftn` but IFFTs only the last two dimensions by default.
  387. :attr:`input` is interpreted as a one-sided Hermitian signal in the Fourier
  388. domain, as produced by :func:`~torch.fft.rfft2`. By the Hermitian property, the
  389. output will be real-valued.
  390. Note:
  391. Some input frequencies must be real-valued to satisfy the Hermitian
  392. property. In these cases the imaginary component will be ignored.
  393. For example, any imaginary component in the zero-frequency term cannot
  394. be represented in a real output and so will always be ignored.
  395. Note:
  396. The correct interpretation of the Hermitian input depends on the length of
  397. the original data, as given by :attr:`s`. This is because each input shape
  398. could correspond to either an odd or even length signal. By default, the
  399. signal is assumed to be even length and odd signals will not round-trip
  400. properly. So, it is recommended to always pass the signal shape :attr:`s`.
  401. Note:
  402. Supports torch.half and torch.chalf on CUDA with GPU Architecture SM53 or greater.
  403. However it only supports powers of 2 signal length in every transformed dimensions.
  404. With default arguments, the size of last dimension should be (2^n + 1) as argument
  405. `s` defaults to even output size = 2 * (last_dim_size - 1)
  406. Args:
  407. input (Tensor): the input tensor
  408. s (Tuple[int], optional): Signal size in the transformed dimensions.
  409. If given, each dimension ``dim[i]`` will either be zero-padded or
  410. trimmed to the length ``s[i]`` before computing the real FFT.
  411. If a length ``-1`` is specified, no padding is done in that dimension.
  412. Defaults to even output in the last dimension:
  413. ``s[-1] = 2*(input.size(dim[-1]) - 1)``.
  414. dim (Tuple[int], optional): Dimensions to be transformed.
  415. The last dimension must be the half-Hermitian compressed dimension.
  416. Default: last two dimensions.
  417. norm (str, optional): Normalization mode. For the backward transform
  418. (:func:`~torch.fft.irfft2`), these correspond to:
  419. * ``"forward"`` - no normalization
  420. * ``"backward"`` - normalize by ``1/n``
  421. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the real IFFT orthonormal)
  422. Where ``n = prod(s)`` is the logical IFFT size.
  423. Calling the forward transform (:func:`~torch.fft.rfft2`) with the same
  424. normalization mode will apply an overall normalization of ``1/n`` between
  425. the two transforms. This is required to make :func:`~torch.fft.irfft2`
  426. the exact inverse.
  427. Default is ``"backward"`` (normalize by ``1/n``).
  428. Keyword args:
  429. {out}
  430. Example:
  431. >>> t = torch.rand(10, 9)
  432. >>> T = torch.fft.rfft2(t)
  433. Without specifying the output length to :func:`~torch.fft.irfft2`, the output
  434. will not round-trip properly because the input is odd-length in the last
  435. dimension:
  436. >>> torch.fft.irfft2(T).size()
  437. torch.Size([10, 8])
  438. So, it is recommended to always pass the signal shape :attr:`s`.
  439. >>> roundtrip = torch.fft.irfft2(T, t.size())
  440. >>> roundtrip.size()
  441. torch.Size([10, 9])
  442. >>> torch.testing.assert_close(roundtrip, t, check_stride=False)
  443. """.format(**common_args))
  444. rfftn = _add_docstr(_fft.fft_rfftn, r"""
  445. rfftn(input, s=None, dim=None, norm=None, *, out=None) -> Tensor
  446. Computes the N-dimensional discrete Fourier transform of real :attr:`input`.
  447. The FFT of a real signal is Hermitian-symmetric,
  448. ``X[i_1, ..., i_n] = conj(X[-i_1, ..., -i_n])`` so the full
  449. :func:`~torch.fft.fftn` output contains redundant information.
  450. :func:`~torch.fft.rfftn` instead omits the negative frequencies in the
  451. last dimension.
  452. Note:
  453. Supports torch.half on CUDA with GPU Architecture SM53 or greater.
  454. However it only supports powers of 2 signal length in every transformed dimensions.
  455. Args:
  456. input (Tensor): the input tensor
  457. s (Tuple[int], optional): Signal size in the transformed dimensions.
  458. If given, each dimension ``dim[i]`` will either be zero-padded or
  459. trimmed to the length ``s[i]`` before computing the real FFT.
  460. If a length ``-1`` is specified, no padding is done in that dimension.
  461. Default: ``s = [input.size(d) for d in dim]``
  462. dim (Tuple[int], optional): Dimensions to be transformed.
  463. Default: all dimensions, or the last ``len(s)`` dimensions if :attr:`s` is given.
  464. norm (str, optional): Normalization mode. For the forward transform
  465. (:func:`~torch.fft.rfftn`), these correspond to:
  466. * ``"forward"`` - normalize by ``1/n``
  467. * ``"backward"`` - no normalization
  468. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the real FFT orthonormal)
  469. Where ``n = prod(s)`` is the logical FFT size.
  470. Calling the backward transform (:func:`~torch.fft.irfftn`) with the same
  471. normalization mode will apply an overall normalization of ``1/n`` between
  472. the two transforms. This is required to make :func:`~torch.fft.irfftn`
  473. the exact inverse.
  474. Default is ``"backward"`` (no normalization).
  475. Keyword args:
  476. {out}
  477. Example:
  478. >>> t = torch.rand(10, 10)
  479. >>> rfftn = torch.fft.rfftn(t)
  480. >>> rfftn.size()
  481. torch.Size([10, 6])
  482. Compared against the full output from :func:`~torch.fft.fftn`, we have all
  483. elements up to the Nyquist frequency.
  484. >>> fftn = torch.fft.fftn(t)
  485. >>> torch.testing.assert_close(fftn[..., :6], rfftn, check_stride=False)
  486. The discrete Fourier transform is separable, so :func:`~torch.fft.rfftn`
  487. here is equivalent to a combination of :func:`~torch.fft.fft` and
  488. :func:`~torch.fft.rfft`:
  489. >>> two_ffts = torch.fft.fft(torch.fft.rfft(t, dim=1), dim=0)
  490. >>> torch.testing.assert_close(rfftn, two_ffts, check_stride=False)
  491. """.format(**common_args))
  492. irfftn = _add_docstr(_fft.fft_irfftn, r"""
  493. irfftn(input, s=None, dim=None, norm=None, *, out=None) -> Tensor
  494. Computes the inverse of :func:`~torch.fft.rfftn`.
  495. :attr:`input` is interpreted as a one-sided Hermitian signal in the Fourier
  496. domain, as produced by :func:`~torch.fft.rfftn`. By the Hermitian property, the
  497. output will be real-valued.
  498. Note:
  499. Some input frequencies must be real-valued to satisfy the Hermitian
  500. property. In these cases the imaginary component will be ignored.
  501. For example, any imaginary component in the zero-frequency term cannot
  502. be represented in a real output and so will always be ignored.
  503. Note:
  504. The correct interpretation of the Hermitian input depends on the length of
  505. the original data, as given by :attr:`s`. This is because each input shape
  506. could correspond to either an odd or even length signal. By default, the
  507. signal is assumed to be even length and odd signals will not round-trip
  508. properly. So, it is recommended to always pass the signal shape :attr:`s`.
  509. Note:
  510. Supports torch.half and torch.chalf on CUDA with GPU Architecture SM53 or greater.
  511. However it only supports powers of 2 signal length in every transformed dimensions.
  512. With default arguments, the size of last dimension should be (2^n + 1) as argument
  513. `s` defaults to even output size = 2 * (last_dim_size - 1)
  514. Args:
  515. input (Tensor): the input tensor
  516. s (Tuple[int], optional): Signal size in the transformed dimensions.
  517. If given, each dimension ``dim[i]`` will either be zero-padded or
  518. trimmed to the length ``s[i]`` before computing the real FFT.
  519. If a length ``-1`` is specified, no padding is done in that dimension.
  520. Defaults to even output in the last dimension:
  521. ``s[-1] = 2*(input.size(dim[-1]) - 1)``.
  522. dim (Tuple[int], optional): Dimensions to be transformed.
  523. The last dimension must be the half-Hermitian compressed dimension.
  524. Default: all dimensions, or the last ``len(s)`` dimensions if :attr:`s` is given.
  525. norm (str, optional): Normalization mode. For the backward transform
  526. (:func:`~torch.fft.irfftn`), these correspond to:
  527. * ``"forward"`` - no normalization
  528. * ``"backward"`` - normalize by ``1/n``
  529. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the real IFFT orthonormal)
  530. Where ``n = prod(s)`` is the logical IFFT size.
  531. Calling the forward transform (:func:`~torch.fft.rfftn`) with the same
  532. normalization mode will apply an overall normalization of ``1/n`` between
  533. the two transforms. This is required to make :func:`~torch.fft.irfftn`
  534. the exact inverse.
  535. Default is ``"backward"`` (normalize by ``1/n``).
  536. Keyword args:
  537. {out}
  538. Example:
  539. >>> t = torch.rand(10, 9)
  540. >>> T = torch.fft.rfftn(t)
  541. Without specifying the output length to :func:`~torch.fft.irfft`, the output
  542. will not round-trip properly because the input is odd-length in the last
  543. dimension:
  544. >>> torch.fft.irfftn(T).size()
  545. torch.Size([10, 8])
  546. So, it is recommended to always pass the signal shape :attr:`s`.
  547. >>> roundtrip = torch.fft.irfftn(T, t.size())
  548. >>> roundtrip.size()
  549. torch.Size([10, 9])
  550. >>> torch.testing.assert_close(roundtrip, t, check_stride=False)
  551. """.format(**common_args))
  552. hfft = _add_docstr(_fft.fft_hfft, r"""
  553. hfft(input, n=None, dim=-1, norm=None, *, out=None) -> Tensor
  554. Computes the one dimensional discrete Fourier transform of a Hermitian
  555. symmetric :attr:`input` signal.
  556. Note:
  557. :func:`~torch.fft.hfft`/:func:`~torch.fft.ihfft` are analogous to
  558. :func:`~torch.fft.rfft`/:func:`~torch.fft.irfft`. The real FFT expects
  559. a real signal in the time-domain and gives a Hermitian symmetry in the
  560. frequency-domain. The Hermitian FFT is the opposite; Hermitian symmetric in
  561. the time-domain and real-valued in the frequency-domain. For this reason,
  562. special care needs to be taken with the length argument :attr:`n`, in the
  563. same way as with :func:`~torch.fft.irfft`.
  564. Note:
  565. Because the signal is Hermitian in the time-domain, the result will be
  566. real in the frequency domain. Note that some input frequencies must be
  567. real-valued to satisfy the Hermitian property. In these cases the imaginary
  568. component will be ignored. For example, any imaginary component in
  569. ``input[0]`` would result in one or more complex frequency terms which
  570. cannot be represented in a real output and so will always be ignored.
  571. Note:
  572. The correct interpretation of the Hermitian input depends on the length of
  573. the original data, as given by :attr:`n`. This is because each input shape
  574. could correspond to either an odd or even length signal. By default, the
  575. signal is assumed to be even length and odd signals will not round-trip
  576. properly. So, it is recommended to always pass the signal length :attr:`n`.
  577. Note:
  578. Supports torch.half and torch.chalf on CUDA with GPU Architecture SM53 or greater.
  579. However it only supports powers of 2 signal length in every transformed dimension.
  580. With default arguments, size of the transformed dimension should be (2^n + 1) as argument
  581. `n` defaults to even output size = 2 * (transformed_dim_size - 1)
  582. Args:
  583. input (Tensor): the input tensor representing a half-Hermitian signal
  584. n (int, optional): Output signal length. This determines the length of the
  585. real output. If given, the input will either be zero-padded or trimmed to this
  586. length before computing the Hermitian FFT.
  587. Defaults to even output: ``n=2*(input.size(dim) - 1)``.
  588. dim (int, optional): The dimension along which to take the one dimensional Hermitian FFT.
  589. norm (str, optional): Normalization mode. For the forward transform
  590. (:func:`~torch.fft.hfft`), these correspond to:
  591. * ``"forward"`` - normalize by ``1/n``
  592. * ``"backward"`` - no normalization
  593. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the Hermitian FFT orthonormal)
  594. Calling the backward transform (:func:`~torch.fft.ihfft`) with the same
  595. normalization mode will apply an overall normalization of ``1/n`` between
  596. the two transforms. This is required to make :func:`~torch.fft.ihfft`
  597. the exact inverse.
  598. Default is ``"backward"`` (no normalization).
  599. Keyword args:
  600. {out}
  601. Example:
  602. Taking a real-valued frequency signal and bringing it into the time domain
  603. gives Hermitian symmetric output:
  604. >>> t = torch.linspace(0, 1, 5)
  605. >>> t
  606. tensor([0.0000, 0.2500, 0.5000, 0.7500, 1.0000])
  607. >>> T = torch.fft.ifft(t)
  608. >>> T
  609. tensor([ 0.5000-0.0000j, -0.1250-0.1720j, -0.1250-0.0406j, -0.1250+0.0406j,
  610. -0.1250+0.1720j])
  611. Note that ``T[1] == T[-1].conj()`` and ``T[2] == T[-2].conj()`` is
  612. redundant. We can thus compute the forward transform without considering
  613. negative frequencies:
  614. >>> torch.fft.hfft(T[:3], n=5)
  615. tensor([0.0000, 0.2500, 0.5000, 0.7500, 1.0000])
  616. Like with :func:`~torch.fft.irfft`, the output length must be given in order
  617. to recover an even length output:
  618. >>> torch.fft.hfft(T[:3])
  619. tensor([0.1250, 0.2809, 0.6250, 0.9691])
  620. """.format(**common_args))
  621. ihfft = _add_docstr(_fft.fft_ihfft, r"""
  622. ihfft(input, n=None, dim=-1, norm=None, *, out=None) -> Tensor
  623. Computes the inverse of :func:`~torch.fft.hfft`.
  624. :attr:`input` must be a real-valued signal, interpreted in the Fourier domain.
  625. The IFFT of a real signal is Hermitian-symmetric, ``X[i] = conj(X[-i])``.
  626. :func:`~torch.fft.ihfft` represents this in the one-sided form where only the
  627. positive frequencies below the Nyquist frequency are included. To compute the
  628. full output, use :func:`~torch.fft.ifft`.
  629. Note:
  630. Supports torch.half on CUDA with GPU Architecture SM53 or greater.
  631. However it only supports powers of 2 signal length in every transformed dimension.
  632. Args:
  633. input (Tensor): the real input tensor
  634. n (int, optional): Signal length. If given, the input will either be zero-padded
  635. or trimmed to this length before computing the Hermitian IFFT.
  636. dim (int, optional): The dimension along which to take the one dimensional Hermitian IFFT.
  637. norm (str, optional): Normalization mode. For the backward transform
  638. (:func:`~torch.fft.ihfft`), these correspond to:
  639. * ``"forward"`` - no normalization
  640. * ``"backward"`` - normalize by ``1/n``
  641. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the IFFT orthonormal)
  642. Calling the forward transform (:func:`~torch.fft.hfft`) with the same
  643. normalization mode will apply an overall normalization of ``1/n`` between
  644. the two transforms. This is required to make :func:`~torch.fft.ihfft`
  645. the exact inverse.
  646. Default is ``"backward"`` (normalize by ``1/n``).
  647. Keyword args:
  648. {out}
  649. Example:
  650. >>> t = torch.arange(5)
  651. >>> t
  652. tensor([0, 1, 2, 3, 4])
  653. >>> torch.fft.ihfft(t)
  654. tensor([ 2.0000-0.0000j, -0.5000-0.6882j, -0.5000-0.1625j])
  655. Compare against the full output from :func:`~torch.fft.ifft`:
  656. >>> torch.fft.ifft(t)
  657. tensor([ 2.0000-0.0000j, -0.5000-0.6882j, -0.5000-0.1625j, -0.5000+0.1625j,
  658. -0.5000+0.6882j])
  659. """.format(**common_args))
  660. hfft2 = _add_docstr(_fft.fft_hfft2, r"""
  661. hfft2(input, s=None, dim=(-2, -1), norm=None, *, out=None) -> Tensor
  662. Computes the 2-dimensional discrete Fourier transform of a Hermitian symmetric
  663. :attr:`input` signal. Equivalent to :func:`~torch.fft.hfftn` but only
  664. transforms the last two dimensions by default.
  665. :attr:`input` is interpreted as a one-sided Hermitian signal in the time
  666. domain. By the Hermitian property, the Fourier transform will be real-valued.
  667. Note:
  668. Supports torch.half and torch.chalf on CUDA with GPU Architecture SM53 or greater.
  669. However it only supports powers of 2 signal length in every transformed dimensions.
  670. With default arguments, the size of last dimension should be (2^n + 1) as argument
  671. `s` defaults to even output size = 2 * (last_dim_size - 1)
  672. Args:
  673. input (Tensor): the input tensor
  674. s (Tuple[int], optional): Signal size in the transformed dimensions.
  675. If given, each dimension ``dim[i]`` will either be zero-padded or
  676. trimmed to the length ``s[i]`` before computing the Hermitian FFT.
  677. If a length ``-1`` is specified, no padding is done in that dimension.
  678. Defaults to even output in the last dimension:
  679. ``s[-1] = 2*(input.size(dim[-1]) - 1)``.
  680. dim (Tuple[int], optional): Dimensions to be transformed.
  681. The last dimension must be the half-Hermitian compressed dimension.
  682. Default: last two dimensions.
  683. norm (str, optional): Normalization mode. For the forward transform
  684. (:func:`~torch.fft.hfft2`), these correspond to:
  685. * ``"forward"`` - normalize by ``1/n``
  686. * ``"backward"`` - no normalization
  687. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the Hermitian FFT orthonormal)
  688. Where ``n = prod(s)`` is the logical FFT size.
  689. Calling the backward transform (:func:`~torch.fft.ihfft2`) with the same
  690. normalization mode will apply an overall normalization of ``1/n`` between
  691. the two transforms. This is required to make :func:`~torch.fft.ihfft2`
  692. the exact inverse.
  693. Default is ``"backward"`` (no normalization).
  694. Keyword args:
  695. {out}
  696. Example:
  697. Starting from a real frequency-space signal, we can generate a
  698. Hermitian-symmetric time-domain signal:
  699. >>> T = torch.rand(10, 9)
  700. >>> t = torch.fft.ihfft2(T)
  701. Without specifying the output length to :func:`~torch.fft.hfftn`, the
  702. output will not round-trip properly because the input is odd-length in the
  703. last dimension:
  704. >>> torch.fft.hfft2(t).size()
  705. torch.Size([10, 10])
  706. So, it is recommended to always pass the signal shape :attr:`s`.
  707. >>> roundtrip = torch.fft.hfft2(t, T.size())
  708. >>> roundtrip.size()
  709. torch.Size([10, 9])
  710. >>> torch.allclose(roundtrip, T)
  711. True
  712. """.format(**common_args))
  713. ihfft2 = _add_docstr(_fft.fft_ihfft2, r"""
  714. ihfft2(input, s=None, dim=(-2, -1), norm=None, *, out=None) -> Tensor
  715. Computes the 2-dimensional inverse discrete Fourier transform of real
  716. :attr:`input`. Equivalent to :func:`~torch.fft.ihfftn` but transforms only the
  717. two last dimensions by default.
  718. Note:
  719. Supports torch.half on CUDA with GPU Architecture SM53 or greater.
  720. However it only supports powers of 2 signal length in every transformed dimensions.
  721. Args:
  722. input (Tensor): the input tensor
  723. s (Tuple[int], optional): Signal size in the transformed dimensions.
  724. If given, each dimension ``dim[i]`` will either be zero-padded or
  725. trimmed to the length ``s[i]`` before computing the Hermitian IFFT.
  726. If a length ``-1`` is specified, no padding is done in that dimension.
  727. Default: ``s = [input.size(d) for d in dim]``
  728. dim (Tuple[int], optional): Dimensions to be transformed.
  729. Default: last two dimensions.
  730. norm (str, optional): Normalization mode. For the backward transform
  731. (:func:`~torch.fft.ihfft2`), these correspond to:
  732. * ``"forward"`` - no normalization
  733. * ``"backward"`` - normalize by ``1/n``
  734. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the Hermitian IFFT orthonormal)
  735. Where ``n = prod(s)`` is the logical IFFT size.
  736. Calling the forward transform (:func:`~torch.fft.hfft2`) with the same
  737. normalization mode will apply an overall normalization of ``1/n`` between
  738. the two transforms. This is required to make :func:`~torch.fft.ihfft2`
  739. the exact inverse.
  740. Default is ``"backward"`` (normalize by ``1/n``).
  741. Keyword args:
  742. {out}
  743. Example:
  744. >>> T = torch.rand(10, 10)
  745. >>> t = torch.fft.ihfft2(t)
  746. >>> t.size()
  747. torch.Size([10, 6])
  748. Compared against the full output from :func:`~torch.fft.ifft2`, the
  749. Hermitian time-space signal takes up only half the space.
  750. >>> fftn = torch.fft.ifft2(t)
  751. >>> torch.allclose(fftn[..., :6], rfftn)
  752. True
  753. The discrete Fourier transform is separable, so :func:`~torch.fft.ihfft2`
  754. here is equivalent to a combination of :func:`~torch.fft.ifft` and
  755. :func:`~torch.fft.ihfft`:
  756. >>> two_ffts = torch.fft.ifft(torch.fft.ihfft(t, dim=1), dim=0)
  757. >>> torch.allclose(t, two_ffts)
  758. True
  759. """.format(**common_args))
  760. hfftn = _add_docstr(_fft.fft_hfftn, r"""
  761. hfftn(input, s=None, dim=None, norm=None, *, out=None) -> Tensor
  762. Computes the n-dimensional discrete Fourier transform of a Hermitian symmetric
  763. :attr:`input` signal.
  764. :attr:`input` is interpreted as a one-sided Hermitian signal in the time
  765. domain. By the Hermitian property, the Fourier transform will be real-valued.
  766. Note:
  767. :func:`~torch.fft.hfftn`/:func:`~torch.fft.ihfftn` are analogous to
  768. :func:`~torch.fft.rfftn`/:func:`~torch.fft.irfftn`. The real FFT expects
  769. a real signal in the time-domain and gives Hermitian symmetry in the
  770. frequency-domain. The Hermitian FFT is the opposite; Hermitian symmetric in
  771. the time-domain and real-valued in the frequency-domain. For this reason,
  772. special care needs to be taken with the shape argument :attr:`s`, in the
  773. same way as with :func:`~torch.fft.irfftn`.
  774. Note:
  775. Some input frequencies must be real-valued to satisfy the Hermitian
  776. property. In these cases the imaginary component will be ignored.
  777. For example, any imaginary component in the zero-frequency term cannot
  778. be represented in a real output and so will always be ignored.
  779. Note:
  780. The correct interpretation of the Hermitian input depends on the length of
  781. the original data, as given by :attr:`s`. This is because each input shape
  782. could correspond to either an odd or even length signal. By default, the
  783. signal is assumed to be even length and odd signals will not round-trip
  784. properly. It is recommended to always pass the signal shape :attr:`s`.
  785. Note:
  786. Supports torch.half and torch.chalf on CUDA with GPU Architecture SM53 or greater.
  787. However it only supports powers of 2 signal length in every transformed dimensions.
  788. With default arguments, the size of last dimension should be (2^n + 1) as argument
  789. `s` defaults to even output size = 2 * (last_dim_size - 1)
  790. Args:
  791. input (Tensor): the input tensor
  792. s (Tuple[int], optional): Signal size in the transformed dimensions.
  793. If given, each dimension ``dim[i]`` will either be zero-padded or
  794. trimmed to the length ``s[i]`` before computing the real FFT.
  795. If a length ``-1`` is specified, no padding is done in that dimension.
  796. Defaults to even output in the last dimension:
  797. ``s[-1] = 2*(input.size(dim[-1]) - 1)``.
  798. dim (Tuple[int], optional): Dimensions to be transformed.
  799. The last dimension must be the half-Hermitian compressed dimension.
  800. Default: all dimensions, or the last ``len(s)`` dimensions if :attr:`s` is given.
  801. norm (str, optional): Normalization mode. For the forward transform
  802. (:func:`~torch.fft.hfftn`), these correspond to:
  803. * ``"forward"`` - normalize by ``1/n``
  804. * ``"backward"`` - no normalization
  805. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the Hermitian FFT orthonormal)
  806. Where ``n = prod(s)`` is the logical FFT size.
  807. Calling the backward transform (:func:`~torch.fft.ihfftn`) with the same
  808. normalization mode will apply an overall normalization of ``1/n`` between
  809. the two transforms. This is required to make :func:`~torch.fft.ihfftn`
  810. the exact inverse.
  811. Default is ``"backward"`` (no normalization).
  812. Keyword args:
  813. {out}
  814. Example:
  815. Starting from a real frequency-space signal, we can generate a
  816. Hermitian-symmetric time-domain signal:
  817. >>> T = torch.rand(10, 9)
  818. >>> t = torch.fft.ihfftn(T)
  819. Without specifying the output length to :func:`~torch.fft.hfftn`, the
  820. output will not round-trip properly because the input is odd-length in the
  821. last dimension:
  822. >>> torch.fft.hfftn(t).size()
  823. torch.Size([10, 10])
  824. So, it is recommended to always pass the signal shape :attr:`s`.
  825. >>> roundtrip = torch.fft.hfftn(t, T.size())
  826. >>> roundtrip.size()
  827. torch.Size([10, 9])
  828. >>> torch.allclose(roundtrip, T)
  829. True
  830. """.format(**common_args))
  831. ihfftn = _add_docstr(_fft.fft_ihfftn, r"""
  832. ihfftn(input, s=None, dim=None, norm=None, *, out=None) -> Tensor
  833. Computes the N-dimensional inverse discrete Fourier transform of real :attr:`input`.
  834. :attr:`input` must be a real-valued signal, interpreted in the Fourier domain.
  835. The n-dimensional IFFT of a real signal is Hermitian-symmetric,
  836. ``X[i, j, ...] = conj(X[-i, -j, ...])``. :func:`~torch.fft.ihfftn` represents
  837. this in the one-sided form where only the positive frequencies below the
  838. Nyquist frequency are included in the last signal dimension. To compute the
  839. full output, use :func:`~torch.fft.ifftn`.
  840. Note:
  841. Supports torch.half on CUDA with GPU Architecture SM53 or greater.
  842. However it only supports powers of 2 signal length in every transformed dimensions.
  843. Args:
  844. input (Tensor): the input tensor
  845. s (Tuple[int], optional): Signal size in the transformed dimensions.
  846. If given, each dimension ``dim[i]`` will either be zero-padded or
  847. trimmed to the length ``s[i]`` before computing the Hermitian IFFT.
  848. If a length ``-1`` is specified, no padding is done in that dimension.
  849. Default: ``s = [input.size(d) for d in dim]``
  850. dim (Tuple[int], optional): Dimensions to be transformed.
  851. Default: all dimensions, or the last ``len(s)`` dimensions if :attr:`s` is given.
  852. norm (str, optional): Normalization mode. For the backward transform
  853. (:func:`~torch.fft.ihfftn`), these correspond to:
  854. * ``"forward"`` - no normalization
  855. * ``"backward"`` - normalize by ``1/n``
  856. * ``"ortho"`` - normalize by ``1/sqrt(n)`` (making the Hermitian IFFT orthonormal)
  857. Where ``n = prod(s)`` is the logical IFFT size.
  858. Calling the forward transform (:func:`~torch.fft.hfftn`) with the same
  859. normalization mode will apply an overall normalization of ``1/n`` between
  860. the two transforms. This is required to make :func:`~torch.fft.ihfftn`
  861. the exact inverse.
  862. Default is ``"backward"`` (normalize by ``1/n``).
  863. Keyword args:
  864. {out}
  865. Example:
  866. >>> T = torch.rand(10, 10)
  867. >>> ihfftn = torch.fft.ihfftn(T)
  868. >>> ihfftn.size()
  869. torch.Size([10, 6])
  870. Compared against the full output from :func:`~torch.fft.ifftn`, we have all
  871. elements up to the Nyquist frequency.
  872. >>> ifftn = torch.fft.ifftn(t)
  873. >>> torch.allclose(ifftn[..., :6], ihfftn)
  874. True
  875. The discrete Fourier transform is separable, so :func:`~torch.fft.ihfftn`
  876. here is equivalent to a combination of :func:`~torch.fft.ihfft` and
  877. :func:`~torch.fft.ifft`:
  878. >>> two_iffts = torch.fft.ifft(torch.fft.ihfft(t, dim=1), dim=0)
  879. >>> torch.allclose(ihfftn, two_iffts)
  880. True
  881. """.format(**common_args))
  882. fftfreq = _add_docstr(_fft.fft_fftfreq, r"""
  883. fftfreq(n, d=1.0, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor
  884. Computes the discrete Fourier Transform sample frequencies for a signal of size :attr:`n`.
  885. Note:
  886. By convention, :func:`~torch.fft.fft` returns positive frequency terms
  887. first, followed by the negative frequencies in reverse order, so that
  888. ``f[-i]`` for all :math:`0 < i \leq n/2`` in Python gives the negative
  889. frequency terms. For an FFT of length :attr:`n` and with inputs spaced in
  890. length unit :attr:`d`, the frequencies are::
  891. f = [0, 1, ..., (n - 1) // 2, -(n // 2), ..., -1] / (d * n)
  892. Note:
  893. For even lengths, the Nyquist frequency at ``f[n/2]`` can be thought of as
  894. either negative or positive. :func:`~torch.fft.fftfreq` follows NumPy's
  895. convention of taking it to be negative.
  896. Args:
  897. n (int): the FFT length
  898. d (float, optional): The sampling length scale.
  899. The spacing between individual samples of the FFT input.
  900. The default assumes unit spacing, dividing that result by the actual
  901. spacing gives the result in physical frequency units.
  902. Keyword Args:
  903. {out}
  904. {dtype}
  905. {layout}
  906. {device}
  907. {requires_grad}
  908. Example:
  909. >>> torch.fft.fftfreq(5)
  910. tensor([ 0.0000, 0.2000, 0.4000, -0.4000, -0.2000])
  911. For even input, we can see the Nyquist frequency at ``f[2]`` is given as
  912. negative:
  913. >>> torch.fft.fftfreq(4)
  914. tensor([ 0.0000, 0.2500, -0.5000, -0.2500])
  915. """.format(**factory_common_args))
  916. rfftfreq = _add_docstr(_fft.fft_rfftfreq, r"""
  917. rfftfreq(n, d=1.0, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor
  918. Computes the sample frequencies for :func:`~torch.fft.rfft` with a signal of size :attr:`n`.
  919. Note:
  920. :func:`~torch.fft.rfft` returns Hermitian one-sided output, so only the
  921. positive frequency terms are returned. For a real FFT of length :attr:`n`
  922. and with inputs spaced in length unit :attr:`d`, the frequencies are::
  923. f = torch.arange((n + 1) // 2) / (d * n)
  924. Note:
  925. For even lengths, the Nyquist frequency at ``f[n/2]`` can be thought of as
  926. either negative or positive. Unlike :func:`~torch.fft.fftfreq`,
  927. :func:`~torch.fft.rfftfreq` always returns it as positive.
  928. Args:
  929. n (int): the real FFT length
  930. d (float, optional): The sampling length scale.
  931. The spacing between individual samples of the FFT input.
  932. The default assumes unit spacing, dividing that result by the actual
  933. spacing gives the result in physical frequency units.
  934. Keyword Args:
  935. {out}
  936. {dtype}
  937. {layout}
  938. {device}
  939. {requires_grad}
  940. Example:
  941. >>> torch.fft.rfftfreq(5)
  942. tensor([0.0000, 0.2000, 0.4000])
  943. >>> torch.fft.rfftfreq(4)
  944. tensor([0.0000, 0.2500, 0.5000])
  945. Compared to the output from :func:`~torch.fft.fftfreq`, we see that the
  946. Nyquist frequency at ``f[2]`` has changed sign:
  947. >>> torch.fft.fftfreq(4)
  948. tensor([ 0.0000, 0.2500, -0.5000, -0.2500])
  949. """.format(**factory_common_args))
  950. fftshift = _add_docstr(_fft.fft_fftshift, r"""
  951. fftshift(input, dim=None) -> Tensor
  952. Reorders n-dimensional FFT data, as provided by :func:`~torch.fft.fftn`, to have
  953. negative frequency terms first.
  954. This performs a periodic shift of n-dimensional data such that the origin
  955. ``(0, ..., 0)`` is moved to the center of the tensor. Specifically, to
  956. ``input.shape[dim] // 2`` in each selected dimension.
  957. Note:
  958. By convention, the FFT returns positive frequency terms first, followed by
  959. the negative frequencies in reverse order, so that ``f[-i]`` for all
  960. :math:`0 < i \leq n/2` in Python gives the negative frequency terms.
  961. :func:`~torch.fft.fftshift` rearranges all frequencies into ascending order
  962. from negative to positive with the zero-frequency term in the center.
  963. Note:
  964. For even lengths, the Nyquist frequency at ``f[n/2]`` can be thought of as
  965. either negative or positive. :func:`~torch.fft.fftshift` always puts the
  966. Nyquist term at the 0-index. This is the same convention used by
  967. :func:`~torch.fft.fftfreq`.
  968. Args:
  969. input (Tensor): the tensor in FFT order
  970. dim (int, Tuple[int], optional): The dimensions to rearrange.
  971. Only dimensions specified here will be rearranged, any other dimensions
  972. will be left in their original order.
  973. Default: All dimensions of :attr:`input`.
  974. Example:
  975. >>> f = torch.fft.fftfreq(4)
  976. >>> f
  977. tensor([ 0.0000, 0.2500, -0.5000, -0.2500])
  978. >>> torch.fft.fftshift(f)
  979. tensor([-0.5000, -0.2500, 0.0000, 0.2500])
  980. Also notice that the Nyquist frequency term at ``f[2]`` was moved to the
  981. beginning of the tensor.
  982. This also works for multi-dimensional transforms:
  983. >>> x = torch.fft.fftfreq(5, d=1/5) + 0.1 * torch.fft.fftfreq(5, d=1/5).unsqueeze(1)
  984. >>> x
  985. tensor([[ 0.0000, 1.0000, 2.0000, -2.0000, -1.0000],
  986. [ 0.1000, 1.1000, 2.1000, -1.9000, -0.9000],
  987. [ 0.2000, 1.2000, 2.2000, -1.8000, -0.8000],
  988. [-0.2000, 0.8000, 1.8000, -2.2000, -1.2000],
  989. [-0.1000, 0.9000, 1.9000, -2.1000, -1.1000]])
  990. >>> torch.fft.fftshift(x)
  991. tensor([[-2.2000, -1.2000, -0.2000, 0.8000, 1.8000],
  992. [-2.1000, -1.1000, -0.1000, 0.9000, 1.9000],
  993. [-2.0000, -1.0000, 0.0000, 1.0000, 2.0000],
  994. [-1.9000, -0.9000, 0.1000, 1.1000, 2.1000],
  995. [-1.8000, -0.8000, 0.2000, 1.2000, 2.2000]])
  996. :func:`~torch.fft.fftshift` can also be useful for spatial data. If our
  997. data is defined on a centered grid (``[-(N//2), (N-1)//2]``) then we can
  998. use the standard FFT defined on an uncentered grid (``[0, N)``) by first
  999. applying an :func:`~torch.fft.ifftshift`.
  1000. >>> x_centered = torch.arange(-5, 5)
  1001. >>> x_uncentered = torch.fft.ifftshift(x_centered)
  1002. >>> fft_uncentered = torch.fft.fft(x_uncentered)
  1003. Similarly, we can convert the frequency domain components to centered
  1004. convention by applying :func:`~torch.fft.fftshift`.
  1005. >>> fft_centered = torch.fft.fftshift(fft_uncentered)
  1006. The inverse transform, from centered Fourier space back to centered spatial
  1007. data, can be performed by applying the inverse shifts in reverse order:
  1008. >>> x_centered_2 = torch.fft.fftshift(torch.fft.ifft(torch.fft.ifftshift(fft_centered)))
  1009. >>> torch.testing.assert_close(x_centered.to(torch.complex64), x_centered_2, check_stride=False)
  1010. """)
  1011. ifftshift = _add_docstr(_fft.fft_ifftshift, r"""
  1012. ifftshift(input, dim=None) -> Tensor
  1013. Inverse of :func:`~torch.fft.fftshift`.
  1014. Args:
  1015. input (Tensor): the tensor in FFT order
  1016. dim (int, Tuple[int], optional): The dimensions to rearrange.
  1017. Only dimensions specified here will be rearranged, any other dimensions
  1018. will be left in their original order.
  1019. Default: All dimensions of :attr:`input`.
  1020. Example:
  1021. >>> f = torch.fft.fftfreq(5)
  1022. >>> f
  1023. tensor([ 0.0000, 0.2000, 0.4000, -0.4000, -0.2000])
  1024. A round-trip through :func:`~torch.fft.fftshift` and
  1025. :func:`~torch.fft.ifftshift` gives the same result:
  1026. >>> shifted = torch.fft.fftshift(f)
  1027. >>> torch.fft.ifftshift(shifted)
  1028. tensor([ 0.0000, 0.2000, 0.4000, -0.4000, -0.2000])
  1029. """)