Skip to content

Instantly share code, notes, and snippets.

@fakufaku
Created December 22, 2022 14:12
Show Gist options
  • Save fakufaku/d395f5aa5e5c5fa07c7cbcf51d543413 to your computer and use it in GitHub Desktop.
Save fakufaku/d395f5aa5e5c5fa07c7cbcf51d543413 to your computer and use it in GitHub Desktop.
Matching the STFT of paderbox and torch
import torch
import paderbox
import numpy as np
from scipy.signal import hamming, blackman, get_window, hann
import matplotlib.pyplot as plt
f = 1500.0 # exactly periodic
fs = 48000.0
nfft = 512
hop = 128
demo = np.sin(2 * np.pi * f / fs * np.arange(fs))
# To make the comparison easier, we pad the input signal to
# make it a multiple of the FFT size
pad_size = nfft - hop
padding = np.zeros(pad_size)
demo = np.concatenate([demo, np.zeros(pad_size)])
# torch
# - use `center=False`
# - zero-pad the front of the signal
demo_lr = np.concatenate([padding, demo])
demo_lr = torch.from_numpy(demo_lr)
win_pt = torch.hamming_window(nfft, dtype=demo_lr.dtype)
PT = torch.stft(
demo_lr,
n_fft=nfft,
hop_length=hop,
window=win_pt,
return_complex=True,
pad_mode="constant",
center=False,
)
recon_pt = torch.istft(PT, n_fft=nfft, hop_length=hop, window=win_pt, center=False)
recon_pt = recon_pt[pad_size:]
recon_pt = recon_pt.numpy()
PT = PT.numpy()
print("torch: reconstruction exact ?", np.allclose(recon_pt, demo))
# paderbox
AR = paderbox.transform.stft(
demo_lr,
size=nfft,
shift=hop,
fading=False,
window=hamming,
# window=hamming_win,
# symmetric_window=True,
)
recon_ar = paderbox.transform.istft(
AR, size=nfft, shift=hop, fading=False, window=hamming
)
AR = AR.T
recon_ar = recon_ar[pad_size:]
print(
"pader: reconstruction exact ?",
np.allclose(recon_ar, demo),
)
print("difference between librosa's and pra's STFT", abs(PT - AR).max())
@fakufaku
Copy link
Author

fakufaku commented Dec 24, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment