Skip to content

Instantly share code, notes, and snippets.

@standy66
Created July 12, 2017 14:47
Show Gist options
  • Save standy66/2047715897d1d7fcb309ae2d678fd1a1 to your computer and use it in GitHub Desktop.
Save standy66/2047715897d1d7fcb309ae2d678fd1a1 to your computer and use it in GitHub Desktop.
A small snippet that helps reconstruct audio from spectrogram in Python
from scipy.signal import stft, istft
def reconstruct(spectrogram, sample_rate, nperseg, iters=100):
length = istft(spectrogram, sample_rate, nperseg=nperseg)[1].shape[0]
x = np.random.normal(size=length)
for i in range(iters):
# Code based on the answer here: https://dsp.stackexchange.com/a/3410
X = stft(x, sample_rate, nperseg=nperseg)[2]
Z = spectrogram * np.exp(np.angle(X) * 1j)
x = istft(Z, sample_rate, nperseg=nperseg)[1]
return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment