winsize=256;%窗长
a=4;b=6;
%定义汉明窗
ham=hamming(winsize)';
hamwin=zeros(1,size);
enhanced=zeros(1,size);
improved=zeros(1,size);
%读入原始语音文件
[speech,fs,nbits]=wavread('speech_dft.wav');
speech=speech(:,1);
size=length(speech);%语音长度
numofwin=floor(size/winsize);%窗数
%读入有噪声的语音信号
[y,fs]=wavread('output.wav');
y=y(:,1)';
%读入噪声信号
[n,fs]=wavread('noise.wav');
n=n(:,1)';
for q=1:2*numofwin-1
yframe=y(1+(q-1)*winsize/2:winsize+(q-1)*winsize/2);%分帧
nframe=n(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;
hamwin(1+(q-1)*winsize/2:winsize+(q-1)*winsize/2)+ham;
%加噪信号FFT
y1=fft(yframe.*ham);
ypow=abs(y1);%加噪信号幅度
yangle=angle(y1);%相位
n1=fft(nframe.*ham);
npow=abs(n1);%加噪信号幅度
% nangle=angle(n1);%相位
%计算功率谱密度
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(1i*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(1i*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
%{
%设计低通椭圆滤波器
Ft=8000;
Fp1=1000;
Fs1=1200;
wp1=2*Fp1/Ft;
ws1=2*Fs1/Ft;
[N,wc]=ellipord(wp1,ws1,1,100,'s');
%最小阶数和截止频率。根据转换后的技术指标使用滤波器阶数函数,确定滤波器的最小阶数N和截止频率Wc;
[b,a]=ellip(N,1,100,wc);
%经过低通椭圆滤波器
improved=filter(b,a,improved);
enhanced=filter(b,a,enhanced);
%}
SNR1=10*log10(var(speech')/var(n(1:size)'));%加噪语音信噪比
SNR2=10*log10(var(speech')/var(enhanced-speech'));%增强语音信噪比
SNR3=10*log10(var(speech')/var(improved-speech'));
colormap white;
subplot(511)
plot(speech');%原始语音波形
title('Original Voice');
subplot(512)
plot(n');%原始噪声波形
title('Original Noice');
subplot(513)
plot(y);
title(['Noise Added(SNR=',num2str(SNR1),'dB)']);
subplot(514)
plot(enhanced);
title(['Enhanced Voice(SNR=',num2str(SNR2),'dB)']);
subplot(515)
plot(improved);
title(['Improved Voice(SNR=',num2str(SNR3),'dB)']);