音量
發(fā)布日期:2022/10/23 10:38:51 瀏覽量:
音量
音量代表聲音的強(qiáng)度,可由一個(gè)窗口或一幀內(nèi)信號(hào)振幅的大小來衡量,一般有兩種度量方法:
(1)每個(gè)幀的振幅的絕對(duì)值的總和:

其中為該幀的第i個(gè)采樣點(diǎn),n為該幀總的采樣點(diǎn)數(shù)。這種度量方法的計(jì)算量小,但不太符合人的聽覺感受。
(2)樣本平方和取10為底的對(duì)數(shù)的10倍

它的單位是分貝(Decibels),是一個(gè)對(duì)數(shù)強(qiáng)度值,比較符合人耳對(duì)聲音大小的感覺,但計(jì)算量稍復(fù)雜。
音量計(jì)算的Python實(shí)現(xiàn)如下:
import math
import numpy as np
# method 1: absSum
def calVolume(waveData, frameSize, overLap):
wlen = len(waveData)
step = frameSize - overLap
frameNum = int(math.ceil(wlen*1.0/step))
volume = np.zeros((frameNum,1))
for i in range(frameNum):
curFrame = waveData[np.arange(i*step,min(i*step+frameSize,wlen))]
curFrame = curFrame - np.median(curFrame) # zero-justified
volume[i] = np.sum(np.abs(curFrame))
return volume
# method 2: 10 times log10 of square sum
def calVolumeDB(waveData, frameSize, overLap):
wlen = len(waveData)
step = frameSize - overLap
frameNum = int(math.ceil(wlen*1.0/step))
volume = 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) # zero-justified
volume[i] = 10*np.log10(np.sum(curFrame*curFrame))
return volume
#--------main.py-------
import wave
import pylab as pl
import numpy as np
import volume as vp
# ============ test the algorithm =============
# read wave file and get parameters.
fw = wave.open(’aeiou.wav’,’r’)
params = fw.getparams()
print(params)
nchannels, sampwidth, framerate, nframes = params[:4]
strData = fw.readframes(nframes)
waveData = np.fromstring(strData, dtype=np.int16)
waveData = waveData*1.0/max(abs(waveData)) # normalization
fw.close()
# calculate volume
frameSize = 256
overLap = 128
volume11 = vp.calVolume(waveData,frameSize,overLap)
volume12 = vp.calVolumeDB(waveData,frameSize,overLap)
# plot the wave
# 計(jì)算時(shí)間軸的長(zhǎng)度
time = np.arange(0, nframes)*(1.0/framerate)
time2 = np.arange(0, len(volume11))*(frameSize-overLap)*1.0/framerate
pl.subplot(311)
pl.plot(time, waveData)
pl.ylabel("Amplitude")
pl.subplot(312)
pl.plot(time2, volume11)
pl.ylabel("absSum")
pl.subplot(313)
pl.plot(time2, volume12, c="g")
pl.ylabel("Decibel(dB)")
pl.xlabel("time (seconds)")
pl.show()
馬上咨詢: 如果您有業(yè)務(wù)方面的問題或者需求,歡迎您咨詢!我們帶來的不僅僅是技術(shù),還有行業(yè)經(jīng)驗(yàn)積累。
QQ: 39764417/308460098 Phone: 13 9800 1 9844 / 135 6887 9550 聯(lián)系人:石先生/雷先生