Get Mel-bands from mel-spectrogram

469 views
Skip to first unread message

Joaquin Gonzalez

unread,
Nov 25, 2021, 3:29:17 PM11/25/21
to librosa
Hello everyone, 

I used librosa to calculate a melspectrogram, then I performed a calculation on the mel-spectrogram and I wanted to plot the results on the frequency time axis, so I need the frequencies corresponding to each mel-band. I didn’t find any way for librosa to return those values.
How can I calculate the frequencies corresponding to those mel bands?

Here's an extract of the code:

wav = wavfile.read(self.wav_fname)[1]

wav = wav.astype("float")

n_fft = 125

hop_length = 125

n_mels = 16

S = librosa.feature.melspectrogram(wav, sr=self.audio_sr, n_fft=n_fft, hop_length=hop_length, n_mels=n_mels)


Which gives:

Spectrograma16.png

I wanted to see the correlation between this spectrogram and another variable:

Sesion21_Sujeto1.png

The thing is I would like to know at what frequencies are found the maximum values in this plot, so i would need to know the frequencies [Hz] in each of the 16 mel-bands of the mel-spectrogram.

Would you know if I can get that information from the librosa.feature.melspectrogram() function, or if I can calculate it in some way?

Thank you!

Joaquin

Tiago Tavares

unread,
Nov 25, 2021, 3:32:05 PM11/25/21
to Joaquin Gonzalez, librosa
Hi Joaquin,

I am not sure there  is a better way, but I would use the mel filterbank (https://librosa.org/doc/main/generated/librosa.filters.mel.html) to get those bands.


Tiago


--
You received this message because you are subscribed to the Google Groups "librosa" group.
To unsubscribe from this group and stop receiving emails from it, send an email to librosa+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/librosa/e407ce40-cdac-4c82-91fa-b0e8a1d7e6f9n%40googlegroups.com.


--

Vincent Lostanlen

unread,
Nov 25, 2021, 3:52:41 PM11/25/21
to lib...@googlegroups.com

Hello Joaquin and Tiago,

There is a better way :)


Use the function librosa.mel_frequencies

https://librosa.org/doc/main/generated/librosa.mel_frequencies.html

(which is called internally by librosa.filters.mel)

and you will get the frequencies in Hertz.


You can pass the same hyperparameters to librosa.mel_frequencies as those you passed to librosa.melspectrogram


I hope this helps!


Sincerely,

Vincent

Joaquin Gonzalez

unread,
Nov 26, 2021, 9:54:20 AM11/26/21
to librosa
Hi Tiago and Vincent, 

First of all, thank you very much for your help!

Concerning the mel filterbank, I am not sure what the  returned matrix of  dimensions (n_mels, nfft/2 +1), or how could I extrac the frequencies in Hz from it.

The function librosa.mel_frequencies seems just right. I set fmax = 8000 because of the nysquit frequency of the audio samples and it works! I have one doubt left about it. 

The function returns 16 values, and I was expecting 16 bands (with it low cut and high cut frequencies). Are those 16 values the bands limits? Should I run it with n_mels = 17 in order to get those 16 bands?

Thanks again!

Joaquin

Vincent Lostanlen

unread,
Nov 26, 2021, 10:33:33 AM11/26/21
to lib...@googlegroups.com

Hello Joaquin,

> Are those 16 values the bands limits? Should I run it with n_mels = 17 in order to get those 16 bands?

These 16 values are the center frequencies of the bands.

If you want the lower limits, do the following:

> mel_f = mel_frequencies(n_mels + 2, fmin=fmin, fmax=fmax, htk=htk)
> low = mel_f[:-2]
> center = mel_f[1:-1]
> high = mel_f[2:]

Note the +2 offset to account for the boundaries. This +2 offset is present in the source code of librosa.filters.mel
https://librosa.org/doc/main/_modules/librosa/filters.html#mel


I hope this helps!

Vincent
Reply all
Reply to author
Forward
0 new messages