您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 维纳滤波可直接执行matlab代码
functionoutput=WienerScalart96(signal,fs,IS)%output=WIENERSCALART96(signal,fs,IS)%WienerfilterbasedontrackingaprioriSNRusingDecision-Directed%method,proposedbyScalartetal96.Inthismethoditisassumedthat%SNRpost=SNRprior+1.basedonthistheWienerFiltercanbeadaptedtoa%modellikeEphraimsmodelinwhichwehaveagainfunctionwhichisa%functionofaprioriSNRandaprioriSNRisbeingtrackedusingDecision%Directedmethod.%Author:EsfandiarZavarehei%Created:MAR-05if(nargin3|isstruct(IS))IS=.25;%InitialSilenceorNoiseOnlypartinsecondsendW=fix(.025*fs);%Windowlengthis25msSP=.4;%Shiftpercentageis40%(10ms)%Overlap-Addmethodworksgoodwiththisvalue(.4)wnd=hamming(W);%IGNOREFROMHERE...............................if(nargin=3&isstruct(IS))%ThisoptionisforcompatibilitywithanotherprogrammeW=IS.windowsizeSP=IS.shiftsize/W;%nfft=IS.nfft;wnd=IS.window;ifisfield(IS,'IS')IS=IS.IS;elseIS=.25;endend%......................................UPTOHEREpre_emph=0;signal=filter([1-pre_emph],1,signal);NIS=fix((IS*fs-W)/(SP*W)+1);%numberofinitialsilencesegmentsy=segment(signal,W,SP,wnd);%ThisfunctionchopsthesignalintoframesY=fft(y);YPhase=angle(Y(1:fix(end/2)+1,:));%NoisySpeechPhaseY=abs(Y(1:fix(end/2)+1,:));%SpecrogramnumberOfFrames=size(Y,2);FreqResol=size(Y,1);N=mean(Y(:,1:NIS)')';%initialNoisePowerSpectrummeanLambdaD=mean((Y(:,1:NIS)').^2)';%initialNoisePowerSpectrumvariancealpha=.99;%usedinsmoothingxi(ForDeciesionDirectedmethodforestimationofAPrioriSNR)NoiseCounter=0;NoiseLength=9;%ThisisasmoothingfactorforthenoiseupdatingG=ones(size(N));%InitialGainusedincalculationofthenewxiGamma=G;X=zeros(size(Y));%InitializeX(memoryallocation)h=waitbar(0,'Wait...');fori=1:numberOfFrames%%%%%%%%%%%%%%%%VADandNoiseEstimationSTARTifi=NIS%IfinitialsilenceignoreVADSpeechFlag=0;NoiseCounter=100;else%ElseDoVAD[NoiseFlag,SpeechFlag,NoiseCounter,Dist]=vad(Y(:,i),N,NoiseCounter);%MagnitudeSpectrumDistanceVADendifSpeechFlag==0%IfnotSpeechUpdateNoiseParametersN=(NoiseLength*N+Y(:,i))/(NoiseLength+1);%UpdateandsmoothnoisemeanLambdaD=(NoiseLength*LambdaD+(Y(:,i).^2))./(1+NoiseLength);%Updateandsmoothnoisevarianceend%%%%%%%%%%%%%%%%%%%VADandNoiseEstimationENDgammaNew=(Y(:,i).^2)./LambdaD;%ApostirioriSNRxi=alpha*(G.^2).*Gamma+(1-alpha).*max(gammaNew-1,0);%DecisionDirectedMethodforAPrioriSNRGamma=gammaNew;G=(xi./(xi+1));X(:,i)=G.*Y(:,i);%ObtainthenewCleanedvaluewaitbar(i/numberOfFrames,h,num2str(fix(100*i/numberOfFrames)));endclose(h);output=OverlapAdd2(X,YPhase,W,SP*W);%Overlap-addSynthesisofspeechoutput=filter(1,[1-pre_emph],output);%UndotheeffectofPre-emphasisfunctionReconstructedSignal=OverlapAdd2(XNEW,yphase,windowLen,ShiftLen);%Y=OverlapAdd(X,A,W,S);%Yisthesignalreconstructedsignalfromitsspectrogram.Xisamatrix%witheachcolumnbeingthefftofasegmentofsignal.Aisthephase%angleofthespectrumwhichshouldhavethesamedimensionasX.ifitis%notgiventhephaseangleofXisusedwhichinthecaseofrealvaluesis%zero(assumingthatitsthemagnitude).Wisthewindowlengthoftime%domainsegmentsifnotgiventhelengthisassumedtobetwiceaslongas%fftwindowlength.Sistheshiftlengthofthesegmentationprocess(for%exampleinthecaseofnonoverlappingsignalsitisequaltoWandinthe%caseof%50overlapisequaltoW/2.ifnotgivvenW/2isused.Yisthe%reconstructedtimedomainsignal.%Sep-04%EsfandiarZavareheiifnargin2yphase=angle(XNEW);endifnargin3windowLen=size(XNEW,1)*2;endifnargin4ShiftLen=windowLen/2;endiffix(ShiftLen)~=ShiftLenShiftLen=fix(ShiftLen);disp('Theshiftlengthhavetobeanintegerasitisthenumberofsamples.')disp(['shiftlengthisfixedto'num2str(ShiftLen)])end[FreqResFrameNum]=size(XNEW);Spec=XNEW.*exp(j*yphase);ifmod(windowLen,2)%ifFreqResolisoddSpec=[Spec;flipud(conj(Spec(2:end,:)))];elseSpec=[Spec;flipud(conj(Spec(2:end-1,:)))];endsig=zeros((FrameNum-1)*ShiftLen+windowLen,1);weight=sig;fori=1:FrameNumstart=(i-1)*ShiftLen+1;spec=Spec(:,i);sig(start:start+windowLen-1)=sig(start:start+windowLen-1)+real(ifft(spec,windowLen));endReconstructedSignal=sig;functionSeg=segment(signal,W,SP,Window)%SEGMENTchopsasignaltooverlappingwindowedsegments%A=SEGMENT(X,W,SP,WIN)returnsamatrixwhichitscolumnsaresegmented%andwindowedframesoftheinputonedimentionalsignal,X.Wisthe%numberofsamplesperwindow,defaultvalueW=256.SPistheshift%percentage,defaultvalueSP=0.4.WINisthewindowthatismultipliedby%eachsegmentanditslengthshouldbeW.thedefaultwindowishamming%window.%06-Sep-04%EsfandiarZavareheiifnargin3SP=.4;endifnargin2W=256;endifnargin4Window=hamming(W);endWindow=Window(:);%makeitacolumnvectorL=length(signal);SP=fix(W.*SP);N=fix((L-W)/SP+1);%numberofsegmentsIndex=(repmat(1:W,N,1)+repmat((0:(N-1))'*SP,1,W))';hw=repmat(Window,1,N);Seg=signal(Index).*hw;function[NoiseFlag,SpeechFlag,NoiseCounter,Dist]=vad(signal,noise,NoiseCounter,NoiseMargin,Hangover)%[NOISEFLAG,SPEECHFLAG,NOISECOUNTER,DIST]=vad(SIGNAL,NOISE,NOISECOUNTER,NOISEMARGIN,HANGOVER)%SpectralDistanceVoiceActivityDetector%SIGNAListhethecurrentframesmagnitudespectrumwhichistolabeldas%noiseorspeech,NOISEisnoisemagnitudespectrumtemplate(estimation),%NOISECOUNTERisthenumberofimediatepreviousnoiseframes,NOISEMARGIN%(default3)i
本文标题:维纳滤波可直接执行matlab代码
链接地址:https://www.777doc.com/doc-6585091 .html