您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 卷积码编码程序的c语言实现
#includeiostreamusingnamespacestd;inttable1[8]={1,2,4,8,16,32,64,128};intmyn=0;intstalen=0;intstan0[256][2]={0};//输入0时个状态的输出intstan1[256][2]={0};//输入1时各状态的输出intstachn[256][2]={0};//状态装换表intmyg1[10]={0};intmyg2[10]={0};intmyout[100];//intmyoutsym=0;voidchartobits(charch,int*bits);charbitstochar(int*bits);voidconvolution(void);voidcreatsta(void);voidmyinput(void);intmain(){charexit_char;myinput();creatsta();convolution();cinexit_char;}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,myj;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++)couttembits[k];coutendl;*/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++)couttembits[k];coutendl;*/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;}voidconvolution(){FILE*fp_input,*fp_output;if(!(fp_input=fopen(D://input.txt,r))==1){coutfailedtoopeninput_fileendl;exit(0);}elsecoutweopenedtheinput_fileendl;if(!(fp_output=fopen(D://output.txt,w+))==1){coutfailedtoopenoutput_fileendl;exit(0);}elsecoutweopenedtheoutput_fileendl;charch;inti,j;intmybits[8],mytembits[8];intmysta=0;intwcout;charwch;for(ch=fgetc(fp_input);feof(fp_input)==0;ch=fgetc(fp_input)){chartobits(ch,mybits);/*cout输入数据为endl;for(i=0;i8;i++)coutmybits[i];coutendl;*/for(i=0;i8;i++){if(mybits[i]==0){myout[myoutsym++]=stan0[mysta][0];myout[myoutsym++]=stan0[mysta][1];mysta=stachn[mysta][0];}else{myout[myoutsym++]=stan1[mysta][0];myout[myoutsym++]=stan1[mysta][1];mysta=stachn[mysta][1];}}/*cout输出数据1为endl;for(temi=0;temimyoutsym;temi++)coutmyout[temi];coutendl;*/if(myoutsym=8){wcout=myoutsym/8;for(i=0;iwcout;i++){for(j=0;j8;j++)mytembits[j]=myout[8*i+j];wch=bitstochar(mytembits);fputc(wch,fp_output);/*cout输出数据2为endl;coutwch;*/}for(i=0;imyoutsym-wcout*8;i++)myout[i]=myout[wcout*8+i];myoutsym=myoutsym-wcout*8;}}for(i=0;imyn-1;i++){myout[myoutsym++]=stan0[mysta][0];myout[myoutsym++]=stan0[mysta][1];mysta=stachn[mysta][0];}wcout=myoutsym/8;for(i=0;iwcout;i++){for(j=0;j8;j++)mytembits[j]=myout[8*i+j];wch=bitstochar(mytembits);fputc(wch,fp_output);}for(i=0;imyoutsym-wcout*8;i++)myout[i]=myout[wcout*8+i];myoutsym=myoutsym-wcout*8;if(myoutsym!=0){for(i=0;i8-myoutsym;i++)myout[myoutsym++]=0;}for(j=0;j8;j++)mytembits[j]=myout[j];wch=bitstochar(mytembits);fputc(wch,fp_output);fclose(fp_input);fclose(fp_output);}
本文标题:卷积码编码程序的c语言实现
链接地址:https://www.777doc.com/doc-5595546 .html