Replace torch.rfft() code (PyTorch 1.6)

I want to use a repository that was built with PyTorch 1.6, with newer versions of PyTorch (preferably the latest). This repository uses

torch.rfft()

which was deprecated to torch.fft.rfft in later PyTorch versions.

The snippet of code that I want to replace is

v = torch.cat([x[:, ::2], x[:, 1::2].flip([1])], dim=1)
Vc = torch.fft.rfft(v, 1, onesided=False)

where x is a two dimensional tensor. In the comments around that piece of code, it is written that

Discrete Cosine Transform, Type II (a.k.a. the DCT)

I switched torch.rfft() with torch.fft.rfft but then I get the error

TypeError: fft_rfft() got an unexpected keyword argument 'onesided'

Does anyone know the exact equivalent of this code to the current PyTorch version?

Additionally, I have also found this line of code

v = torch.irfft(V, 1, onesided=False)

which also needs an update.

Thank you in advance.

I found the equivalent code for that line. Since oneseded=False, the corresponding function to use is torch.fft.fft. Additionally, since PyTorch supports complex values, to produce the same output with that of the deprecated version, torch.view_as_real must be used. Below is the equivalent code of Vc = torch.fft.rfft(v, 1, onesided=False)

Vc = torch.fft.fft(v)
Vc = torch.view_as_real(Vc)

@ptrch_c_m i found one reference post Fourier transform in latest pytorch

Very difficult to find a runtime env where we can install torch==1.6.0 to verify the results unfortunately. It would be great if you can try the above article’s suggestions from your end