winsize=256;%窗长
n=0.1;%噪声水平
a=2;
b=6;
[speech,fs,nbits]=wavread('E:\matlab\louyin.wav');%读入wav文件
size=length(speech);%语音长度
numofwin=floor(size/winsize);%窗数
%定义汉明窗
ham=hamming(winsize)';
hamwin=zeros(1,size);
enhanced=zeros(1,size);
improved=zeros(1,size);
%生成噪声信号
noise=n*randn(1,size);
y=speech'+noise;
%噪声处理
noisy=n*randn(1,winsize);
N=fft(noisy);
npow=abs(N);
for q=1:2*numofwin-1
yframe=y(1+(q-1)*winsize/2:winsize+(q-1)*winsize/2);%分帧
hamwin(1+(q-1)*winsize/2:winsize+(q-1)*winsize/2)=hamwin(1+(q-1)*winsize/2:winsize+(q-1)*winsize/2)+ham;%
%加噪信号FFT
y1=fft(yframe.*ham);
ypow=abs(y1);%加噪信号幅度
yangle=angle(y1);%相位
%计算功率谱密度
Py=ypow.^2;
Pn=npow.^2;
Pyy=ypow.^a;
Pnn=npow.^a;
%基本谱减
for i=1:winsize
if Py(i)-Pn(i)>0
Ps(i)=Py(i)-Pn(i);
else
Ps(i)=0;
end
end
s=sqrt(Ps).*exp(j*yangle);
for i=1:winsize
if Pyy(i)-b*Pnn(i)>0
Pss(i)=Pyy(i)-b*Pnn(i);
else
Pss(i)=0;
end
end
ss=Pss.^(1/a).*exp(j*yangle);
%去噪语音IFFT
enhanced(1+(q-1)*winsize/2:winsize+(q-1)*winsize/2)=enhanced(1+(q-1)*winsize/2:winsize+(q-1)*winsize/2)+real(ifft(s));
improved(1+(q-1)*winsize/2:winsize+(q-1)*winsize/2)=improved(1+(q-1)*winsize/2:winsize+(q-1)*winsize/2)+real(ifft(ss));
end
%去除汉明窗引起的增益
for i=1:size
if hamwin(i)==0
enhanced(i)=0;
improved(i)=0;
else
enhanced(i)=enhanced(i)/hamwin(i);
improved(i)=improved(i)/hamwin(i);
end
end
SNR1=10*log10(var(speech')/var(noisy));%加噪语音信噪比
SNR2=10*log10(var(speech')/var(enhanced-speech'));%增强语音信噪比
SNR3=10*log10(var(speech')/var(improved-speech'));
figure(1);plot(speech');%原始语音波形
title(['Original Voice(n=',num2str(n),')']);
figure(2);plot(y);
title(['Noise Added(SNR=',num2str(SNR1),'dB)']);
figure(3);plot(enhanced);
title(['Enhanced Voice(SNR=',num2str(SNR2),'dB)']);
figure(4);plot(improved);
title(['Improved Voice(SNR=',num2str(SNR3),'dB)']);