快捷方式

torch.fft.fftshift

torch.fft.fftshift(input, dim=None) Tensor

重新排列由 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() 使用的約定相同。

引數
  • input (Tensor) – 以 FFT 順序排列的張量

  • dim (int, Tuple[int], optional) – 需要重新排列的維度。只有指定的維度會被重新排列,其他維度將保持原始順序。預設值: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])

另請注意,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() 來使用在未中心化網格 ([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)

文件

訪問 PyTorch 全面的開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源