(n,k,N)卷积码的维特比译码算法实现#includeiostream#definet_src0#definet_des1#definet_len2#definet_flag3#definet_in4usingnamespacestd;intmyn=0;intstalen=0;intmyg1[10]={0};intmyg2[10]={0};intstan0[256][2]={0};//输入0时个状态的输出intstan1[256][2]={0};//输入1时各状态的输出intstachn[256][2]={0};//状态装换表intpath[256][100]={0};//存储路径intcalpath[256]={0};//存储路径长度intmyin[24];//一次处理12次intmyout[200];//intmyoutsym=0;intpthsym;intoutfull=0;//决定是否输出inttable1[8]={1,2,4,8,16,32,64,128};voidchartobits(charch,int*bits);charbitstochar(int*bits);intcalluj(inta1,inta2,intb1,intb2);voidinitpath(void);voidselpath(inta1,inta2);voidwridata(void);voidviterbit(void);voidwritdataedn(void);voidcreatsta(void);voidmyinput(void);intmain(){myinput();creatsta();viterbit();}voidmyinput(void){inti,j;cout输入编码的约束长度N:(3N9)endl;cinmyn;stalen=int(pow(2.0,myn-1));cout选择默认的编码矢量则输入1,输入2则可输入其他的编码矢量endl;cini;if(i==1){switch(myn){case3:myg1[0]=1,myg1[1]=1,myg1[2]=1;myg2[0]=1,myg2[1]=0,myg2[2]=1;break;case4:myg1[0]=1,myg1[1]=1,myg1[2]=1,myg1[3]=1;myg2[0]=1,myg2[1]=0,myg2[2]=1,myg2[3]=1;break;case5:myg1[0]=1,myg1[1]=0,myg1[2]=1,myg1[3]=1,myg1[4]=1;myg2[0]=1,myg2[1]=1,myg2[2]=0,myg2[3]=1,myg2[4]=1;break;case6:myg1[0]=1,myg1[1]=0,myg1[2]=1,myg1[3]=1,myg1[4]=1,myg1[5]=1;myg2[0]=1,myg2[1]=1,myg2[2]=0,myg2[3]=1,myg2[4]=0,myg2[5]=1;break;case7:myg1[0]=1,myg1[1]=0,myg1[2]=0,myg1[3]=1,myg1[4]=1,myg1[5]=1,myg1[6]=1;myg2[0]=1,myg2[1]=1,myg2[2]=0,myg2[3]=1,myg2[4]=1,myg2[5]=0,myg2[6]=1;break;case8:myg1[0]=1,myg1[1]=0,myg1[2]=0,myg1[3]=1,myg1[4]=1,myg1[5]=1,myg1[6]=1,myg1[7]=1;myg2[0]=1,myg2[1]=1,myg2[2]=1,myg2[3]=0,myg2[4]=0,myg2[5]=1,myg2[6]=0,myg2[7]=1;break;case9:myg1[0]=1,myg1[1]=1,myg1[2]=0,myg1[3]=1,myg1[4]=0,myg1[5]=1,myg1[6]=1,myg1[7]=1,myg1[8]=1;myg2[0]=1,myg2[1]=0,myg2[2]=0,myg2[3]=0,myg2[4]=1,myg2[5]=1,myg2[6]=1,myg2[7]=0,myg2[8]=1;break;}}else{cout输入g1endl;for(j=0;jmyn;j++)cinmyg1[j];cout输入g2endl;for(j=0;jmyn;j++)cinmyg2[j];}cout连接矢量1为endl;for(j=0;jmyn;j++)coutmyg1[j];coutendl;cout连接矢量2为endl;for(j=0;jmyn;j++)coutmyg2[j];coutendl;coutstalen:stalen;coutendl;}voidcreatsta(void){inti,j,k,myi;inttembits[10];for(i=0;istalen;i++){stan1[i][0]=0;stan1[i][1]=0;stan0[i][0]=0;stan0[i][1]=0;stachn[i][0]=i/2;myi=i;for(j=0;jmyn;j++){if(myi=pow(2.0,myn-1-j)){tembits[j]=1;myi=myi-pow(2.0,myn-1-j);}else{tembits[j]=0;}}for(k=0;kmyn;k++){stan0[i][0]+=myg1[k]*tembits[k];stan0[i][1]+=myg2[k]*tembits[k];}stan0[i][0]=stan0[i][0]%2;stan0[i][1]=stan0[i][1]%2;myi=i+int(pow(2.0,myn-1));stachn[i][1]=myi/2;for(j=0;jmyn;j++){if(myi=pow(2.0,myn-1-j)){tembits[j]=1;myi=myi-pow(2.0,myn-1-j);}else{tembits[j]=0;}}for(k=0;kmyn;k++){stan1[i][0]+=myg1[k]*tembits[k];stan1[i][1]+=myg2[k]*tembits[k];}stan1[i][0]=stan1[i][0]%2;stan1[i][1]=stan1[i][1]%2;}cout状态转移出endl;for(i=0;istalen;i++)coutstachn[i][0],stachn[i][1];coutendl;cout输入0状态转移后的输出endl;for(i=0;istalen;i++)coutstan0[i][0],stan0[i][1];coutendl;cout输入1状态转移后的输出endl;for(i=0;istalen;i++)coutstan1[i][0],stan1[i][1];coutendl;}voidchartobits(charch,int*bits){inti;for(i=0;i8;i++){if(ch0)bits[i]=1;elsebits[i]=0;ch=ch1;}}charbitstochar(int*bits){chartemp=0;inti;for(i=0;i8;i++){if(bits[i]==1)temp+=table1[7-i];}returntemp;}intcalluj(inta1,inta2,intb1,intb2){inty=0;if(a1!=b1)y++;if(a2!=b2)y++;return(y);}voidinitpath(){inttem;intt_tem[256][5]={0};inti,j,k,l;intljtem[256][100];intpttem[256]={0};intstaflag[256]={0};staflag[0]=1;inta1,a2;for(l=0;lmyn-1;l++){for(i=0;istalen;i++)for(j=0;jpthsym;j++)ljtem[i][j]=path[i][j];i=0;a1=myin[2*l];a2=myin[2*l+1];for(j=0;jstalen;j++){if(staflag[j]==1){tem=calluj(a1,a2,stan0[j][0],stan0[j][1]);t_tem[i][t_src]=j;t_tem[i][t_des]=stachn[j][0];t_tem[i][t_len]=calpath[j]+tem;t_tem[i][t_in]=0;tem=calluj(a1,a2,stan1[j][0],stan1[j][1]);t_tem[i+1][t_src]=j;t_tem[i+1][t_des]=stachn[j][1];t_tem[i+1][t_len]=calpath[j]+tem;t_tem[i+1][t_in]=1;i=i+2;}}for(k=0;kstalen;k++)staflag[k]=0;for(k=0;ki;k++){staflag[t_tem[k][t_des]]=1;calpath[t_tem[k][t_des]]=t_tem[k][t_len];for(j=0;jpthsym;j++)path[t_tem[k][t_des]][j]=ljtem[t_tem[k][t_src]][j];path[t_tem[k][t_des]][pthsym]=t_tem[k][t_in];}pthsym++;}/*cout初始化后的路径长度endl;for(inti=0;i8;i++)coutcalpath[i];coutendl;*/}voidselpath(inta1,inta2){//16选8intt_tem[512][5]={0};inti,j,tem;intljtem[256][100];j=0;for(i=0;i2*stalen;i=i+2){tem=calluj(a1,a2,stan0[j][0],stan0[j][1]);t_tem[i][t_src]=j;t_tem[i][t_des]=stachn[j][0];t_tem[i][t_len]=calpath[j]+tem;t_tem[i][t_flag]=0;t_tem[i][t_in]=0;//t_tem[i][t_rep]=0;tem=calluj(a1,a2,stan1[j][0],stan1[j][1]);t_tem[i+1][t_src]=j;t_tem[i+1][t_des]=stachn[j][1];t_tem[i+1][t_len]=calpath[j]+tem;t_tem[i+1][t_flag]=0;t_tem[i+1][t_in]=1;//t_tem[i][t_rep]=0;j++;}for(i=0;i2*stalen;i++)for(j=i+1;j2*stalen;j++)if(t_tem[i][t_des]==t_tem[j][t_des]){if(t_tem[i][t_len]=t_tem[j][t_len]){t_tem[i][t_flag]=1;}else{t_tem[j][t_flag]=1;}}for(i=0;istalen;i++)for(j=0;jpthsym;j++)ljtem[i][j]=path[i][j];for(i=0;i2*stalen;i++)if(t_tem[i][t_flag]==1){calpath[t_tem[i][t_des]]=t_tem[i][t_len]