Python谱减法语音降噪实例
代码中用到了nextpow2,其中n=nextpow2(x)表示最接近x的2的n次幂。
#!/usr/bin/envpython
importnumpyasnp
importwave
importnextpow2
importmath
#打开WAV文档
f=wave.open("filename.wav")
#读取格式信息
#(nchannels,sampwidth,framerate,nframes,comptype,compname)
params=f.getparams()
nchannels,sampwidth,framerate,nframes=params[:4]
fs=framerate
#读取波形数据
str_data=f.readframes(nframes)
f.close()
#将波形数据转换为数组
x=np.fromstring(str_data,dtype=np.short)
#计算参数
len_=20*fs//1000
PERC=50
len1=len_*PERC//100
len2=len_-len1
#设置默认参数
Thres=3
Expnt=2.0
beta=0.002
G=0.9
#初始化汉明窗
win=np.hamming(len_)
#normalizationgainforoverlap+addwith50%overlap
winGain=len2/sum(win)
#Noisemagnitudecalculations-assumingthatthefirst5framesisnoise/silence
nFFT=2*2**(nextpow2.nextpow2(len_))
noise_mean=np.zeros(nFFT)
j=0
forkinrange(1,6):
noise_mean=noise_mean+abs(np.fft.fft(win*x[j:j+len_],nFFT))
j=j+len_
noise_mu=noise_mean/5
#---allocatememoryandinitializevariousvariables
k=1
img=1j
x_old=np.zeros(len1)
Nframes=len(x)//len2-1
xfinal=np.zeros(Nframes*len2)
#=========================StartProcessing===============================
forninrange(0,Nframes):
#Windowing
insign=win*x[k-1:k+len_-1]
#computefouriertransformofaframe
spec=np.fft.fft(insign,nFFT)
#computethemagnitude
sig=abs(spec)
#savethenoisyphaseinformation
theta=np.angle(spec)
SNRseg=10*np.log10(np.linalg.norm(sig,2)**2/np.linalg.norm(noise_mu,2)**2)
defberouti(SNR):
if-5.0<=SNR<=20.0:
a=4-SNR*3/20
else:
ifSNR<-5.0:
a=5
ifSNR>20:
a=1
returna
defberouti1(SNR):
if-5.0<=SNR<=20.0:
a=3-SNR*2/20
else:
ifSNR<-5.0:
a=4
ifSNR>20:
a=1
returna
ifExpnt==1.0:#幅度谱
alpha=berouti1(SNRseg)
else:#功率谱
alpha=berouti(SNRseg)
#############
sub_speech=sig**Expnt-alpha*noise_mu**Expnt;
#当纯净信号小于噪声信号的功率时
diffw=sub_speech-beta*noise_mu**Expnt
#betanegativecomponents
deffind_index(x_list):
index_list=[]
foriinrange(len(x_list)):
ifx_list[i]<0:
index_list.append(i)
returnindex_list
z=find_index(diffw)
iflen(z)>0:
#用估计出来的噪声信号表示下限值
sub_speech[z]=beta*noise_mu[z]**Expnt
#---implementasimpleVADdetector--------------
ifSNRseg
以上这篇Python谱减法语音降噪实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。