为什么librosa plot不同于matplotlib和audacity

2024-05-20 19:54:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我从一个文件中读取pcm数据,然后打印出来。我注意到情节各不相同librosa.display.waveplot文件阴谋和大胆。你知道吗

这是代码和图片

%matplotlib inline
import matplotlib.pyplot as plt
import librosa.display
import numpy as np
import IPython.display as ipd
import matplotlib.pyplot as plt
import numpy, pylab

# the pcm file is 32le integer with a sampling rate of 16KHz
pcm_data = np.fromfile('someaudio.pcm', dtype=np.int32)

# the sample has the same sound as audacity
ipd.Audio(data=pcm_data, rate=16000) 

# all of these give the same resulting plot
plt.figure()
plt.subplot(3, 1, 1)
#librosa.display.waveplot(pcm_data, sr=16000)
#librosa.display.waveplot(pcm_data.astype('double'), sr=16000)
librosa.display.waveplot(pcm_data.astype('float'), max_points=None, sr=16000, max_sr=16000)

这个结果看起来像 enter image description here

# alternatively plot via matplotlib
pylab.plot(pcm_data)
pylab.show()

这个结果看起来像 enter image description here

matplotlib的结果看起来很大胆 enter image description here


Tags: 文件theimportdataplotmatplotlibasdisplay
2条回答

我通过librosa论坛得到了一些答案。Brian McFee给出了一个答案:

根据Vincent发布的内容,librosa的波形图没有直接显示样本,原因有二:

  • 它会通过将点保持在较高的位置来提高内存使用率 分辨率超过了可视化所需的分辨率

  • 它可以被高频噪声所掩盖。

相反,librosa的绘图仪工作起来更像是一个典型的DAW,在这里音频信号被下采样以达到viz的目的,包络被可视化而不是信号本身。这些步骤是通过显示最大值(abs(y[i:i+k]),而不是样本y[i]、y[i+1]、。。。是[i+k]。下采样窗口的长度由波形图的参数控制。你知道吗

由于上下轴的信息通过abs被丢弃,我们使用轴来分离立体声信号中的左右声道(左上,右下)。在单声道信号中,包络线在y轴上反射,从而产生您报告的对称图形。你知道吗

不同的daw执行这些步骤的方式略有不同,一旦您放大到某个范围,这样做就变得可行,一个奇特的实现将还原为样例打印。Matplotlib并不能使这一点很容易实现,所以我们选择了这个折衷方案。如果您想要样品精确的绘图,我们建议使用pyplot.plt公司()取而代之。你知道吗

matplotlibAudacity显示了实际的信号样本,显然在记录的后半部分都是负数。你知道吗

另一方面,librosa显示了绝对信号的包络,如其documentation中所述:

Plot the amplitude envelope of a waveform.

If y is monophonic, a filled curve is drawn between [-abs(y), abs(y)].

y是这种情况下的信号。你知道吗

这有效地导致沿x轴的镜像效应,这就是librosa图对称的原因。matplotlib和Audacity显然没有这样的行为。你知道吗

有人可能会说,librosa的行为有效地隐藏了不对称波形(即正负样本的振幅不相似),这在野外是可能的。从soundonsound.com

This asymmetry is due mainly to two things, the first being the relative phase relationships between the fundamental and different harmonic components in a harmonically complex signal. In combining different frequency signals with differing phase relationships, the result is often a distinctly asymmetrical waveform, and that waveform asymmetry often changes and evolves over time, too. That's just what happens when complex related signals are superimposed.

也有人可能会说,不对称中没有很多有用的信息,因为人类通常无法感知。你知道吗

如果你认为librosa的行为出乎意料或是错误的,我建议你填写一份bug报告,要求解释。你知道吗

相关问题 更多 >