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]),那麼我們可以使用定義在非中心網格([0, N))上的標準 FFT,透過先應用ifftshift()來實現。>>> 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)