简体   繁体   English

librosa melspectrogram y 轴刻度错误?

[英]librosa melspectrogram y-axis scale wrong?

I'm trying to figure out why Mel scale spectrogram seems to have the wrong frequency scale.我试图弄清楚为什么梅尔比例谱图似乎有错误的频率比例。 I generate a 4096Hz tone and plot it using librosa's display library, and the tone does not align with the known frequency?我使用 librosa 的显示库生成了 4096Hz 的音调和 plot,但音调与已知频率不一致? I'm obviously doing something wrong, can someone help?我显然做错了什么,有人可以帮忙吗? Thanks!谢谢!

import numpy as np
import librosa.display
import matplotlib.pyplot as plt

sr = 44100
t = np.linspace(0, 1, sr)
y = 0.1 * np.sin(2 * np.pi * 4096 * t)

M = librosa.feature.melspectrogram(y=y, sr=sr)
M_db = librosa.power_to_db(M, ref=np.max)
librosa.display.specshow(M_db, y_axis='mel', x_axis='time')
plt.show()

When you compute the mel spectrogram using librosa.feature.melspectrogram(y=y, sr=sr) you implicitly create a mel filter using the parameters fmin=0 and fmax=sr/2 (see docs here ).当您使用librosa.feature.melspectrogram(y=y, sr=sr)计算 mel 频谱图时,您会使用参数fmin=0fmax=sr/2隐式创建一个 mel 滤波器(请参阅此处的文档)。 To correctly plot the spectrogram, librosa.display.specshow needs to know how it was created, ie what sample rate sr was used (to get the time axis right) and what frequency range was used to get the frequency axis right.为了正确 plot 频谱图, librosa.display.specshow需要知道它是如何创建的,即使用什么采样率sr (使时间轴正确)以及使用什么频率范围使频率轴正确。 While librosa.feature.melspectrogram defaults to 0 - sr/2 , librosa.display.specshow unfortunately defaults to 0 - 11050 (see here ).虽然librosa.feature.melspectrogram默认为0 - sr/2 ,但不幸的是librosa.display.specshow默认为0 - 11050 (参见此处)。 This describes librosa 0.8—I could imagine this changes in the future.这描述了 librosa 0.8——我可以想象未来会发生这种变化。

To get this to work correctly, explicitly add fmax parameters.要使其正常工作,请显式添加fmax参数。 To also get the time axis right, add the sr parameter to librosa.display.specshow :要同时获得正确的时间轴,请将sr参数添加到librosa.display.specshow

import numpy as np
import librosa.display
import matplotlib.pyplot as plt

sr = 44100
t = np.linspace(0, 1, sr)
y = 0.1 * np.sin(2 * np.pi * 4096 * t)

M = librosa.feature.melspectrogram(y=y, sr=sr, fmax=sr/2)
M_db = librosa.power_to_db(M, ref=np.max)
librosa.display.specshow(M_db, sr=sr, y_axis='mel', x_axis='time', fmax=sr/2)
plt.show()

正确的梅尔谱图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM