torch.fft
Discrete Fourier transforms and related functions.
Fast Fourier Transforms
-
torch.fft.fft(input, n=None, dim=-1, norm=None) → Tensor
-
Computes the one dimensional discrete Fourier transform of
input
.Note
The Fourier domain representation of any real signal satisfies the Hermitian property:
X[i] = conj(X[-i])
. This function always returns both the positive and negative frequency terms even though, for real inputs, the negative frequencies are redundant.rfft()
returns the more compact one-sided representation where only the positive frequencies are returned.- Parameters
-
- input (Tensor) – the input tensor
- n (int, optional) – Signal length. If given, the input will either be zero-padded or trimmed to this length before computing the FFT.
- dim (int, optional) – The dimension along which to take the one dimensional FFT.
-
norm (str, optional) –
Normalization mode. For the forward transform (
fft()
), these correspond to:-
"forward"
- normalize by1/n
-
"backward"
- no normalization -
"ortho"
- normalize by1/sqrt(n)
(making the FFT orthonormal)
Calling the backward transform (
ifft()
) with the same normalization mode will apply an overall normalization of1/n
between the two transforms. This is required to makeifft()
the exact inverse.Default is
"backward"
(no normalization). -
Example
>>> t = torch.arange(4) >>> t tensor([0, 1, 2, 3]) >>> torch.fft.fft(t) tensor([ 6.+0.j, -2.+2.j, -2.+0.j, -2.-2.j])
>>> t = tensor([0.+1.j, 2.+3.j, 4.+5.j, 6.+7.j]) >>> torch.fft.fft(t) tensor([12.+16.j, -8.+0.j, -4.-4.j, 0.-8.j])
-
torch.fft.ifft(input, n=None, dim=-1, norm=None) → Tensor
-
Computes the one dimensional inverse discrete Fourier transform of
input
.- Parameters
-
- input (Tensor) – the input tensor
- n (int, optional) – Signal length. If given, the input will either be zero-padded or trimmed to this length before computing the IFFT.
- dim (int, optional) – The dimension along which to take the one dimensional IFFT.
-
norm (str, optional) –
Normalization mode. For the backward transform (
ifft()
), these correspond to:-
"forward"
- no normalization -
"backward"
- normalize by1/n
-
"ortho"
- normalize by1/sqrt(n)
(making the IFFT orthonormal)
Calling the forward transform (
fft()
) with the same normalization mode will apply an overall normalization of1/n
between the two transforms. This is required to makeifft()
the exact inverse.Default is
"backward"
(normalize by1/n
). -
Example
>>> t = torch.tensor([ 6.+0.j, -2.+2.j, -2.+0.j, -2.-2.j]) >>> torch.fft.ifft(t) tensor([0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j])
-
torch.fft.fft2(input, s=None, dim=(-2, -1), norm=None) → Tensor
-
Computes the 2 dimensional discrete Fourier transform of
input
. Equivalent tofftn()
but FFTs only the last two dimensions by default.Note
The Fourier domain representation of any real signal satisfies the Hermitian property:
X[i, j] = conj(X[-i, -j])
. This function always returns all positive and negative frequency terms even though, for real inputs, half of these values are redundant.rfft2()
returns the more compact one-sided representation where only the positive frequencies of the last dimension are returned.- Parameters
-
- input (Tensor) – the input tensor
-
s (Tuple[int], optional) – Signal size in the transformed dimensions. If given, each dimension
dim[i]
will either be zero-padded or trimmed to the lengths[i]
before computing the FFT. If a length-1
is specified, no padding is done in that dimension. Default:s = [input.size(d) for d in dim]
- dim (Tuple[int], optional) – Dimensions to be transformed. Default: last two dimensions.
-
norm (str, optional) –
Normalization mode. For the forward transform (
fft2()
), these correspond to:-
"forward"
- normalize by1/n
-
"backward"
- no normalization -
"ortho"
- normalize by1/sqrt(n)
(making the FFT orthonormal)
Where
n = prod(s)
is the logical FFT size. Calling the backward transform (ifft2()
) with the same normalization mode will apply an overall normalization of1/n
between the two transforms. This is required to makeifft2()
the exact inverse.Default is
"backward"
(no normalization). -
Example
>>> x = torch.rand(10, 10, dtype=torch.complex64) >>> fft2 = torch.fft.fft2(t)
The discrete Fourier transform is separable, so
fft2()
here is equivalent to two one-dimensionalfft()
calls:>>> two_ffts = torch.fft.fft(torch.fft.fft(x, dim=0), dim=1) >>> torch.allclose(fft2, two_ffts)
-
torch.fft.ifft2(input, s=None, dim=(-2, -1), norm=None) → Tensor
-
Computes the 2 dimensional inverse discrete Fourier transform of
input
. Equivalent toifftn()
but IFFTs only the last two dimensions by default.- Parameters
-
- input (Tensor) – the input tensor
-
s (Tuple[int], optional) – Signal size in the transformed dimensions. If given, each dimension
dim[i]
will either be zero-padded or trimmed to the lengths[i]
before computing the IFFT. If a length-1
is specified, no padding is done in that dimension. Default:s = [input.size(d) for d in dim]
- dim (Tuple[int], optional) – Dimensions to be transformed. Default: last two dimensions.
-
norm (str, optional) –
Normalization mode. For the backward transform (
ifft2()
), these correspond to:-
"forward"
- no normalization -
"backward"
- normalize by1/n
-
"ortho"
- normalize by1/sqrt(n)
(making the IFFT orthonormal)
Where
n = prod(s)
is the logical IFFT size. Calling the forward transform (fft2()
) with the same normalization mode will apply an overall normalization of1/n
between the two transforms. This is required to makeifft2()
the exact inverse.Default is
"backward"
(normalize by1/n
). -
Example
>>> x = torch.rand(10, 10, dtype=torch.complex64) >>> ifft2 = torch.fft.ifft2(t)
The discrete Fourier transform is separable, so
ifft2()
here is equivalent to two one-dimensionalifft()
calls:>>> two_iffts = torch.fft.ifft(torch.fft.ifft(x, dim=0), dim=1) >>> torch.allclose(ifft2, two_iffts)
-
torch.fft.fftn(input, s=None, dim=None, norm=None) → Tensor
-
Computes the N dimensional discrete Fourier transform of
input
.Note
The Fourier domain representation of any real signal satisfies the Hermitian property:
X[i_1, ..., i_n] = conj(X[-i_1, ..., -i_n])
. This function always returns all positive and negative frequency terms even though, for real inputs, half of these values are redundant.rfftn()
returns the more compact one-sided representation where only the positive frequencies of the last dimension are returned.- Parameters
-
- input (Tensor) – the input tensor
-
s (Tuple[int], optional) – Signal size in the transformed dimensions. If given, each dimension
dim[i]
will either be zero-padded or trimmed to the lengths[i]
before computing the FFT. If a length-1
is specified, no padding is done in that dimension. Default:s = [input.size(d) for d in dim]
-
dim (Tuple[int], optional) – Dimensions to be transformed. Default: all dimensions, or the last
len(s)
dimensions ifs
is given. -
norm (str, optional) –
Normalization mode. For the forward transform (
fftn()
), these correspond to:-
"forward"
- normalize by1/n
-
"backward"
- no normalization -
"ortho"
- normalize by1/sqrt(n)
(making the FFT orthonormal)
Where
n = prod(s)
is the logical FFT size. Calling the backward transform (ifftn()
) with the same normalization mode will apply an overall normalization of1/n
between the two transforms. This is required to makeifftn()
the exact inverse.Default is
"backward"
(no normalization). -
Example
>>> x = torch.rand(10, 10, dtype=torch.complex64) >>> fftn = torch.fft.fftn(t)
The discrete Fourier transform is separable, so
fftn()
here is equivalent to two one-dimensionalfft()
calls:>>> two_ffts = torch.fft.fft(torch.fft.fft(x, dim=0), dim=1) >>> torch.allclose(fftn, two_ffts)
-
torch.fft.ifftn(input, s=None, dim=None, norm=None) → Tensor
-
Computes the N dimensional inverse discrete Fourier transform of
input
.- Parameters
-
- input (Tensor) – the input tensor
-
s (Tuple[int], optional) – Signal size in the transformed dimensions. If given, each dimension
dim[i]
will either be zero-padded or trimmed to the lengths[i]
before computing the IFFT. If a length-1
is specified, no padding is done in that dimension. Default:s = [input.size(d) for d in dim]
-
dim (Tuple[int], optional) – Dimensions to be transformed. Default: all dimensions, or the last
len(s)
dimensions ifs
is given. -
norm (str, optional) –
Normalization mode. For the backward transform (
ifftn()
), these correspond to:-
"forward"
- no normalization -
"backward"
- normalize by1/n
-
"ortho"
- normalize by1/sqrt(n)
(making the IFFT orthonormal)
Where
n = prod(s)
is the logical IFFT size. Calling the forward transform (fftn()
) with the same normalization mode will apply an overall normalization of1/n
between the two transforms. This is required to makeifftn()
the exact inverse.Default is
"backward"
(normalize by1/n
). -
Example
>>> x = torch.rand(10, 10, dtype=torch.complex64) >>> ifftn = torch.fft.ifftn(t)
The discrete Fourier transform is separable, so
ifftn()
here is equivalent to two one-dimensionalifft()
calls:>>> two_iffts = torch.fft.ifft(torch.fft.ifft(x, dim=0), dim=1) >>> torch.allclose(ifftn, two_iffts)
-
torch.fft.rfft(input, n=None, dim=-1, norm=None) → Tensor
-
Computes the one dimensional Fourier transform of real-valued
input
.The FFT of a real signal is Hermitian-symmetric,
X[i] = conj(X[-i])
so the output contains only the positive frequencies below the Nyquist frequency. To compute the full output, usefft()
- Parameters
-
- input (Tensor) – the real input tensor
- n (int, optional) – Signal length. If given, the input will either be zero-padded or trimmed to this length before computing the real FFT.
- dim (int, optional) – The dimension along which to take the one dimensional real FFT.
-
norm (str, optional) –
Normalization mode. For the forward transform (
rfft()
), these correspond to:-
"forward"
- normalize by1/n
-
"backward"
- no normalization -
"ortho"
- normalize by1/sqrt(n)
(making the FFT orthonormal)
Calling the backward transform (
irfft()
) with the same normalization mode will apply an overall normalization of1/n
between the two transforms. This is required to makeirfft()
the exact inverse.Default is
"backward"
(no normalization). -
Example
>>> t = torch.arange(4) >>> t tensor([0, 1, 2, 3]) >>> torch.fft.rfft(t) tensor([ 6.+0.j, -2.+2.j, -2.+0.j])
Compare against the full output from
fft()
:>>> torch.fft.fft(t) tensor([ 6.+0.j, -2.+2.j, -2.+0.j, -2.-2.j])
Notice that the symmetric element
T[-1] == T[1].conj()
is omitted. At the Nyquist frequencyT[-2] == T[2]
is it’s own symmetric pair, and therefore must always be real-valued.
-
torch.fft.irfft(input, n=None, dim=-1, norm=None) → Tensor
-
Computes the inverse of
rfft()
.input
is interpreted as a one-sided Hermitian signal in the Fourier domain, as produced byrfft()
. By the Hermitian property, the output will be real-valued.Note
Some input frequencies must be real-valued to satisfy the Hermitian property. In these cases the imaginary component will be ignored. For example, any imaginary component in the zero-frequency term cannot be represented in a real output and so will always be ignored.
Note
The correct interpretation of the Hermitian input depends on the length of the original data, as given by
n
. This is because each input shape could correspond to either an odd or even length signal. By default, the signal is assumed to be even length and odd signals will not round-trip properly. So, it is recommended to always pass the signal lengthn
.- Parameters
-
- input (Tensor) – the input tensor representing a half-Hermitian signal
-
n (int, optional) – Output signal length. This determines the length of the output signal. If given, the input will either be zero-padded or trimmed to this length before computing the real IFFT. Defaults to even output:
n=2*(input.size(dim) - 1)
. - dim (int, optional) – The dimension along which to take the one dimensional real IFFT.
-
norm (str, optional) –
Normalization mode. For the backward transform (
irfft()
), these correspond to:-
"forward"
- no normalization -
"backward"
- normalize by1/n
-
"ortho"
- normalize by1/sqrt(n)
(making the real IFFT orthonormal)
Calling the forward transform (
rfft()
) with the same normalization mode will apply an overall normalization of1/n
between the two transforms. This is required to makeirfft()
the exact inverse.Default is
"backward"
(normalize by1/n
). -
Example
>>> t = torch.arange(5) >>> t tensor([0, 1, 2, 3, 4]) >>> T = torch.fft.rfft(t) >>> T tensor([10.0000+0.0000j, -2.5000+3.4410j, -2.5000+0.8123j])
Without specifying the output length to
irfft()
, the output will not round-trip properly because the input is odd-length:>>> torch.fft.irfft(T) tensor([0.6250, 1.4045, 3.1250, 4.8455])
So, it is recommended to always pass the signal length
n
:>>> torch.fft.irfft(T, t.numel()) tensor([0.0000, 1.0000, 2.0000, 3.0000, 4.0000])
-
torch.fft.rfft2(input, s=None, dim=(-2, -1), norm=None) → Tensor
-
Computes the 2-dimensional discrete Fourier transform of real
input
. Equivalent torfftn()
but FFTs only the last two dimensions by default.The FFT of a real signal is Hermitian-symmetric,
X[i, j] = conj(X[-i, -j])
, so the fullfft2()
output contains redundant information.rfft2()
instead omits the negative frequencies in the last dimension.- Parameters
-
- input (Tensor) – the input tensor
-
s (Tuple[int], optional) – Signal size in the transformed dimensions. If given, each dimension
dim[i]
will either be zero-padded or trimmed to the lengths[i]
before computing the real FFT. If a length-1
is specified, no padding is done in that dimension. Default:s = [input.size(d) for d in dim]
- dim (Tuple[int], optional) – Dimensions to be transformed. Default: last two dimensions.
-
norm (str, optional) –
Normalization mode. For the forward transform (
rfft2()
), these correspond to:-
"forward"
- normalize by1/n
-
"backward"
- no normalization -
"ortho"
- normalize by1/sqrt(n)
(making the real FFT orthonormal)
Where
n = prod(s)
is the logical FFT size. Calling the backward transform (irfft2()
) with the same normalization mode will apply an overall normalization of1/n
between the two transforms. This is required to makeirfft2()
the exact inverse.Default is
"backward"
(no normalization). -
Example
>>> t = torch.rand(10, 10) >>> rfft2 = torch.fft.rfft2(t) >>> rfft2.size() torch.Size([10, 6])
Compared against the full output from
fft2()
, we have all elements up to the Nyquist frequency.>>> fft2 = torch.fft.fft2(t) >>> torch.allclose(fft2[..., :6], rfft2) True
The discrete Fourier transform is separable, so
rfft2()
here is equivalent to a combination offft()
andrfft()
:>>> two_ffts = torch.fft.fft(torch.fft.rfft(x, dim=1), dim=0) >>> torch.allclose(rfft2, two_ffts)
-
torch.fft.irfft2(input, s=None, dim=(-2, -1), norm=None) → Tensor
-
Computes the inverse of
rfft2()
. Equivalent toirfftn()
but IFFTs only the last two dimensions by default.input
is interpreted as a one-sided Hermitian signal in the Fourier domain, as produced byrfft2()
. By the Hermitian property, the output will be real-valued.Note
Some input frequencies must be real-valued to satisfy the Hermitian property. In these cases the imaginary component will be ignored. For example, any imaginary component in the zero-frequency term cannot be represented in a real output and so will always be ignored.
Note
The correct interpretation of the Hermitian input depends on the length of the original data, as given by
s
. This is because each input shape could correspond to either an odd or even length signal. By default, the signal is assumed to be even length and odd signals will not round-trip properly. So, it is recommended to always pass the signal shapes
.- Parameters
-
- input (Tensor) – the input tensor
-
s (Tuple[int], optional) – Signal size in the transformed dimensions. If given, each dimension
dim[i]
will either be zero-padded or trimmed to the lengths[i]
before computing the real FFT. If a length-1
is specified, no padding is done in that dimension. Defaults to even output in the last dimension:s[-1] = 2*(input.size(dim[-1]) - 1)
. - dim (Tuple[int], optional) – Dimensions to be transformed. The last dimension must be the half-Hermitian compressed dimension. Default: last two dimensions.
-
norm (str, optional) –
Normalization mode. For the backward transform (
irfft2()
), these correspond to:-
"forward"
- no normalization -
"backward"
- normalize by1/n
-
"ortho"
- normalize by1/sqrt(n)
(making the real IFFT orthonormal)
Where
n = prod(s)
is the logical IFFT size. Calling the forward transform (rfft2()
) with the same normalization mode will apply an overall normalization of1/n
between the two transforms. This is required to makeirfft2()
the exact inverse.Default is
"backward"
(normalize by1/n
). -
Example
>>> t = torch.rand(10, 9) >>> T = torch.fft.rfft2(t)
Without specifying the output length to
irfft2()
, the output will not round-trip properly because the input is odd-length in the last dimension:>>> torch.fft.irfft2(T).size() torch.Size([10, 10])
So, it is recommended to always pass the signal shape
s
.>>> roundtrip = torch.fft.irfft2(T, t.size()) >>> roundtrip.size() torch.Size([10, 9]) >>> torch.allclose(roundtrip, t) True
-
torch.fft.rfftn(input, s=None, dim=None, norm=None) → Tensor
-
Computes the N-dimensional discrete Fourier transform of real
input
.The FFT of a real signal is Hermitian-symmetric,
X[i_1, ..., i_n] = conj(X[-i_1, ..., -i_n])
so the fullfftn()
output contains redundant information.rfftn()
instead omits the negative frequencies in the last dimension.- Parameters
-
- input (Tensor) – the input tensor
-
s (Tuple[int], optional) – Signal size in the transformed dimensions. If given, each dimension
dim[i]
will either be zero-padded or trimmed to the lengths[i]
before computing the real FFT. If a length-1
is specified, no padding is done in that dimension. Default:s = [input.size(d) for d in dim]
-
dim (Tuple[int], optional) – Dimensions to be transformed. Default: all dimensions, or the last
len(s)
dimensions ifs
is given. -
norm (str, optional) –
Normalization mode. For the forward transform (
rfftn()
), these correspond to:-
"forward"
- normalize by1/n
-
"backward"
- no normalization -
"ortho"
- normalize by1/sqrt(n)
(making the real FFT orthonormal)
Where
n = prod(s)
is the logical FFT size. Calling the backward transform (irfftn()
) with the same normalization mode will apply an overall normalization of1/n
between the two transforms. This is required to makeirfftn()
the exact inverse.Default is
"backward"
(no normalization). -
Example
>>> t = torch.rand(10, 10) >>> rfftn = torch.fft.rfftn(t) >>> rfftn.size() torch.Size([10, 6])
Compared against the full output from
fftn()
, we have all elements up to the Nyquist frequency.>>> fftn = torch.fft.fftn(t) >>> torch.allclose(fftn[..., :6], rfftn) True
The discrete Fourier transform is separable, so
rfftn()
here is equivalent to a combination offft()
andrfft()
:>>> two_ffts = torch.fft.fft(torch.fft.rfft(x, dim=1), dim=0) >>> torch.allclose(rfftn, two_ffts)
-
torch.fft.irfftn(input, s=None, dim=None, norm=None) → Tensor
-
Computes the inverse of
rfftn()
.input
is interpreted as a one-sided Hermitian signal in the Fourier domain, as produced byrfftn()
. By the Hermitian property, the output will be real-valued.Note
Some input frequencies must be real-valued to satisfy the Hermitian property. In these cases the imaginary component will be ignored. For example, any imaginary component in the zero-frequency term cannot be represented in a real output and so will always be ignored.
Note
The correct interpretation of the Hermitian input depends on the length of the original data, as given by
s
. This is because each input shape could correspond to either an odd or even length signal. By default, the signal is assumed to be even length and odd signals will not round-trip properly. So, it is recommended to always pass the signal shapes
.- Parameters
-
- input (Tensor) – the input tensor
-
s (Tuple[int], optional) – Signal size in the transformed dimensions. If given, each dimension
dim[i]
will either be zero-padded or trimmed to the lengths[i]
before computing the real FFT. If a length-1
is specified, no padding is done in that dimension. Defaults to even output in the last dimension:s[-1] = 2*(input.size(dim[-1]) - 1)
. -
dim (Tuple[int], optional) – Dimensions to be transformed. The last dimension must be the half-Hermitian compressed dimension. Default: all dimensions, or the last
len(s)
dimensions ifs
is given. -
norm (str, optional) –
Normalization mode. For the backward transform (
irfftn()
), these correspond to:-
"forward"
- no normalization -
"backward"
- normalize by1/n
-
"ortho"
- normalize by1/sqrt(n)
(making the real IFFT orthonormal)
Where
n = prod(s)
is the logical IFFT size. Calling the forward transform (rfftn()
) with the same normalization mode will apply an overall normalization of1/n
between the two transforms. This is required to makeirfftn()
the exact inverse.Default is
"backward"
(normalize by1/n
). -
Example
>>> t = torch.rand(10, 9) >>> T = torch.fft.rfftn(t)
Without specifying the output length to
irfft()
, the output will not round-trip properly because the input is odd-length in the last dimension:>>> torch.fft.irfftn(T).size() torch.Size([10, 10])
So, it is recommended to always pass the signal shape
s
.>>> roundtrip = torch.fft.irfftn(T, t.size()) >>> roundtrip.size() torch.Size([10, 9]) >>> torch.allclose(roundtrip, t) True
-
torch.fft.hfft(input, n=None, dim=-1, norm=None) → Tensor
-
Computes the one dimensional discrete Fourier transform of a Hermitian symmetric
input
signal.Note
hfft()
/ihfft()
are analogous torfft()
/irfft()
. The real FFT expects a real signal in the time-domain and gives a Hermitian symmetry in the frequency-domain. The Hermitian FFT is the opposite; Hermitian symmetric in the time-domain and real-valued in the frequency-domain. For this reason, special care needs to be taken with the length argumentn
, in the same way as withirfft()
.Note
Because the signal is Hermitian in the time-domain, the result will be real in the frequency domain. Note that some input frequencies must be real-valued to satisfy the Hermitian property. In these cases the imaginary component will be ignored. For example, any imaginary component in
input[0]
would result in one or more complex frequency terms which cannot be represented in a real output and so will always be ignored.Note
The correct interpretation of the Hermitian input depends on the length of the original data, as given by
n
. This is because each input shape could correspond to either an odd or even length signal. By default, the signal is assumed to be even length and odd signals will not round-trip properly. So, it is recommended to always pass the signal lengthn
.- Parameters
-
- input (Tensor) – the input tensor representing a half-Hermitian signal
-
n (int, optional) – Output signal length. This determines the length of the real output. If given, the input will either be zero-padded or trimmed to this length before computing the Hermitian FFT. Defaults to even output:
n=2*(input.size(dim) - 1)
. - dim (int, optional) – The dimension along which to take the one dimensional Hermitian FFT.
-
norm (str, optional) –
Normalization mode. For the forward transform (
hfft()
), these correspond to:-
"forward"
- normalize by1/n
-
"backward"
- no normalization -
"ortho"
- normalize by1/sqrt(n)
(making the Hermitian FFT orthonormal)
Calling the backward transform (
ihfft()
) with the same normalization mode will apply an overall normalization of1/n
between the two transforms. This is required to makeihfft()
the exact inverse.Default is
"backward"
(no normalization). -
Example
Taking a real-valued frequency signal and bringing it into the time domain gives Hermitian symmetric output:
>>> t = torch.arange(5) >>> t tensor([0, 1, 2, 3, 4]) >>> T = torch.fft.ifft(t) >>> T tensor([ 2.0000+-0.0000j, -0.5000-0.6882j, -0.5000-0.1625j, -0.5000+0.1625j, -0.5000+0.6882j])
Note that
T[1] == T[-1].conj()
andT[2] == T[-2].conj()
is redundant. We can thus compute the forward transform without considering negative frequencies:>>> torch.fft.hfft(T[:3], n=5) tensor([0., 1., 2., 3., 4.])
Like with
irfft()
, the output length must be given in order to recover an even length output:>>> torch.fft.hfft(T[:3]) tensor([0.5000, 1.1236, 2.5000, 3.8764])
-
torch.fft.ihfft(input, n=None, dim=-1, norm=None) → Tensor
-
Computes the inverse of
hfft()
.input
must be a real-valued signal, interpreted in the Fourier domain. The IFFT of a real signal is Hermitian-symmetric,X[i] = conj(X[-i])
.ihfft()
represents this in the one-sided form where only the positive frequencies below the Nyquist frequency are included. To compute the full output, useifft()
.- Parameters
-
- input (Tensor) – the real input tensor
- n (int, optional) – Signal length. If given, the input will either be zero-padded or trimmed to this length before computing the Hermitian IFFT.
- dim (int, optional) – The dimension along which to take the one dimensional Hermitian IFFT.
-
norm (str, optional) –
Normalization mode. For the backward transform (
ihfft()
), these correspond to:-
"forward"
- no normalization -
"backward"
- normalize by1/n
-
"ortho"
- normalize by1/sqrt(n)
(making the IFFT orthonormal)
Calling the forward transform (
hfft()
) with the same normalization mode will apply an overall normalization of1/n
between the two transforms. This is required to makeihfft()
the exact inverse.Default is
"backward"
(normalize by1/n
). -
Example
>>> t = torch.arange(5) >>> t tensor([0, 1, 2, 3, 4]) >>> torch.fft.ihfft(t) tensor([ 2.0000+-0.0000j, -0.5000-0.6882j, -0.5000-0.1625j])
Compare against the full output from
ifft()
:>>> torch.fft.ifft(t) tensor([ 2.0000+-0.0000j, -0.5000-0.6882j, -0.5000-0.1625j, -0.5000+0.1625j, -0.5000+0.6882j])
Helper Functions
-
torch.fft.fftfreq(n, d=1.0, *, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
-
Computes the discrete Fourier Transform sample frequencies for a signal of size
n
.Note
By convention,
fft()
returns positive frequency terms first, followed by the negative frequencies in reverse order, so thatf[-i]
for all in Python gives the negative frequency terms. For an FFT of lengthn
and with inputs spaced in length unitd
, the frequencies are:f = [0, 1, ..., (n - 1) // 2, -(n // 2), ..., -1] / (d * n)
Note
For even lengths, the Nyquist frequency at
f[n/2]
can be thought of as either negative or positive.fftfreq()
follows NumPy’s convention of taking it to be negative.- Parameters
- Keyword Arguments
-
-
dtype (
torch.dtype
, optional) – the desired data type of returned tensor. Default: ifNone
, uses a global default (seetorch.set_default_tensor_type()
). -
layout (
torch.layout
, optional) – the desired layout of returned Tensor. Default:torch.strided
. -
device (
torch.device
, optional) – the desired device of returned tensor. Default: ifNone
, uses the current device for the default tensor type (seetorch.set_default_tensor_type()
).device
will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types. -
requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default:
False
.
-
dtype (
Example
>>> torch.fft.fftfreq(5) tensor([ 0.0000, 0.2000, 0.4000, -0.4000, -0.2000])
For even input, we can see the Nyquist frequency at
f[2]
is given as negative:>>> torch.fft.fftfreq(4) tensor([ 0.0000, 0.2500, -0.5000, -0.2500])
-
torch.fft.rfftfreq(n, d=1.0, *, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
-
Computes the sample frequencies for
rfft()
with a signal of sizen
.Note
rfft()
returns Hermitian one-sided output, so only the positive frequency terms are returned. For a real FFT of lengthn
and with inputs spaced in length unitd
, the frequencies are:f = torch.arange((n + 1) // 2) / (d * n)
Note
For even lengths, the Nyquist frequency at
f[n/2]
can be thought of as either negative or positive. Unlikefftfreq()
,rfftfreq()
always returns it as positive.- Parameters
- Keyword Arguments
-
-
dtype (
torch.dtype
, optional) – the desired data type of returned tensor. Default: ifNone
, uses a global default (seetorch.set_default_tensor_type()
). -
layout (
torch.layout
, optional) – the desired layout of returned Tensor. Default:torch.strided
. -
device (
torch.device
, optional) – the desired device of returned tensor. Default: ifNone
, uses the current device for the default tensor type (seetorch.set_default_tensor_type()
).device
will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types. -
requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default:
False
.
-
dtype (
Example
>>> torch.fft.rfftfreq(5) tensor([ 0.0000, 0.2000, 0.4000])
>>> torch.fft.rfftfreq(4) tensor([ 0.0000, 0.2500, 0.5000])
Compared to the output from
fftfreq()
, we see that the Nyquist frequency atf[2]
has changed sign: >>> torch.fft.fftfreq(4) tensor([ 0.0000, 0.2500, -0.5000, -0.2500])
-
torch.fft.fftshift(input, dim=None) → Tensor
-
Reorders n-dimensional FFT data, as provided by
fftn()
, to have negative frequency terms first.This performs a periodic shift of n-dimensional data such that the origin
(0, ..., 0)
is moved to the center of the tensor. Specifically, toinput.shape[dim] // 2
in each selected dimension.Note
By convention, the FFT returns positive frequency terms first, followed by the negative frequencies in reverse order, so that
f[-i]
for all in Python gives the negative frequency terms.fftshift()
rearranges all frequencies into ascending order from negative to positive with the zero-frequency term in the center.Note
For even lengths, the Nyquist frequency at
f[n/2]
can be thought of as either negative or positive.fftshift()
always puts the Nyquist term at the 0-index. This is the same convention used byfftfreq()
.- Parameters
Example
>>> f = torch.fft.fftfreq(4) >>> f tensor([ 0.0000, 0.2500, -0.5000, -0.2500])
>>> torch.fft.fftshift(f) tensor([-0.5000, -0.2500, 0.0000, 0.2500])
Also notice that the Nyquist frequency term at
f[2]
was moved to the beginning of the tensor.This also works for multi-dimensional transforms:
>>> x = torch.fft.fftfreq(5, d=1/5) + 0.1 * torch.fft.fftfreq(5, d=1/5).unsqueeze(1) >>> x tensor([[ 0.0000, 1.0000, 2.0000, -2.0000, -1.0000], [ 0.1000, 1.1000, 2.1000, -1.9000, -0.9000], [ 0.2000, 1.2000, 2.2000, -1.8000, -0.8000], [-0.2000, 0.8000, 1.8000, -2.2000, -1.2000], [-0.1000, 0.9000, 1.9000, -2.1000, -1.1000]])
>>> torch.fft.fftshift(x) tensor([[-2.2000, -1.2000, -0.2000, 0.8000, 1.8000], [-2.1000, -1.1000, -0.1000, 0.9000, 1.9000], [-2.0000, -1.0000, 0.0000, 1.0000, 2.0000], [-1.9000, -0.9000, 0.1000, 1.1000, 2.1000], [-1.8000, -0.8000, 0.2000, 1.2000, 2.2000]])
fftshift()
can also be useful for spatial data. If our data is defined on a centered grid ([-(N//2), (N-1)//2]
) then we can use the standard FFT defined on an uncentered grid ([0, N)
) by first applying anifftshift()
.>>> x_centered = torch.arange(-5, 5) >>> x_uncentered = torch.fft.ifftshift(x_centered) >>> fft_uncentered = torch.fft.fft(x_uncentered)
Similarly, we can convert the frequency domain components to centered convention by applying
fftshift()
.>>> fft_centered = torch.fft.fftshift(fft_uncentered)
The inverse transform, from centered Fourier space back to centered spatial data, can be performed by applying the inverse shifts in reverse order:
>>> x_centered_2 = torch.fft.fftshift(torch.fft.ifft(torch.fft.ifftshift(fft_centered))) >>> torch.allclose(x_centered.to(torch.complex64), x_centered_2) True
-
torch.fft.ifftshift(input, dim=None) → Tensor
-
Inverse of
fftshift()
.- Parameters
Example
>>> f = torch.fft.fftfreq(5) >>> f tensor([ 0.0000, 0.2000, 0.4000, -0.4000, -0.2000])
A round-trip through
fftshift()
andifftshift()
gives the same result:>>> shifted = torch.fftshift(f) >>> torch.ifftshift(shifted) tensor([ 0.0000, 0.2000, 0.4000, -0.4000, -0.2000])
© 2019 Torch Contributors
Licensed under the 3-clause BSD License.
https://pytorch.org/docs/1.8.0/fft.html