从 .wav 文件中提取数据

extract data from .wav file

我有 this 包含 ECG 信号的 .wav 文件,我想使用 scipy.io.wavfile.read('sig100.wav') 提取该文件中的数据 但是我收到了这个错误

"has {}-bit data.".format(bit_depth))

ValueError: Unsupported bit depth: the wav file has 11-bit data.

当我对此进行一些研究时,我发现该函数只接受 8 位深度文件,但我不知道如何修改它以接受我在 this 上找到的文件Whosebug 但没有得到它

根据 scipy.io.wavfile source code ,它接受 (8, 16, 32, 64, 96, 128) 位数据。

虽然您可以修改 wavfile 源代码以接受数据,但更简单的替代方法是使用 pydub 等外部库。请参阅 API 和安装详细信息 here.

首先,我们获取您的文件,将比特率转换为 16 位并导出。
然后,只需使用 scipy 导入修改后的 wav 文件即可获取数据和帧率。

from scipy.io import wavfile
from pydub import AudioSegment

audio = "sig100.wav"
audio1 = "sig100_16.wav"

#read wav file and export with 16bit bitrate
s = AudioSegment.from_file(audio, format = "wav" )
s.export(audio1 , bitrate="16", format="wav")

#read modified file
rate, data = wavfile.read(audio1)

结果:

>>> rate
360
>>> data
array([[ -928,  -416],
       [ -928,  -416],
       [ -928,  -416],
       ...,
       [-4320, -2336],
       [-4896, -2144],
       [-8192,     0]], dtype=int16)
>>> 

希望这对您有所帮助。