资源描述:
《MATLAB程序-信源编码.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、计算信源熵的MATLAB源程序functionH=entropy(P,r)if(length(find(P<=0))~=0)error('Notaprob.vector,negativecomponent');endif(abs(sum(P)-1)>10e-10)error('Notaprob.vector,componentdonotaddupto1');endH=(sum(-P.*log2(P)))/(log2(r)+eps);香农编码的MATLAB源程序function[s,L,q]=shannon(p)%if(length(find(p<=0))~=0)%er
2、ror('Notaprob.vector,negativecomponent');%end%if(abs(sum(p)-1)>10e-10)%error('Notaprob.vector,componentdonotaddupto1')%endn=length(p);x=1:n;[p,x]=array(p,x);l=ceil(-log2(p));P(1)=0;n=length(p);fori=2:nP(i)=P(i-1)+p(i-1);endfori=1:nforj=1:l(i)temp(i,j)=floor(P(i)*2);P(i)=P(i)*2-temp(i,j)
3、;endends=[];fori=1:nforj=1:l(i)t=temp(i,j);%if(temp(i,j)==0)%W(i,j)=48;s=[snum2str(t)];%else%W(i,j)=49;%endends=[s''];endL=sum(p.*l);H=entropy(p,2);q=H/L;fori=1:nB{i}=i;ends0='很好!输入正确,编码结果如下:';s1='Shannon编码所得码字W:';s2='Shannon编码平均码字长度L:';s3='Shannon编码的编码效率q:';disp(s0);disp(s1),disp(B),di
4、sp(s);disp(s2),disp(L);disp(s3),disp(q);费诺编码的MATLAB源程序function[W,L,q]=fano(P)if(length(find(P<=0))~=0)error('Notaprob.vector,negativecomponent');endif(abs(sum(P)-1)>10e-10)error('Notaprob.vector,componentdonotaddupto1');endn=length(P);x=1:n;[P,x]=array(P,x);fori=1:ncurrent_index=i;j=1;c
5、urrent_P=P;while1[next_P,code_num,next_index]=compare(current_P,current_index);current_index=next_index;current_P=next_P;W(i,j)=code_num;j=j+1;if(length(current_P)==1)break;endendl(i)=length(find(abs(W(i,:))~=0));endL=sum(P.*l);H=entropy(P,2);q=H/L;fori=1:nB{i}=i;end[n,m]=size(W);TEMP=3
6、2*ones(n,5);W=[W,TEMP];W=W';[n,m]=size(W);W=reshape(W,1,n*m);W=sprintf('%s',W);s0='很好!输入正确,编码结果如下:';s1='Fano编码所得码字W:';s2='Fano编码平均码字长度L:';s3='Fano编码的编码效率q:';disp(s0);disp(s1),disp(B),disp(W);disp(s2),disp(L);disp(s3),disp(q);霍夫曼编码的MATLAB源程序function[W,L,q]=huffman(P)if(length(find(P<=0))
7、~=0)error('Notaprob.vector,negativecomponent');endif(abs(sum(P)-1)>10e-10)error('Notaprob.vector,componentdonotaddupto1');endn=length(P);p=P;mark=zeros(n-1,n);fori=1:n-1[p,num]=sort(p);mark(i,:)=[num(1:n-i+1),zeros(1,i-1)];p=[p(1)+p(2),p(3:n),1];endfori=1:n-1table(i,:)=blanks(n