torch.fft.fftshift¶
- torch.fft.fftshift(input, dim=None) Tensor ¶
重新排序 n 維 FFT 資料,由
fftn()
提供,使負頻率項優先。此操作會執行 n 維資料的週期性位移,使原點
(0, ..., 0)
移動到 Tensor 的中心。具體而言,對於每個選定的維度,移動到input.shape[dim] // 2
。注意
依照慣例,FFT 會先回傳正頻率項,然後是反向順序的負頻率項,因此對於 Python 中的所有 ,
f[-i]
會提供負頻率項。fftshift()
將所有頻率重新排列為從負到正的升序,零頻率項位於中心。注意
對於偶數長度,
f[n/2]
處的奈奎斯特頻率可以被認為是負數或正數。fftshift()
總是將奈奎斯特項放在索引 0 處。這與fftfreq()
使用的慣例相同。- 參數
範例
>>> 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)