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()使用的約定相同。- 引數
示例
>>> 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)