资源描述:
《信息论实验报告2信源编码》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、辽宁工程技术大学上机实验报告成绩实验名称信源编码院系姓名实验目的简述本次实验冃的:1、理解并掌握香农编码2、理解并掌握费诺编码3、理解并掌握霍夫曼编码实验准备你为本次实验做了哪些准备:认真阅读《信息论》教材,熟悉三种编码的原理以及相应的MATLAB函数指令实验进度本次共有6个练习,完成6实验总结本次实验的收获、体会、经验、问题和教训:1、香农编码Matlab源码function[W,L,q]二shannon(p)if(length(find(p<=0))~二0)error(?Notaprob,vector,negative
2、component,);endif(abs(sum(p)-l)>10e-10)error(,Notaprob.vector,componentdonotaddupto1');endn=length(p);x=l:n;[p,x]=array(p,x);%1)排序l=ceil(-log2(p));%2)计算代码组长度1P(l)=0;n=length(p);%3)计算累加概率Pfori=2:nP(i)二P(iT)+p(iT);endfori=l:n%4)求得二进制代码组Wforj=l:1(i)temp(i,j)=floor(P(
3、i)*2);P(i)=P(i)*2-temp(i,j);endendfori=l:nforj=l:1(i)if(temp(i,j)==0);W(i,j)=48;elseW(i,j)=49;endendendL=sum(p.*1);%计算平均码字长度H二entropy®,2);%计算信源爛q二H/L;%计算编码效率fori=l: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=sprint
4、f('%s',W);sO二'很好!输入正确,编码结果如下:’;sl二'Sharrnon编码所得码字W:';s2=,Shannon编码平均码字长度L:';s3=,Shannon编码的编码效率q:';disp(sO);disp(sl),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,negativecompone
5、nt,);endif(abs(sum(P)-l)>10e-10)error(?Notaprob,vector,componentdonotaddupto1');endH=(sum(-P.*log2(P)))/(log2(r)+eps);2、香农编码数值试验算例单符号离散无记忆信源■X=al,a2,a3,a4,a5,a6p(X)_0.25,0.25,0.2,0.15,0.1,0.05_p二[0.25,0.25,0.2,0.15,0.1,0.05];[W,L,q]=shannon(p)很好!输入正确,编码结果如下:Shanno
6、n编码所得码宁W:[1]⑵[3][4][5]⑹0001100101110111110Shannon编码平均码字长度L:2.7000Shannon编码的编码效率q:0.89750001100101110111110L=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);fo
7、ri=2:n%1)求概率的依次累加和add(i)=0;add(i)=add(i-l)+current_P(i):ends=add(n);%2)求概率和最接近的两小组fori=l:ntemp(i)二abs(s-2*add(i));endEc,k]二min(temp);if(current_index<=k)next_index=current_index;code_num=48;next_P=current_P(l:k);elsenext_index=current_index-k;code_num=49;next_P=cu
8、rrent_P((k+1):n);end(2)编写M文件fano.mfunction[W,L,q]=fano(P)if(1ength(find(P<=0))~二0)errorCNotaprob・vector,negativecomponent,);endif(abs(sum(P)-l)>10e~10)e