将3字节立体声WAV文件转换为numpy数组 [英] Convert 3-byte stereo WAV-file to numpy array
问题描述
我得到了一个很大的连续水下记录的WAV文件,我想将其转换为一个numpy数组进行分析.我正在努力做到这一点.
I have been given a large WAV-file of continuous underwater recording which I would like to convert to a numpy array for analysis. I am struggling to do this.
到目前为止,我有:
import numpy as np
import scipy as sp
import wave as wv
import struct
wavefile = wv.open(filename,'r')
(nchannels,sampwidth,framerate,nframes,comptype,compname) = wavefile.getparams()
// read a sample as example
wavedata =wavefile.readframes(1)
第一帧如下:"\ xcd \ xbc \ xff @ \ x01 \ x00".我尝试使用struct对其进行解压缩,但是无论如何解压缩我都会收到以下错误:" str大小与格式不匹配".我想这与Python结构无法处理24位数据有关.
The first frame looks like this: '\xcd\xbc\xff@\x01\x00'. I have tried to unpack it using struct but unpack whatever I do I get the following error: "str size does not match format". I guess this is related to the fact that Python struct cannot handle 24-bit data.
波形文件的参数如下:
- nchannels = 2
- sampwidth = 3
- framerate = 48000
- nframes = 283516532L
- comptype ='NONE'
- compname ='未压缩'
有人知道如何将24位立体声WAV文件读取到numpy数组中吗?
Someone know hows to read a 24-bit stereo WAV-file into a numpy array?
推荐答案
对于那些有类似问题的人,我发布了我的解决方案.请注意,这会将24位wave文件转换为带符号的浮点numpy数组.仅当转换为整数时,将/int2float部分保留.
For those with similar issues I post my solution. Note that this converts a 24-bit wave file into a signed floating point numpy array. Leave the /int2float part out when only converting to integers.
frames = wavfile.readframes(nsamples)
ch1 = np.zeros(nsamples)
ch2 = np.zeros(nsamples)
int2float = (2**23)-1
for x in np.arange(int(nsamples)):
ch1_24bit_sample = frames[x*6:x*6+3]
ch2_24bit_sample = frames[x*6+3:x*6+6]
ch1_32bit_sample = bit24_2_32(ch1_24bit_sample)
ch2_32bit_sample = bit24_2_32(ch2_24bit_sample)
ch1[x]=struct.unpack('i',ch_32bit_sample)[0]
ch2[x]=struct.unpack('i',ch_32bit_sample)[0]
ch1[x]=ch1[x]/int2float
ch2[x]=ch2[x]/int2float
def bit24_2_32(strbytes):
if strbytes[2] < '\x80':
return strbytes+'\x00'
else:
return strbytes+'\xff'
这篇关于将3字节立体声WAV文件转换为numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!