首頁 > 軟體

Python torch.fft.rfft()函數用法範例程式碼

2022-04-14 16:02:53

在新舊版的torch中的傅立葉變換函數在定義和用法上存在不同,記錄一下。

1、舊版

fft = torch.rfft(input, 2, normalized=True, onesided=False)
#  input 為輸入的圖片或者向量,dtype=torch.float32,size比如為[1,3,64,64]
#  signal_ndim(int):The number of dimensions in each signal,can only be 1、2、3
#  normalized(bool,optional):controls wheather to return normallized results. Default:False
#  onesided(bool,optional):controls whether to return half of results to avoid redundancy.Default:True 

上面例子中影象中 singal_ndim = 2 ,是因為輸入影象是2維的。

1.7之後的版本中,如果要用 oneside output,則改用torch.fft.rfft();如果要用two-side output,則改用torch.fft.fft()

input= torch.arange(4)
fft = torch.rfft(input, 2, normalized=True, onesided=False)

2、新版

一維離散傅立葉變換

torch.fft.rfft(input,n=None,dim=-1,norm=None) --> Tensor
# input:Tensor
# n(int,optional):Output signal length. This determines the length of the
        output signal. 
# dim(int, optional): The dimension along which to take the one dimensional real IFFT.
# norm (str, optional): Normalization mode.

二維離散傅立葉變換 

torch.fft.rfft2(input, s=None, dim=(-2, -1), norm=None, *, out=None) -> Tensor
input (Tensor): the input tensor
s (Tuple[int], optional): Signal size in the transformed dimensions.
dim (Tuple[int], optional): Dimensions to be transformed.
norm (str, optional): Normalization mode.

高維離散傅立葉變換 

rfftn(input, s=None, dim=None, norm=None, *, out=None) -> Tensor
input (Tensor): the input tensor
s (Tuple[int], optional): Signal size in the transformed dimensions.
dim (Tuple[int], optional): Dimensions to be transformed.
norm (str, optional): Normalization mode. For the forward transform

3、新舊版對比

import torch
input = torch.rand(1,3,32,32)
 
# 舊版pytorch.rfft()函數
fft = torch.rfft(input, 2, normalized=True, onesided=False)
 
# 新版 pytorch.fft.rfft2()函數
output = torch.fft.fft2(input, dim=(-2, -1))
output = torch.stack((output.real, output_new.imag), -1)
 
ffted = torch.rfft(input, 1, onesided=False) to ffted = torch.view_as_real(torch.fft.fft(input, dim=1))
and
iffted = torch.irfft(time_step_as_inner, 1, onesided=False) to
iffted = torch.fft.irfft(torch.view_as_complex(time_step_as_inner), n=time_step_as_inner.shape[1], dim=1)

補充:使用numpy模擬torch.fft.fft拯救paddle

import numpy as np
import torch
import paddle
def paddle_fft(x,dim=-1):
    if dim==-1:
        return  paddle.to_tensor(np.fft.fft(x.numpy()))
    else:
        shape= [i for i in range(len(x.shape))]
        shape[dim],shape[-1]=shape[-1],shape[dim]

        x=np.transpose(np.fft.fft(np.transpose(x.numpy(), shape)),shape)
        return paddle.to_tensor(x)





if __name__ == '__main__':
    data=paddle.to_tensor(np.array([[[1, 4, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3]]]))

    paddle_f_d=paddle_fft(paddle_fft(data,-1),-2)
    torch_f_d =paddle_fft(torch.fft.fft(torch.Tensor(data.numpy()),dim=-1),-2)
    print(paddle_f_d.numpy())
    print(torch_f_d.numpy())

總結

到此這篇關於Python torch.fft.rfft()函數用法的文章就介紹到這了,更多相關torch.fft.rfft()函數用法內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com