《卷积码编码程序的c语言实现(6页).doc》由会员分享,可在线阅读,更多相关《卷积码编码程序的c语言实现(6页).doc(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-#includeusing 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(voi
2、d);void creatsta(void);void myinput(void);int main()char exit_char;myinput();creatsta();convolution();cinexit_char;void myinput(void)int i,j;cout输入编码的约束长度N:(3N9)myn;stalen=int(pow(2.0,myn-1);cout选择默认的编码矢量则输入1,输入2则可输入其他的编码矢量i;if(i=1)switch(myn)case 3:myg10=1,myg11=1,myg12=1;myg20=1,myg21=0,myg22=1;br
3、eak;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,my
4、g11=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,myg1
5、5=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输入g1endl;for(j=0;jmyg1j;cout输入g2endl;for(j=0;jmyg2j;cout连接矢量1为endl;for(j=0;jmyn;j+)coutmyg1j ;coutendl;cout连接矢量2为endl;for(j=0;jmyn;j+)coutmyg2j ;coutendl;coutstalen: stalen;coutendl;void
6、 creatsta(void)int i,j,k,myi,myj;int tembits10;for(i=0;istalen;i+)stan1i0=0;stan1i1=0;stan0i0=0;stan0i1=0;stachni0=i/2;myi=i;for(j=0;j=pow(2.0,myn-1-j)tembitsj=1;myi=myi-pow(2.0,myn-1-j);elsetembitsj=0;/*for(k=0;kmyn;k+)couttembitsk ;coutendl;*/for(k=0;kmyn;k+)stan0i0+=myg1k*tembitsk;stan0i1+=myg2k*
7、tembitsk;stan0i0=stan0i0%2;stan0i1=stan0i1%2;myi=i+int(pow(2.0,myn-1);stachni1=myi/2;for(j=0;j=pow(2.0,myn-1-j)tembitsj=1;myi=myi-pow(2.0,myn-1-j);elsetembitsj=0;/*for(k=0;kmyn;k+)couttembitsk ;coutendl;*/for(k=0;kmyn;k+)stan1i0+=myg1k*tembitsk;stan1i1+=myg2k*tembitsk;stan1i0=stan1i0%2;stan1i1=stan1
8、i1%2;cout状态转移出endl;for(i=0;istalen;i+)coutstachni0,stachni1 ; coutendl;cout输入0状态转移后的输出endl;for(i=0;istalen;i+)coutstan0i0,stan0i1 ; coutendl;cout输入1状态转移后的输出endl;for(i=0;istalen;i+)coutstan1i0,stan1i1 ; coutendl;void chartobits(char ch,int *bits)int i;for(i=0;i8;i+)if(ch0)bitsi=1;elsebitsi=0;ch=ch1;c
9、har bitstochar(int *bits)char temp=0;int i;for(i=0;i8;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)coutfailed to open input_fileendl;exit(0);elsecoutwe opened the input_file endl;if(!(fp_output=fopen(D:/output.txt,w+)=1)c
10、outfailed to open output_fileendl;exit(0);elsecoutwe 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;i8;i+)coutmybitsi ;coutendl;*/for(i=0;i8;i+)i
11、f(mybitsi=0)myoutmyoutsym+=stan0mysta0;myoutmyoutsym+=stan0mysta1;mysta=stachnmysta0;elsemyoutmyoutsym+=stan1mysta0;myoutmyoutsym+=stan1mysta1;mysta=stachnmysta1;/*cout输出数据1为endl;for(temi=0;temimyoutsym;temi+)coutmyouttemi ;cout=8)wcout=myoutsym/8;for(i=0;iwcout;i+)for(j=0;j8;j+)mytembitsj=myout8*i+
12、j;wch=bitstochar(mytembits);fputc(wch,fp_output);/*cout输出数据2为endl;coutwch ;*/for(i=0;imyoutsym-wcout*8;i+)myouti=myoutwcout*8+i;myoutsym=myoutsym-wcout*8;for(i=0;imyn-1;i+)myoutmyoutsym+=stan0mysta0;myoutmyoutsym+=stan0mysta1;mysta=stachnmysta0;wcout=myoutsym/8;for(i=0;iwcout;i+)for(j=0;j8;j+)mytemb
13、itsj=myout8*i+j;wch=bitstochar(mytembits);fputc(wch,fp_output);for(i=0;imyoutsym-wcout*8;i+)myouti=myoutwcout*8+i;myoutsym=myoutsym-wcout*8;if(myoutsym!=0)for(i=0;i8-myoutsym;i+)myoutmyoutsym+=0;for(j=0;j8;j+)mytembitsj=myoutj;wch=bitstochar(mytembits);fputc(wch,fp_output);fclose(fp_input);fclose(fp_output);-第 6 页-