torch.fft.fftshift¶
- torch.fft.fftshift(input, dim=None) → Tensor
将由
fftn()
提供的 n 维 FFT 数据重新排序,以便首先具有负频率项。这将执行 n 维数据的周期性平移,使得原点
(0, ..., 0)
移动到张量的中心。具体来说,每个选定的维度移动到input.shape[dim] // 2
。注意
按照惯例,快速傅里叶变换(FFT)首先返回正频率项,然后是按相反顺序排列的负频率项,因此 Python 中的所有 对于
f[-i]
都给出负频率项。fftshift()
将所有频率重新排列成从负到正的升序,零频率项位于中心。注意
对于偶数长度,奈奎斯特频率
f[n/2]
可以认为是正的或负的。fftshift()
总是将奈奎斯特项放在 0 索引处。这是fftfreq()
使用的相同约定。- 参数:
输入(张量)- FFT 顺序中的张量
dim(int,Tuple[int],可选)- 要重新排列的维度。只有在此处指定的维度将被重新排列,任何其他维度将保持原始顺序。默认:
input
的所有维度。
示例
>>> 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])
注意,Nyquist 频率项在
f[2]
被移到了张量的开头。这也适用于多维变换:
>>> 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()
对于空间数据也很有用。如果我们的数据定义在中心网格([-(N//2), (N-1)//2]
)上,那么我们可以通过首先应用一个ifftshift()
来使用定义在非中心网格上的标准 FFT([0, N)
)。>>> x_centered = torch.arange(-5, 5) >>> x_uncentered = torch.fft.ifftshift(x_centered) >>> fft_uncentered = torch.fft.fft(x_uncentered)
同样,我们可以通过应用
fftshift()
将频域分量转换为中心惯例。>>> fft_centered = torch.fft.fftshift(fft_uncentered)
从中心傅里叶空间到中心空间数据的逆变换可以通过按相反顺序应用逆移位来执行:
>>> x_centered_2 = torch.fft.fftshift(torch.fft.ifft(torch.fft.ifftshift(fft_centered))) >>> torch.testing.assert_close(x_centered.to(torch.complex64), x_centered_2, check_stride=False)