wav文件幅度计算

时间:2014-03-09 00:44:00

标签: python audio numpy

我正在乱读正弦波并在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 个答案:

答案 0 :(得分:1)

默认情况下,astype不是就地完成的,而是使用:

byteList = byteList.astype(np.float)

在某些情况下,astype可以在关键字copy=True时就地完成(请参阅文档),但即使在就地完成,它也会返回数组,因此上面的表单可以是使用