Created
July 12, 2017 14:47
-
-
Save standy66/2047715897d1d7fcb309ae2d678fd1a1 to your computer and use it in GitHub Desktop.
A small snippet that helps reconstruct audio from spectrogram in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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