- 1、本文档共4页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
计算信源熵的MATLAB源程序
function H=entropy(P,r)
if (length(find(P=0))~=0)
error(Not a prob.vector,negative component);
end
if(abs(sum(P)-1)10e-10)
error(Not a prob.vector,component do not add up to 1);
end
H=(sum(-P.*log2(P)))/(log2(r)+eps);
香农编码的MATLAB源程序
function [s,L,q]=shannon(p)
%if(length(find(p=0))~=0)
% error(Not a prob.vector,negative component);
%end
%if(abs(sum(p)-1)10e-10)
% error(Not a prob.vector,component do not add up to 1)
%end
n=length(p);
x=1:n;
[p,x]=array(p,x);
l=ceil(-log2(p));
P(1)=0;
n=length(p);
for i=2:n
P(i)=P(i-1)+p(i-1);
end
for i=1:n
for j=1:l(i)
temp(i,j)=floor(P(i)*2);
P(i)=P(i)*2-temp(i,j);
end
end
s=[];
for i=1:n
for j=1:l(i)
t=temp(i,j);
% if(temp(i,j)==0)
% W(i,j)=48;
s=[s num2str(t)];
%else
%W(i,j)=49;
% end
end
s=[s ];
end
L=sum(p.*l);
H=entropy(p,2);
q=H/L;
for i=1:n
B{i}=i;
end
s0=很好!输入正确,编码结果如下:;
s1=Shannon 编码所得码字W:;
s2=Shannon 编码平均码字长度L:;
s3=Shannon 编码的编码效率q:;
disp(s0);
disp(s1),disp(B),disp(s);
disp(s2),disp(L);
disp(s3),disp(q);
费诺编码的MATLAB源程序
function[W,L,q]=fano(P)
if(length(find(P=0))~=0)
error(Not a prob.vector,negative component);
end
if(abs(sum(P)-1)10e-10)
error(Not a prob.vector,component do not add up to 1);
end
n=length(P);
x=1:n;
[P,x]=array(P,x);
for i=1:n
current_index=i;
j=1;
current_P=P;
while 1
[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;
end
end
l(i)=length(find(abs(W(i,:))~=0));
end
L=sum(P.*l);
H=entropy(P,2);
q=H/L;
for i=1:n
B{i}=i;
end
[n,m]=size(W);
TEMP=32*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),di
文档评论(0)