您好,欢迎访问三七文档
信息论实验报告学生:班级:学号:实验一香农编码一、程序设计的流程图二、程序清单#includeiostream输入N输入信源概率X(N)由大到小排序求累加概率累加概率转为二进制确定码长K[i]求码字输出码字结束开始#includemath.h#includestringusingnamespacestd;voidswap(double*x,double*y);intmain(){intN;cout输入信源个数endl;cinN;doubleS[N];//注意变量在数组中的影响cout输入信源概率endl;for(inti=0;iN;i++)cinS[i];for(inti=0;iN;i++){for(intj=i;jN;j++)if(S[i]S[j])swap(S[i],S[j]);}intnm[N];for(inti=0;iN;i++){nm[i]=int(-(log(S[i])/log(2)))+1;if(nm[i]==(-(log(S[i])/log(2)))+1)nm[i]--;}doubleAA[N];AA[0]=S[0];for(inti=1;iN;i++)AA[i]=AA[i-1]+S[i];stringMM[N];for(inti=0;iN;i++){doubletem=0;doubleaa=AA[i];for(intj=0;jN;j++){tem=aa*2;if(tem1){MM[i]+='1';aa=tem-1;}else{MM[i]+='0';aa=tem;}}}stringBB[N];for(inti=0;iN;i++){for(intj=0;jnm[i];j++)BB[i]+=MM[i][j];}cout输出编码endl;for(inti=0;iN;i++)coutBB[i]endl;}voidswap(double&x,double&y){doublea;a=x;x=y;y=a;}实验二.费洛编码.一、流程图NY二、程序清单#includeiostream#includestring开始输入信源个数n输入信源符号sing【i】输入信源符号概率平p【i】按p【i】从小到大排序N-1=1?调用fano()进行编码编码输出结束#includemath.husingnamespacestd;voiddw(intn,intH,intW,double*si,string*m);intmain(){intN;cout输入信源个数endl;cinN;doubleS[N];cout输入信源概率endl;for(inti=0;iN;i++)cinS[i];for(inti=0;iN;i++){for(intj=i;jN;j++){if(S[i]S[j]){doublea;a=S[i];S[i]=S[j];S[j]=a;}}}stringMM[N];dw(N,0,N-1,S,MM);cout输出编码endl;for(inti=0;iN;i++)coutMM[i]endl;}voiddw(intn,intH,intW,double*si,string*m){doublesum=0,sm=0;inta1=W-H;if(a1=1){for(intj=H;j=W;j++)sum+=*(si+j);doubleb[n];for(inti=H;i=W;i++){sm+=*(si+i);b[i]=fabs(2*sm-sum)/sum;}intZH;doublea=b[H];for(inti=H;i=W;i++)if(b[i]=a){a=b[i];ZH=i;}for(inti=H;i=W;i++){if(i=ZH)m[i]+='0';elsem[i]+='1';}intnn=n;double*ss;ss=si;string*mm;mm=m;if(ZH==H)dw(nn,ZH+1,W,ss,mm);elseif(ZH==W-1)dw(nn,H,ZH,ss,mm);else{dw(nn,H,ZH,ss,mm);dw(nn,ZH+1,W,ss,mm);}}}实验三.霍夫曼一、程序流程图否是开始输入lettersStrcmp(“%是”,是)是否二、程序清单#includeiostream#includestringusingnamespacestd;structHNode{doubleweight;intparent;intlchild;intrchild;};voidHuffmanTree(HNode*HuffNode,intn,double*si,string*m);inthc(stringm);voidnv(string*m,intn);intmain(){intN;cout输入信源个数endl;cinN;double*S=newdouble[N];cout输入信源概率endl;for(inti=0;iN;i++)cinS[i];string*MM=newstring[N];for(inti=0;iN;i++)计算字符个数与出现次数I++输出letersIcnt?条用霍夫曼编码结束{for(intj=i;jN;j++){if(S[i]S[j]){doublea;a=S[i];S[i]=S[j];S[j]=a;}}}HNode*node=newHNode[2*N-1];HuffmanTree(node,N,S,MM);nv(MM,N);cout输出编码endl;for(inti=0;iN;i++)coutMM[i]endl;}voidHuffmanTree(HNode*HuffNode,intn,double*si,string*m){inti,j,x1,x2,c,p;floatm1,m2;for(i=0;i2*n-1;i++){HuffNode[i].weight=0;HuffNode[i].parent=-1;HuffNode[i].lchild=-1;HuffNode[i].rchild=-1;}for(i=0;in;i++){HuffNode[i].weight=si[i];}for(i=0;in-1;i++){m1=m2=1;x1=x2=0;for(j=0;jn+i;j++){if(HuffNode[j].parent==-1&&HuffNode[j].weightm1){m2=m1;x2=x1;m1=HuffNode[j].weight;x1=j;}elseif(HuffNode[j].parent==-1&&HuffNode[j].weightm2){m2=HuffNode[j].weight;x2=j;}}HuffNode[x1].parent=n+i;HuffNode[x2].parent=n+i;HuffNode[n+i].weight=HuffNode[x1].weight+HuffNode[x2].weight;HuffNode[n+i].lchild=x1;HuffNode[n+i].rchild=x2;}for(i=0;in;i++){c=i;p=HuffNode[c].parent;while(p!=-1){if(HuffNode[p].lchild==c)m[i]+='0';elsem[i]+='1';c=p;p=HuffNode[c].parent;}}}inthc(stringm){inta=0;for(inti=0;m[i]!='\0';i++)a++;returna;}voidnv(string*m,intn){charmn[n];intb;for(inti=0;in;i++){b=hc(m[i]);for(intj=0;jb;j++){mn[j]=m[i][b-j-1];}m[i]=mn;}}实验四.信道一、程序流程图三、程序清单#includestdlib.h#includeiostream#includemath.husingnamespacestd;intmain(){intni,no;inti,j;开始),,,(21)0(rippppp均匀分布迭代计算器k=0ijkjiijjkjiijkippp)()()1(lnexplnexp)1()1()(kkiCxp和C(K+1)转向如果,)1()()1()1(kkkkCCCC输出结果K=k+1结束否是cout输入信源个数endl;cinni;cout输出信源个数endl;cinno;double*Si=newdouble[ni];double*So=newdouble[no];cout输入信源概率endl;for(i=0;ini;i++){cinSi[i];}coutendl;//再输入信道转移概率矩阵//注意进行判断,某行如果总概率大于1,则出现错误应从新输入cout输入转移概率:endl;float**p;//转移条件概率p=newfloat*[ni];for(i=0;ini;i++){p[i]=newfloat[no];}for(i=0;ini;i++){for(j=0;jno;j++){cinp[i][j];}}for(i=0;ini;i++){floata=0;for(j=0;jno;j++){a+=p[i][j];}if(a!=1.0){cout输入数据有误,请检查后再次输入。endl;}}float**pp;//后验概率pp=newfloat*[ni];for(i=0;ini;i++){pp[i]=newfloat[no];}floatC_Pre,C;//当前信道容量和前一次循环信道容量C=10.0;doublePe=0.000001;//两次信道容量相差的阈值intr=0;//迭代次数float*p_up;//计算第r+1次循环输入分布p_in分子p_up=newfloat[ni];floatp_down;//计算第r+1次循环输入分布p_in分母do{r++;//求第r次循环反条件概率p_ijfor(j=0;jno;j++){So[j]=0.0;for(i=0;ini;i++)So[j]+=p[i][j]*Si[i];if(So[j]0){for(i=0;ini;i++)pp[i][j]=Si[i]*p[i][j]/So[j];//p(xy)/w(y),p(xy)=p(y/x)*p(x)}else{for(i=0;ini;i++)pp[i][j]=0;}}//求第r+1次循环输入分布p_inp_down=0.0;for(i=0;ini;i++){p_up[i]=0.0;for(j=0;jno;j++){if(pp[i][j]=0.000001){p_up[i]+=p[i][j]*log(pp[i][j])/log(2.0);}}p_up[i]=pow(2.0,p_up[i]);p_down+=p_up[i];}for(i=0;ini;i++){Si[i]=p_up[i]/p_down;}//求C(r+1)C_Pre=C;C=log(p_down)/log(2.0);cout第r次的容量为:Cendl;}while(fabs(C-C_Pre)/CPe);cout迭代的次数为:rendl;}实验五.循环码一、程序流程图二、程序清单#includeiostream#includestdlib.h退出输入码长nSwitch(n)生成多项式系数矩阵输入校验位输入信源信息向量cUx(g,c,r,n)X=1?X(g,c,r,n)开始选择编码方式序号usingnamespace
本文标题:信息论实验报告-
链接地址:https://www.777doc.com/doc-4931241 .html