Torch.istft onesided output error

I try to do some DSP thing and I need to convert frame into single bin and then restore the frame from that bin. Here’s minimal example:

import torch
import matplotlib.pyplot as plt

frame = torch.rand(1536)
bin = torch.stft(frame, n_fft=frame.shape[0], hop_length=frame.shape[0], center=False,
                     return_complex=True, window=torch.hann_window(frame.shape[0]))
rest_frame = torch.istft(bin, n_fft=frame.shape[0], hop_length=frame.shape[0], center=False,
                        return_complex=True, window=torch.hann_window(frame.shape[0]))

plt.plot(rest_frame-frame)
plt.show()

I expected to get the same frame, but instead I got

RuntimeError: Cannot have onesided output if window or input is complex

in istft. torch.stft produces the bin that I need, but I still can’t make istft work. What do I do wrong?

Hi @jBloodless, thanks for sharing. In this case, I suggest changing the return_complex to False in torch.istft.

Also the n_fft and hop_length and window length need to be carefully selected, as torch.istft requires the settings to pass NOLA check to make the signal invertible. Check this doc for more information: Invertibility of overlap-add processing