Shortcuts

torch.fft.fftshift

torch.fft.fftshift(input, dim=None) 张量

重新排序由 fftn() 提供的 n 维 FFT 数据,使其负频率项在前。

这将对n维数据进行周期性移位,使得原点 (0, ..., 0) 移动到张量的中心。具体来说,在每个选定的维度上移动到 input.shape[dim] // 2

注意

按照惯例,FFT首先返回正频率项,然后是负频率项,按相反顺序排列,因此在Python中,对于所有0<in/20 < i \leq n/2f[-i]给出负频率项。 fftshift()将所有频率重新排列为从负到正的升序,零频率项位于中心。

注意

对于偶数长度,奈奎斯特频率在 f[n/2] 可以被认为是 负或正的。fftshift() 总是将 奈奎斯特项放在0索引处。这与 fftfreq() 使用的约定相同。

Parameters
  • 输入 (张量) – 以FFT顺序排列的张量

  • dim (int, Tuple[int], optional) – 要重新排列的维度。 只有在这里指定的维度会被重新排列,任何其他维度将保持其原始顺序。 默认值:input 的所有维度。

示例

>>> f = torch.fft.fftfreq(4)
>>> f
张量([ 0.0000,  0.2500, -0.5000, -0.2500])
>>> torch.fft.fftshift(f)
张量([-0.5000, -0.2500,  0.0000,  0.2500])

另请注意,奈奎斯特频率项在 f[2] 被移动到了张量的开头。

这也适用于多维变换:

>>> x = torch.fft.fftfreq(5, d=1/5) + 0.1 * torch.fft.fftfreq(5, d=1/5).unsqueeze(1)
>>> x
张量([[ 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)
张量([[-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() 来使用定义在未中心化网格([0, N))上的标准FFT。

>>> 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)
优云智算