wav文件的幅度计算

1 投票
1 回答
2616 浏览
提问于 2025-04-17 21:28

我正在玩弄如何在Python中读取正弦波并进行一些计算。不过,我在想,numpy里定义的数据类型会不会造成什么问题。我的主要目标是读取一个.wav文件,并找到样本的振幅。我不太想使用像sax或ffmpeg这样的命令行工具。

f = wave.open('sine.wav','rb') #3 second long sine wav

nchannels, sampwidth, framerate, nframes, comptype, compname = f.getparams()[:6]

if sampwidth != 2:
    raise ValueError("Only supports 16 bit audio formats")

if nchannels == 2:
    nframes*=2 #this seems to give me all data when I read in a 2-channel wave

byteList = np.fromstring(f.readframes(nframes), dtype = np.int16)

f.close()

byteList.astype(float) #attempt to change type to perform the following operations

maximum = max(byteList)
minimum = min(byteList)
peak = (abs(maximum)+abs(minimum))/2) #find a good max amplitude.  This fails 
    #RuntimeWarning: overflow encountered in short_scalars.  I thought I changed type! 

#I check to see the indices where the max amplitude occurs.  I get no results.
for i in byteList[0:nframes]:
    if peak <= (byteList[i]):
        print('These are the indices where the maximum occurs: {}'.format(i))

#Find the rms value.  This gets me .7344... Close, I guess.
total = 0
for i in byteList[0:nframes]:
    total+=(((byteList[i])/peak))**2
rms = math.sqrt(total/nframes)
print('This is rms: {}'.format(rms))


#Here I tree to find the max amplitude every second.  I get an empy list.  
i = 0
j = 1
amp_list = [0] #default max
while (i < nframes):
    for i in byteList[i:j*framerate]:
        if byteList[i+1] >= byteList[i]:
            amp_list.pop()
            amp_list.append(byteList[i+1])
    j+=1
    i+=framerate           

1 个回答

1

默认情况下,astype 这个方法不会直接在原来的数组上修改,而是会返回一个新的数组。如果你想要在原数组上进行修改,可以使用:

byteList = byteList.astype(np.float)

在某些情况下,当你设置关键字参数 copy=True 时,astype 可以直接在原数组上修改(具体可以查看文档),但即使这样做,它还是会返回一个数组,所以你可以用上面提到的方式。

撰写回答