Skip to content

Instantly share code, notes, and snippets.

@fakufaku
Created December 19, 2022 09:01
Show Gist options
  • Save fakufaku/b5f03abcd9c6b4f2e4b89a003b41c1b5 to your computer and use it in GitHub Desktop.
Save fakufaku/b5f03abcd9c6b4f2e4b89a003b41c1b5 to your computer and use it in GitHub Desktop.
Matching librosa and pyroomacoustics STFT
from librosa import stft, istft
import pyroomacoustics as pra
import numpy as np
f = 1500.0
fs = 48000.0
nfft = 512
hop = 128
demo = np.sin(2 * np.pi * f / fs * np.arange(fs))
demo = np.random.normal(0, 1, 48000)
# 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(128)])
# librosa
# - use `center=False`
# - zero-pad the front of the signal
demo_lr = np.concatenate([padding, demo])
LR = stft(demo_lr, n_fft=nfft, window=pra.hann, center=False)
recon_lr = istft(LR, window=pra.hann, center=False)
print("librosa: reconstruction exact ?", np.allclose(recon_lr, demo_lr))
# pyroomacoustics
# 1) pra requires to define the analysis (win_a) and synthesis (win_s)
# in advance.
win_a = pra.hann(nfft)
win_s = pra.transform.stft.compute_synthesis_window(win_a, hop)
AR = pra.transform.stft.analysis(demo, nfft, hop=hop, win=win_a, zp_back=0, zp_front=0)
recon_ar = pra.transform.stft.synthesis(AR, nfft, hop=hop, win=win_s)
# 2) pra does not remove the zeros added in the forward STFT when doing the
# inverse STFT
recon_ar = recon_ar[pad_size:]
print(
"pra: reconstruction exact ?",
np.allclose(recon_ar, demo[:-pad_size]),
)
print("difference between librosa's and pra's STFT", abs(LR.T - AR).max())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment