Librosa – Audio Spectrogram/Frequency Bins to Spectrum

Question:

I’ve read around for several days but haven’t been to find a solution… I’m able to build Librosa spectrograms and extract amplitude/frequency data using the following:

audio, sr = librosa.load('short_piano melody_keyCmin_110bpm.wav', sr = 22500)
spectrum = librosa.stft(audio, n_fft=2048, window=scipy.signal.windows.hamming)
D = librosa.amplitude_to_db(np.abs(spectrum), ref=np.max)
n = D.shape[0]
Nfft = 1+2*(n-1)
freq_bins = librosa.fft_frequencies(sr=sr, n_fft=Nfft)

However, I cannot turn the data in D and freq_bins back into a spectrum. Once I am able to do this I can convert the new spectrum into a .wav file and listen to my reconstructed audio… Any advice would be appreciated! Thank you.

Asked By: A T

||

Answers:

When I get your question right, you want to reconstruct the real/imaginary spectrum from your magnitude values. You will need the phase component for that, then its all simple complex number arithmetic. You should be aware that the output of an STFT is an array of complex numbers, and the amplitude is the absulute value of each number, while the phase is the angle of each number

Here´s an example of a time-domain signal transformed to magnitude/phase and back without modifying it:

% get the complex valued spectrum from a sample
spectrum = librosa.stft(audio, n_fft=2048,window=scipy.signal.windows.hamming) 

# get magnitude and phase from the complex numbers
magnitude = np.abs(spectrum)
phase = np.angle(spectrum)

# reconstruct real/imaginary parts from magnitude and phase
spectrum = magnitude * np.exp(1j*phase)

# transform back to time-domain

In your case, you should first convert the db-values back to amplitude values, of course. Even having no experience with librosa, I´m sure that there is also a function for that.

Answered By: dCb

Can someone answer what the frequency_bin only axis indicate? What values are they and how us it calculated from frequency?

Answered By: Ashwini Dasare
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.