当前位置:文档之家› 信息论matlab的实验用到的编码

信息论matlab的实验用到的编码

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;
s2='Shannon编码平均码字长度L:';
s3='Shannon编码的编码效率q:';
disp(s0);
disp(s1),disp(B),disp(W);
disp(s2),disp(L);
disp(s3),disp(q);
附录D Fano编码
%函数说明:%
% [next_P,next_index,code_num]=compare(current_P,current_index)%
附录B 离散无记忆信道容量的迭代计算
%信道容量C的迭代算法%
%函数说明:%
% [CC,Paa]=ChannelCap(P,k)为信道容量函数%
%变量说明:%
% P:输入的正向转移概率矩阵,k:迭代计算精度%
% CC:最佳信道容量,Paa:最佳输入概率矩阵%
% Pa:初始输入概率矩阵,Pba:正向转移概率矩阵%
s5='输出符号数s:';
s6='迭代计算精度k:';
for i=1:r
B{i}=i;
end
disp(s0);
disp(s1),disp(B),disp(Paa);
disp(s4),disp(r);
disp(s5),disp(s);
disp(s2),disp(CC);
disp(s6),disp(k);
end
end
% (2)再求Pab
suma=zeros(1,s);
for j=1:s
for i=1:r
Pab(j,i)=Pa(i)*Pba(i,j)/(Pb(j)+eps);
suma(j)=suma(j)+Pa(i)*Pba(i,j)*log2((Pab(j,i)+eps)/(Pa(i)+eps));
end
if (abs(sum(p)-1)>10e-10)
error('Not a ,component do not add up to 1') %判断是否符合概率和为1
end
% 1)排序
n=length(p);
x=1:n;
[p,x]=array(p,x);
% 2)计算代码组长度l
l=ceil(-log2(p));
% L为编码返回的平均码字长度,q为编码效率%
%*****************************************%
function [W,L,q]=shannon(p)
%提示错误信息
if (length(find(p<=0))~=0)
error('Not a ,negative component'); %判断是否符合概率分布条件
end
% 3)求信道容量C
C=sum(suma);
% 4)求下一次Pa,即Paa
L=zeros(1,r);
sumaa=0;
for i=1:r
for j=1:s
L(i)=L(i)+Pba(i,j)*log(Pab(j,i)+eps);
end
a(i)=exp( L(i));
end
sumaa=sum(a);
Pa=(1/(r+eps))*ones(1,r);
sumrow=zeros(1,r);
Pba=P;
% 2)进行迭代计算
n=0;
C=0;
CC=1;
while abs(CC-C)>=k
n=n+1;
% (1)先求Pb
Pb=zeros(1,s);
for j=1:s
for i=1:r
Pb(j)=Pb(j)+Pa(i)*Pba(i,j);
error('Not a ,negative component'); %判断是否符合概率分布条件
end
if (abs(sum(P)-1)>10e-10)
error('Not a ,component do not add up to 1');
end
H=(sum(-P.*log2(P)))/(log2(r)+eps);
error('Not a ,negative component'); %判断是否符合概率分布条件
end
if (abs(sum(P')-1)>10e-10)
error('Not a ,component do not add up to 1') %判断是否符合概率和为1
end
% 1)初始化Pa
[r,s]=size(P);
disp(s3),disp(n);
附录C Shannon编码
%函数说明:%
% [p,x]=array(P,X)为按序排序的函数%
% P为信源的概率矢量,X为概率元素的下标矢量%
% p为排序后返回的信源的概率矢量%
% x为排序后返回的概率元素的下标矢量%
%*******************************************%
% Pb:输出概率矩阵,Pab:反向转移概率矩阵%
% C:初始信道容量,r:输入符号数,s:输出符号数%
%**************************************************%
function [CC,Paa]=ChannelCap(P,k)
%提示错误信息
if (length(find(P<0))~=0)
% L为编码返回的平均码字长度,q为编码效率%
%*****************************************%
function [W,L,q]=fano(P)
%提示错误信息
if (length(find(P<=0))~=0)
error('Not a ,negative component'); %判断是否符合概率分布条件
function [next_P,code_num,next_index]=compare(current_P,current_index);
n=length(current_P);
add(1)=current_P(1);
% 1)求概率的依次累加和
for i=2:n
add(i)=0;
add(i)=add(i-1)+current_P(i);
s2='Fano编码平均码字长度L:';
s3='Fano编码的编码效率q:';
disp(s0);
disp(s1),disp(B),disp(W);
disp(s2),disp(L);
disp(s3),disp(q);
附录E Huffman编码
Huffman编码(1)
% huffman编码生成器%
%函数说明:%
next_P=current_P(1:k);
else
next_index=current_index-k;
code_num=49;
next_P=current_P((k+1):n);
end
% fano编码生成器%
%函数说明:%
% [W,L,q]=fano(P)为fano编码函数%
% P为信源的概率矢量,W为编码返回的码字%
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:';
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; %计算编码效率
%打印输出结果
end
if (abs(sum(P)-1)>10e-10)
error('Not a ,component do not add up to 1') %判断是否符合概率和为1
end
% 1)排序
n=length(P);
x=1:n;
[P,x]=array(P,x);
% 2)将信源符号分组并得到对应的码字
for i=1:n
error('Not a ,negative component'); %判断是否符合概率分布条件
end
if (abs(sum(P)-1)>10e-10)
error('Not a ,component do not add up to 1') %判断是否符合概率和为1
if (i<n)
for k=(maxN-1):-1:i
相关主题