资源描述:
《信息论实验报告2信源编码.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、成绩辽宁工程技术大学上机实验报告实验名称信源编码院系姓名实验目的简述本次实验目的:1、理解并掌握香农编码2、理解并掌握费诺编码3、理解并掌握霍夫曼编码实验准备你为本次实验做了哪些准备:认真阅读《信息论》教材,熟悉三种编码的原理以及相应的MATLAB函数指令实验进度本次共有6个练习,完成6实验总结日本次实验的收获、体会、经验、问题和教训:1、香农编码Matlab源码function[W,L,q]=shannon(p)if(length(find(p<=0))~=0)error('Notaprob.vector,negativec
2、omponent');endif(abs(sum(p)-1)>10e-10)error('Notaprob.vector,componentdonotaddupto1');endn=length(p);x=1:n;[p,x]=array(p,x);%1)排序l=ceil(-log2(p));%2)计算代码组长度lP(1)=0;n=length(p);%3)计算累加概率Pfori=2:nP(i)=P(i-1)+p(i-1);endfori=1:n%4)求得二进制代码组Wforj=1:l(i)temp(i,j)=floor(P(i
3、)*2);P(i)=P(i)*2-temp(i,j);endendfori=1:nforj=1:l(i)if(temp(i,j)==0);W(i,j)=48;elseW(i,j)=49;endendendL=sum(p.*l);%计算平均码字长度H=entropy(p,2);%计算信源熵q=H/L;%计算编码效率fori=1:nB{i}=i;end[n,m]=size(W);TEMP=32*ones(n,6);W=[W,TEMP];W=W';[n,m]=size(W);W=reshape(W,1,n*m);W=sprintf(
4、'%s',W);s0='很好!输入正确,编码结果如下:';s1='Shannon编码所得码字W:';s2='Shannon编码平均码字长度L:';s3='Shannon编码的编码效率q:';disp(s0);disp(s1),disp(B),disp(W);disp(s2),disp(L);disp(s3),disp(q);functionH=entropy(P,r)if(length(find(P<=0))~=0)%判断是否符合概率分布条件error('Notaprob.vector,negativecomponent');
5、endif(abs(sum(P)-1)>10e-10)error('Notaprob.vector,componentdonotaddupto1');endH=(sum(-P.*log2(P)))/(log2(r)+eps);2、香农编码数值试验算例单符号离散无记忆信源=p=[0.25,0.25,0.2,0.15,0.1,0.05];[W,L,q]=shannon(p)很好!输入正确,编码结果如下:Shannon编码所得码字W:[1][2][3][4][5][6]0001100101110111110Shannon编码平均码字
6、长度L:2.7000Shannon编码的编码效率q:0.8975W=0001100101110111110L=2.7000q=0.89753、费诺编码Matlab源码(1)编写M文件compare.mfunction[next_P,code_num,next_index]=compare(current_P,current_index)n=length(current_P);add(1)=current_P(1);fori=2:n%1)求概率的依次累加和add(i)=0;add(i)=add(i-1)+current_P(i)
7、;ends=add(n);%2)求概率和最接近的两小组fori=1:ntemp(i)=abs(s-2*add(i));end[c,k]=min(temp);if(current_index<=k)next_index=current_index;code_num=48;next_P=current_P(1:k);elsenext_index=current_index-k;code_num=49;next_P=current_P((k+1):n);end(2)编写M文件fano.mfunction[W,L,q]=fano(P)
8、if(length(find(P<=0))~=0)error('Notaprob.vector,negativecomponent');endif(abs(sum(P)-1)>10e-10)error('Notaprob.vector,componentdonotaddupt