librosa库的stft函数仅支持numpy类型计算,无法扩展到torch.tensor类型的数据计算。以下pytorch版本代码中,torch.stft可用于短时快速傅里叶变换,但需要设定window函数为hamming_window,因为其默认为hann_window。pad_mode需要修改为constant,不能使用其默认的reflect。经过不断试错排坑,终于找到与librosa相同计算结果的pytorch函数参数/(ㄒoㄒ)/~~。
# librosa D = librosa.stft(y, n_fft=n_fft, hop_length=hop_length,win_length=win_length, window="hamming") spect, phase = librosa.magphase(D) # pytorch D = torch.stft(y, n_fft=n_fft, hop_length=hop_length, win_length=win_length, window=torch.hamming_window(win_length),pad_mode='constant') spect = torch.norm(D, p=2, dim=-1).to(torch.float32)



