Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confused by example in mel_to_stft doc #1672

Closed
lucala opened this issue Feb 18, 2023 · 1 comment · Fixed by #1673
Closed

Confused by example in mel_to_stft doc #1672

lucala opened this issue Feb 18, 2023 · 1 comment · Fixed by #1673
Labels
documentation Issues relating to docstrings, examples, and documentation build

Comments

@lucala
Copy link

lucala commented Feb 18, 2023

In the example for mel_to_stft we have the following code:

y, sr = librosa.load(librosa.ex('trumpet'))
S = np.abs(librosa.stft(y))
mel_spec = librosa.feature.melspectrogram(S=S, sr=sr)
S_inv = librosa.feature.inverse.mel_to_stft(mel_spec, sr=sr)

S is the magnitude, mel_spec is therefore a mel magnitude spectrogram (not mel power spectrogram). However, the code for librosa.feature.inverse.mel_to_stft does the following:

M : np.ndarray [shape=(..., n_mels, n), non-negative]
        The spectrogram as produced by `feature.melspectrogram`
inverse = nnls(mel_basis, M)
return np.power(inverse, 1.0 / power, out=inverse)

The power variable is not passed in the example and is therefore the default value power = 2.0. This would be correct when assuming a power spectrogram as input, which is not the case here.

My understanding is, that S_inv is now the approximate square root of S? However, in the example we proceed to compare the two assuming they should be equal, apart from the approximation error.

IMO we either need to convert magnitude into power with S = np.abs(librosa.stft(y))**2 or pass the power parameter in S_inv = librosa.feature.inverse.mel_to_stft(mel_spec, sr=sr, power=1).

@lostanlen lostanlen added the documentation Issues relating to docstrings, examples, and documentation build label Feb 18, 2023
@bmcfee
Copy link
Member

bmcfee commented Feb 18, 2023

Ah, excellent catch! You are correct that there is a power mismatch between the forward and inverse calls here. Fixing this does dramatically reduce the residual error. I'll PR a fix before the monday release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Issues relating to docstrings, examples, and documentation build
Development

Successfully merging a pull request may close this issue.

3 participants