您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > (2-1-3)卷积码C语言
#includestdio.h#includeConio.h#defineN7#includemath.h#includestdlib.h#includetime.h#definerandomize()srand((unsigned)time(NULL))encode(unsignedint*symbols,/*编码输出*/unsignedint*data,/*编码输入*/unsignedintnbytes,/*nbytes=n/16,n为实际输入码字的数目*/unsignedintstartstate/*定义初始化状态*/){intj;unsignedintinput,a1=0,a2=0;for(j=0;jnbytes;j++){input=*data;data++;*symbols=input^a1^a2;symbols++;*symbols=input^a2;symbols++;a2=a1;a1=input;}return0;}inttrandistance(intm,/*符号m与从state1到state2时输出符号的汉明距离,如果state1无法到state2则输出度量值为100*/intstate1,intstate2){intc;intsym,sym1,sym2;sym1=((state21)&1)^(state2&1)^(state1&1);sym2=((state21)&1)^(state1&1);sym=(sym11)|sym2;if(((state1&2)1)==(state2&1))c=((m&1)^(sym&1))+(((m1)&1)^((sym1)&1));elsec=10000;return(c);}inttraninput(inta,intb)/*状态从a到b时输入卷积码的符号*/{intc;c=((b&2)1);return(c);}inttranoutput(inta,intb)/*状态从a到b时卷积码输出的符号*/{intc,s1,s2;s1=(a&1)^((a&2)1)^((b&2)1);s2=(a&1)^((b&2)1);c=(s11)|s2;return(c);}voidviterbi(intinitialstate,/*定义解码器初始状态*/int*viterbiinput,/*解码器输入码字序列*/int*viterbioutput/*解码器输出码字序列*/){structsta/*定义网格图中每一点为一个结构体,其元素包括*/{intmet;/*转移到此状态累计的度量值*/intvalue;/*输入符号*/structsta*last;/*及指向前一个状态的指针*/};structstastate[4][N];structsta*g,*head;inti,j,p,q,t,r,u,l;for(i=0;i4;i++)/*初始化每个状态的度量值*/for(j=0;jN;j++)state[i][j].met=0;for(l=0;l4;l++){state[l][0].met=trandistance(*viterbiinput,initialstate,l);state[l][0].value=traninput(initialstate,l);state[l][0].last=NULL;}viterbiinput++;/*扩展第一步幸存路径*/for(t=1;tN;t++){for(p=0;p4;p++){state[p][t].met=state[0][t-1].met+trandistance(*viterbiinput,0,p);state[p][t].value=traninput(0,p);state[p][t].last=&state[0][t-1];for(q=0;q4;q++){if(state[q][t-1].met+trandistance(*viterbiinput,q,p)state[p][t].met){state[p][t].met=state[q][t-1].met+trandistance(*viterbiinput,q,p);state[p][t].value=traninput(q,p);state[p][t].last=&state[q][t-1];}}}viterbiinput++;}/*计算出剩余的幸存路径*/r=state[0][N-1].met;/*找出n步后度量值最小的状态,准备回溯路由*/g=&state[0][N-1];for(u=N;u0;u--)/*向前递归的找出最大似然路径*/{*(viterbioutput+(u-1))=g-value;g=g-last;}/*for(u=0;u8;u++)*(viterbioutput+u)=state[u][2].met;*//*此行程序可用于检测第n列的度量值*/}voiddecode(unsignedint*input,int*output,intn){intviterbiinput[100];intj;for(j=0;jn+2;j++){viterbiinput[j]=(input[j*2]1)|input[j*2+1];//printf(%3d,viterbiinput[j]);}viterbi(0,viterbiinput,output);}voidmain(){unsignedintencodeinput[100],wrong[10]={0,0,0,0,0,0,0,0,0,0},encodeoutput[100];intn=5,i,m,j=0,decodeinput[100],decodeoutput[100];randomize();for(i=0;in;i++)encodeinput[i]=rand()%2;encodeinput[n]=encodeinput[n+1]=0;encode(encodeoutput,encodeinput,n+2,0);printf(theinputofencoderis:\n);for(i=0;in;i++)printf(%2d,encodeinput[i]);printf(\n);printf(theoutputofencoderis:\n);for(i=0;i(n+2)*2;i++){printf(%2d,encodeoutput[i]);if(i%20==19)printf(\n);}printf(\n);printf(pleaseinputthenumberofthewrongbit\n);scanf(%d,&m);printf(pleaseinputthepositionsofthewrongbit(0-9)\n);for(i=0;im;i++){scanf(%d,&wrong[m]);if(encodeoutput[wrong[m]]==0)encodeoutput[wrong[m]]=1;elseencodeoutput[wrong[m]]=0;}printf(theinputofdecoderis:\n);for(i=0;i(n+2)*2;i++){printf(%2d,encodeoutput[i]);if(i%20==19)printf(\n);}printf(\n);decode(encodeoutput,decodeoutput,n+2);printf(theoutputofdecoderis:\n);for(i=0;in;i++)printf(%2d,decodeoutput[i]);printf(\n);for(i=0;in;i++){if(encodeinput[i]!=decodeoutput[i])j++;}printf(thenumberofincorrectbitis:%d\n,j);}
本文标题:(2-1-3)卷积码C语言
链接地址:https://www.777doc.com/doc-7247923 .html