python wav文件短时能量并plot出来

版权声明:https://blog.csdn.net/barry_j https://blog.csdn.net/Barry_J/article/details/84177006
import wave
import pyaudio
import numpy as np
import pylab

import pylab as pl

# 计算每一帧的能量 256个采样点为一帧
def calEnergy(wave_data) :
    energy = []
    sum = 0
    for i in range(len(wave_data)) :
        sum = sum + (int(wave_data[i]) * int(wave_data[i]))
        if (i + 1) % 256 == 0 :
            energy.append(sum)
            sum = 0
        elif i == len(wave_data) - 1 :
            energy.append(sum)
    return energy





# f = wave.open("./语料/" + str(i + 1) + ".wav","rb")

f=wave.open("C:/Users/Administrator/Desktop/du.wav","rb")
# getparams() 一次性返回所有的WAV文件的格式信息
params = f.getparams()
# nframes 采样点数目
nchannels, sampwidth, framerate, nframes = params[:4]
# readframes() 按照采样点读取数据
str_data = f.readframes(nframes)            # str_data 是二进制字符串

# 以上可以直接写成 str_data = f.readframes(f.getnframes())

# 转成二字节数组形式(每个采样点占两个字节)
wave_data = np.fromstring(str_data, dtype = np.short)
print( "采样点数目:" + str(len(wave_data)))          #输出应为采样点数目
f.close()



energy = calEnergy(wave_data)


time = np.arange(0, len(wave_data)) * (1.0 / framerate)
time2 = np.arange(0, len(energy)) * (len(wave_data)/len(energy) / framerate)
pl.subplot(211)
pl.plot(time, wave_data)
pl.ylabel("Amplitude")
pl.subplot(212)
pl.plot(time2, energy)
pl.ylabel("short energy")
pl.xlabel("time (seconds)")
pl.show()


# print("短时能量:",energy)

猜你喜欢

转载自blog.csdn.net/Barry_J/article/details/84177006