卷积码编码程序的c语言实现(6页).doc
-#include<iostream>using namespace std;int table18=1,2,4,8,16,32,64,128;int myn=0;int stalen=0;int stan02562=0;/输入0时个状态的输出int stan12562=0;/输入1时各状态的输出int stachn2562=0;/状态装换表int myg110=0;int myg210=0;int myout100; /int myoutsym=0;void chartobits(char ch,int *bits);char bitstochar(int *bits);void convolution(void);void creatsta(void);void myinput(void);int main()char exit_char;myinput();creatsta();convolution();cin>>exit_char;void myinput(void)int i,j;cout<<"输入编码的约束长度N:(3<N<9)"<<endl;cin>>myn;stalen=int(pow(2.0,myn-1);cout<<"选择默认的编码矢量则输入1,输入2则可输入其他的编码矢量"<<endl;cin>>i;if(i=1)switch(myn)case 3:myg10=1,myg11=1,myg12=1;myg20=1,myg21=0,myg22=1;break;case 4:myg10=1,myg11=1,myg12=1,myg13=1;myg20=1,myg21=0,myg22=1,myg23=1;break;case 5:myg10=1,myg11=0,myg12=1,myg13=1,myg14=1;myg20=1,myg21=1,myg22=0,myg23=1,myg24=1;break;case 6:myg10=1,myg11=0,myg12=1,myg13=1,myg14=1,myg15=1;myg20=1,myg21=1,myg22=0,myg23=1,myg24=0,myg25=1;break;case 7:myg10=1,myg11=0,myg12=0,myg13=1,myg14=1,myg15=1,myg16=1;myg20=1,myg21=1,myg22=0,myg23=1,myg24=1,myg25=0,myg26=1;break;case 8:myg10=1,myg11=0,myg12=0,myg13=1,myg14=1,myg15=1,myg16=1,myg17=1;myg20=1,myg21=1,myg22=1,myg23=0,myg24=0,myg25=1,myg26=0,myg27=1;break;case 9:myg10=1,myg11=1,myg12=0,myg13=1,myg14=0,myg15=1,myg16=1,myg17=1,myg18=1;myg20=1,myg21=0,myg22=0,myg23=0,myg24=1,myg25=1,myg26=1,myg27=0,myg28=1;break;elsecout<<"输入g1"<<endl;for(j=0;j<myn;j+)cin>>myg1j;cout<<"输入g2"<<endl;for(j=0;j<myn;j+)cin>>myg2j;cout<<"连接矢量1为"<<endl;for(j=0;j<myn;j+)cout<<myg1j<<" "cout<<endl;cout<<"连接矢量2为"<<endl;for(j=0;j<myn;j+)cout<<myg2j<<" "cout<<endl;cout<<"stalen: "<<stalen;cout<<endl;void creatsta(void)int i,j,k,myi,myj;int tembits10;for(i=0;i<stalen;i+)stan1i0=0;stan1i1=0;stan0i0=0;stan0i1=0;stachni0=i/2;myi=i;for(j=0;j<myn;j+)if(myi>=pow(2.0,myn-1-j)tembitsj=1;myi=myi-pow(2.0,myn-1-j);elsetembitsj=0;/*for(k=0;k<myn;k+)cout<<tembitsk<<" "cout<<endl;*/for(k=0;k<myn;k+)stan0i0+=myg1k*tembitsk;stan0i1+=myg2k*tembitsk;stan0i0=stan0i0%2;stan0i1=stan0i1%2;myi=i+int(pow(2.0,myn-1);stachni1=myi/2;for(j=0;j<myn;j+)if(myi>=pow(2.0,myn-1-j)tembitsj=1;myi=myi-pow(2.0,myn-1-j);elsetembitsj=0;/*for(k=0;k<myn;k+)cout<<tembitsk<<" "cout<<endl;*/for(k=0;k<myn;k+)stan1i0+=myg1k*tembitsk;stan1i1+=myg2k*tembitsk;stan1i0=stan1i0%2;stan1i1=stan1i1%2;cout<<"状态转移出"<<endl;for(i=0;i<stalen;i+)cout<<stachni0<<","<<stachni1<<" " cout<<endl;cout<<"输入0状态转移后的输出"<<endl;for(i=0;i<stalen;i+)cout<<stan0i0<<","<<stan0i1<<" " cout<<endl;cout<<"输入1状态转移后的输出"<<endl;for(i=0;i<stalen;i+)cout<<stan1i0<<","<<stan1i1<<" " cout<<endl;void chartobits(char ch,int *bits)int i;for(i=0;i<8;i+)if(ch<0)bitsi=1;elsebitsi=0;ch=ch<<1;char bitstochar(int *bits)char temp=0;int i;for(i=0;i<8;i+)if(bitsi=1)temp+=table17-i;return temp;void convolution()FILE *fp_input,*fp_output;if(!(fp_input=fopen("D:/input.txt","r")=1)cout<<"failed to open input_file"<<endl;exit(0);elsecout<<"we opened the input_file "<<endl;if(!(fp_output=fopen("D:/output.txt","w+")=1)cout<<"failed to open output_file"<<endl;exit(0);elsecout<<"we opened the output_file "<<endl;char ch;int i,j;int mybits8,mytembits8;int mysta=0;int wcout;char wch;for(ch=fgetc(fp_input);feof(fp_input)=0;ch=fgetc(fp_input)chartobits(ch,mybits);/*cout<<"输入数据为"<<endl;for(i=0;i<8;i+)cout<<mybitsi<<" "cout<<endl;*/for(i=0;i<8;i+)if(mybitsi=0)myoutmyoutsym+=stan0mysta0;myoutmyoutsym+=stan0mysta1;mysta=stachnmysta0;elsemyoutmyoutsym+=stan1mysta0;myoutmyoutsym+=stan1mysta1;mysta=stachnmysta1;/*cout<<"输出数据1为"<<endl;for(temi=0;temi<myoutsym;temi+)cout<<myouttemi<<" "cout<<endl;*/if(myoutsym>=8)wcout=myoutsym/8;for(i=0;i<wcout;i+)for(j=0;j<8;j+)mytembitsj=myout8*i+j;wch=bitstochar(mytembits);fputc(wch,fp_output);/*cout<<"输出数据2为"<<endl;cout<<wch<<" "*/for(i=0;i<myoutsym-wcout*8;i+)myouti=myoutwcout*8+i;myoutsym=myoutsym-wcout*8;for(i=0;i<myn-1;i+)myoutmyoutsym+=stan0mysta0;myoutmyoutsym+=stan0mysta1;mysta=stachnmysta0;wcout=myoutsym/8;for(i=0;i<wcout;i+)for(j=0;j<8;j+)mytembitsj=myout8*i+j;wch=bitstochar(mytembits);fputc(wch,fp_output);for(i=0;i<myoutsym-wcout*8;i+)myouti=myoutwcout*8+i;myoutsym=myoutsym-wcout*8;if(myoutsym!=0)for(i=0;i<8-myoutsym;i+)myoutmyoutsym+=0;for(j=0;j<8;j+)mytembitsj=myoutj;wch=bitstochar(mytembits);fputc(wch,fp_output);fclose(fp_input);fclose(fp_output);-第 6 页-