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