python音频分析

  • Post author:
  • Post category:python




标题

# import librosa as lr
import wave
import numpy as np
import math
import matplotlib.pyplot as plt
def ZeroCR(waveData,frameSize,overLap):
    wlen=len(waveData)
    step=frameSize-overLap
    frameNum=math.ceil(wlen/step)
    zcr=np.zeros((frameNum,1))
    for i in range(frameNum):
        curFrame=waveData[np.arange(i*step,min(i*step+frameSize,wlen))]
        curFrame=curFrame-np.mean(curFrame)
        zcr[i]=sum(curFrame[0:-1]*curFrame[1::]<=0)
    return zcr
audio_path=r"D:\User\Dashujv\语音分析\data\海草舞.mp3"
# x,sr=lr.load(audio_path)
# 
# """
# <class 'numpy.ndarray'> <class 'int'>
# (4413912,) 22050
# """
# lr.load(audio_path,sr=44100)

# import librosa.display
# plt.figure(figsize=(14,5))
# librosa.display.waveplot(x,sr=sr)
# plt.show()
fw=wave.open(audio_path,"rb")
params=fw.getparams()
print(params)
nchannels,sampwidth,framerate,nframes=params[:4]
str_data=fw.readframes(nframes)
wave_data=np.frombuffer(str_data,dtype=np.short)
wave_data.shape=-1,1
fw.close()
#计算过零率
frameSize=256
overLap=0
zcr=ZeroCR(wave_data,frameSize,overLap)
#可视化结果
time=np.arange(0,len(wave_data))*(1.0/framerate)
time2=np.arange(0,len(zcr))*(len(wave_data)/len(zcr)/framerate)
plt.figure(figsize=(14,10))
plt.subplot(211)
plt.plot(time,wave_data)
plt.ylabel("Amplitude")
plt.subplot(212)
plt.plot(time2,zcr)
plt.ylabel("ZCR")
plt.xlabel("time (seconds)")
plt.show()

在这里插入图片描述



版权声明:本文为qestion_yz_10086原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。