functionViterbi=viterbi()%¾í»ý±àÂëÆ÷g1=111,g2=101%xΪÊäÈëµÄ´ýÒëÂëÐòÁÐx=[1,0,1,0,0,1,0,0,0,1,0,1,1,1];%x=[1,1,0,1,0,1,1,0,0,1]%x=[1,1,1,0,0,0,1,0,1,1]a=size(x);%ÊäÈëÐòÁеij¤¶Ès=a(2)/2;%ÒëÂëºóµÄmÐòÁ㤶ÈΪxµÄÒ»°ëm=zeros(1,s);%×îÖÕ½á¹û´æ·Åma=zeros(1,s+1);%´æ·ÅFa·¾¶µÄmb=zeros(1,s+1);%´æ·ÅFb·¾¶µÄmc=zeros(1,s+1);%´æ·ÅFc·¾¶µÄmd=zeros(1,s+1);%´æ·ÅFd·¾¶µÄtempma=zeros(1,s+1);tempmb=zeros(1,s+1);tempmc=zeros(1,s+1);tempmd=zeros(1,s+1);Fa=0;Fb=0;Fc=0;Fd=0;fori=1:sifi==1d0=dis(x(1),x(2),0,0);d3=dis(x(1),x(2),1,1);Fa=Fa+d0Fb=Fb+d3ma(i)=0;mb(i)=0;mc(i)=0;md(i)=0;continue;elseifi==2d0=dis(x(3),x(4),0,0);d1=dis(x(3),x(4),0,1);d2=dis(x(3),x(4),1,0);d3=dis(x(3),x(4),1,1);%×¢ÒâÏÂÃæµÄ˳Ðò£¬²»ÄÜ´í£¬·ñÔò»á²úÉúÊý¾ÝÏà¹Ø£¬ÒªÃ´¼ÓÖмä±äÁ¿¡£Fc=Fb+d2Fd=Fb+d1Fb=Fa+d3Fa=Fa+d0ma(i)=0;mb(i)=0;mc(i)=2;md(i)=2;continue;elseifi2d0=dis(x(2*i-1),x(2*i),0,0);d1=dis(x(2*i-1),x(2*i),0,1);d2=dis(x(2*i-1),x(2*i),1,0);d3=dis(x(2*i-1),x(2*i),1,1);%ÏÂÃ濪ʼ½øÐмӱȽÏÑ¡£¬Ñ¡FֵСµÄ·¾¶tempa=Fa;tempb=Fb;tempc=Fc;tempd=Fd;ifFa+d0Fc+d3tempa=Fa+d0;tempma=ma;tempma(i)=0;elsetempa=Fc+d3;tempma=mc;tempma(i)=1;endifFa+d3Fc+d0tempb=Fa+d3;tempmb=ma;tempmb(i)=0;elsetempb=Fc+d0;tempmb=mc;tempmb(i)=1;endifFb+d2Fd+d1tempc=Fb+d2;tempmc=mb;tempmc(i)=2;elsetempc=Fd+d1;tempmc=md;tempmc(i)=3;endifFb+d1Fd+d2tempd=Fb+d1;tempmd=mb;tempmd(i)=2;elsetempd=Fd+d2;tempmd=md;tempmd(i)=3;endFa=tempa;Fb=tempb;Fc=tempc;Fd=tempd;ma=tempma;mb=tempmb;mc=tempmc;md=tempmd;endend%×îºóFÈ¡×îСֵµÄÄÇÌõ·¾¶Îª×îÓÅ·¾¶£¬%ͬʱ±ê¼ÇÄǸöÊý×éµÄ×îºóÒ»¸öÔªËØΪÏàÓ¦µÄÊý×Ö%²¢½«×îºóµÄ·¾¶Ëã³öÀ´temp=min([Fa,Fb,Fc,Fd]);ifFa==tempma(6)=0;fort=1:5ifma(t)ma(t+1)m(t)=1;elseif(ma(t)==ma(t+1))&&(ma(t)==3)m(t)=1;elsem(t)=0;endendelseifFb==tempmb(6)=2;fort=1:5ifmb(t)mb(t+1)m(t)=1;elseif(mb(t)==mb(t+1))&&(mb(t)==3)m(t)=1;elsem(t)=0;endendelseifFc==tempmc(6)=1fort=1:5ifmc(t)mc(t+1)m(t)=1;elseif(mc(t)==mc(t+1))&&(mc(t)==3)m(t)=1;elsem(t)=0;endendelseifFd==tempmd(6)=3fort=1:5ifmd(t)md(t+1)m(t)=1;elseif(md(t)==md(t+1))&&(md(t)==3)m(t)=1;elsem(t)=0;endendendm%×Óº¯Êý£º¼ÆËã(m,n)µ½(g,h)µÄººÃ÷¾àÀëfunctiond=dis(m,n,g,h)d=xor(m,g)+xor(n,h);endend