Skip to content

Instantly share code, notes, and snippets.

@previtus
Last active October 23, 2019 18:45
Show Gist options
  • Save previtus/7afcb81baa40cc3c50390a39210d7d30 to your computer and use it in GitHub Desktop.
Save previtus/7afcb81baa40cc3c50390a39210d7d30 to your computer and use it in GitHub Desktop.
Tesing LWS
import librosa
import numpy as np
# Variables setup:
fft_size=2048
window_size=2048 # window size
hop_size=512 # window shift size
sample_rate=44100
lws_L = 5
# Loading audio file (and getting some nice sounding part)
file = "MacLeodMonkeys.wav"
audio, sample_rate = librosa.load(file, sr=sample_rate, mono=True)
print("Input audio:", np.asarray(audio).shape)
# select a small part from the song
skip = window_size * 200
audio = audio[skip:531968+skip]
print("Input audio:", np.asarray(audio).shape)
x = audio
# Now this code follows the example from https://github.com/Jonathan-LeRoux/lws/tree/master/python
import lws
import numpy as np
lws_processor = lws.lws(window_size, hop_size, L=lws_L, fftsize=fft_size, mode="music")
#lws_processor=lws.lws(512,128, mode="speech") # 512: window length; 128: window shift
X = lws_processor.stft(x) # where x is a single-channel waveform
X0 = np.abs(X) # Magnitude spectrogram
print('{:6}: {:5.2f} dB'.format('Abs(X)', lws_processor.get_consistency(X0)))
X1 = lws_processor.run_lws(X0) # reconstruction from magnitude (in general, one can reconstruct from an initial complex spectrogram)
print('{:6}: {:5.2f} dB'.format('LWS', lws_processor.get_consistency(X1)))
represented = X1
# now reconstruct from:
print("\trepresented shape", represented.shape)
reconstruction = lws_processor.istft(represented) # where x is a single-channel waveform
reconstruction = np.asarray(reconstruction)
print("\toutput reconstruction:", reconstruction.shape) # (531968,)
print("\treconstruction data type:", reconstruction.dtype)
import scipy.io.wavfile
# sanity check + saved results
scipy.io.wavfile.write("original.wav", sample_rate, audio)
scipy.io.wavfile.write("reconstruction_lws.wav", sample_rate, reconstruction)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment