scipy.io.wavfile.read 返回的数据 [英] Data returned by scipy.io.wavfile.read

查看:35
本文介绍了scipy.io.wavfile.read 返回的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Python 中的 wav 文件中获取数据并绘制它.当我使用 scipy.io.wavfile.read() 时,我得到一个如下所示的数组:

I'm trying to get the data from a wav file in Python and plot it. When I use scipy.io.wavfile.read(), I get back an array that looks like this:

[[ -1.49836736e-02  -1.27559584e-02]
 [ -1.84625713e-02  -1.63264061e-02]
 [ -2.17888858e-02  -1.95001373e-02]
 ..., 
 [  6.10332937e-05   6.10332937e-05]
 [ -3.05166468e-05   0.00000000e+00]
 [  3.05166468e-05  -6.10332937e-05]]

为什么它是一堆长度为 2 的数组,而不是一个带有每个样本值的长数组?返回的这些数据代表什么?提前致谢.

Why is it a bunch of arrays with length 2 as opposed to one long array with the value at each sample? What does this data being returned represent? Thanks in advance.

convert_16_bit = float(2**15)
sr, samples = scipy.io.wavfile.read('singingonenote.wav')
x = np.linspace(0, 2000, 0.01)
samples = samples / (convert_16_bit + 1.0)
y = samples
print samples
plt.plot(x, y)
plt.show()

推荐答案

您正在阅读的文件似乎是立体声文件.这些包含二维数据 - 左声道和右扬声器的声道.

The file you are reading seems to be a stereo file. These contain two-dimensional data - one track for the left and one track for the right speaker.

此处解释了一般概念:https://en.wikipedia.org/wiki/Stereophonic_sound

如果你只想从二维数据序列中选择左声道,你可以像这样选择

If you want to select just the left audio channel from your two-dimensional data sequence, you can select it like

y = samples[:,0]

要选择正确的频道,请将 0 替换为 1.

To select the right channel, replace the 0 with a 1.

作为替代方案,请确保您用于生成文件的程序首先保存单声道文件.根据您尝试执行的操作,这可能是实际的错误.

As an alternative, make sure that the program you use to generate the file saves mono wave files in the first place. Depending on what you are trying to do, this might be the actual bug.

这篇关于scipy.io.wavfile.read 返回的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆