matlab的信源编码
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
B(n,j)=K;%从第二列开始,每列的最后一个元素记录特征元素在该列的位置
r=(B(t-1,j)+B(t,j));%最后两个元素相加
T(t-1)=r;
T(t)=0;
T=fliplr(sort(T));
t=t-1;
end
B;%输出编码表
END1=sym('[0,1]');%给最后一列的元素编码
END=END1;
forn=m:len
r1=r1+a(n);
end
end
cha(i)=abs(f1-r1);
ifcha(i)<cha(mi)
mi=i;
end
end
<<c=
0
0
dowith(2,mi,a,temp)
<<c=
0
1
dowith(3,mi,a,temp)
<<c=
1
0
dowith(4,mi,a,temp)
<<c=
H =
2.4232
P =
0.9891
>>
三种信源编码的MATLAB实现
香农编码
a =[0.25,0.25,0.2,0.15,0.1,0.05];
len=length(a);
p=zeros(1,len);
for i=2:len
p(i)=p(i-1)+a(i-1);
end
for i=1:len
if -log(a(i))/log(2)-fix(-log(a(i))/log(2))==0
end
y=B(n,j+1);
END(t-1)=[char(END1(y)),'0'];
END(t)=[char(END1(y)),'1'];
t=t+1;
END1=END;
end
A%排序后的原概率序列
END%编码结果
for i=1:n
[a,b]=size(char(END(i)));
L(i)=b;
end
nu=nu-mi;
mi=depart(a);
len=len-mi;
dowith(nu,mi,a,temp);
end
end
else
c=0
a=temp;
len=length(a);
mi=depart(a);
fori=1:len
c(i)=' ';
end
end
end
end
子函数
functionmi=depart(a)
B(i,1)=T(i);%生成编码表的第一列
end
r=B(i,1)+B(i-1,1);%最后两个元素相加
T(n-1)=r;
T(n)=0;
T=fliplr(sort(T));
t=n-1;
for j=2:n-1%生成编Байду номын сангаас表的其他各列
for i=1:t
B(i,j)=T(i);
end
K=find(T==r);
for i=1:len
c(i)=' ';
end
dowith(1,mi,a,temp)
子函数
functiondowith(nu,mi,a,temp)
len=length(a);
ifnu<=mi &&mi>1
c=0
fori=1:mi
b(i)=a(i);
end
a=zeros(1,mi);
fori=1:mi
t=3;
d=1;
for j=n-2:-1:1%从倒数第二列开始依次对各列元素编码
for i=1:t-2
if i>1 & B(i,j)==B(i-1,j)
d=d+1;
else
d=1;
end
B(B(n,j+1),j+1)=-1;
temp=B(:,j+1);
x=find(temp==B(i,j));
END(i)=END1(x(d));
k(i)=-log(a(i))/log(2);
else
k(i)=fix(-log(a(i))/log(2))+1;
end
end
n=0;
for i=1:len
n=n+1;
c(n)=k(i);
for m=1:k(i)
n=n+1;
c(n)=fix(p(i)*2);
p(i)=p(i)*2-fix(p(i)*2);
1
1
0
dowith(5,mi,a,temp)
<<c=
1
1
1
0
dowith(6,mi,a,temp)
<<c=
1
1
1
1
哈弗曼编码
A=[0.25,0.25,0.2,0.15,0.1,0.05];
A=fliplr(sort(A));
T=A;
[m,n]=size(A);
B=zeros(n,n-1);
for i=1:n
len=length(a);
front=0;
rest=0;
j=1;
i=1;
front=a(i);
forn=2:len
rest=rest+a(n);
end
mi=1;
cha(1)=abs(front-rest);
f1=front;
r1=0;
fori=2:len
forq=2:i
f1=f1+a(q);
m=i+1;
c=0;
a=temp;
len=length(a);
mi=depart(a);
fori=1:len
c(i)=' ';
end
else
j=1;
fori=(mi+1):len;
b(j)=a(i);
j=j+1;
end
a=zeros(1,len-mi);
fori=1:(len-mi);
a(i)=b(i);
end
a(i)=b(i);
end
len=mi;
mi=depart(a);
dowith(nu,mi,a,temp);
elseifnu>mi
c=1
ifmi==1&&len==2
c=0;
a=temp;len=length(a);mi=depart(a);
fori=1:len
c(i)=' ';
end
elseifmi==1&&nu==2
avlen=sum(L.*A)%平均码长
H1=log2(A);
H=-A*(H1')%熵
P=H/avlen%编码效率
运行结果:
A =
0.2500 0.2500 0.2000 0.1500 0.1000 0.0500
END =
[ 01, 10, 11, 001, 0000, 0001]
avlen =
2.4500
end
end
>> c
c =
Columns 1 through 13
2 0 0 2 0 1 3 1 0 0 3 1 0
Columns 14 through 25
1 4 1 1 0 1 5 1 1 1 1 0
2,2,3,3,4,5分别表示码长
费诺编码
主程序:
a=[0.25,0.25,0.2,0.15,0.1,0.05];len=length(a);mi=depart(a);temp=zeros(1,len);temp=a;
r=(B(t-1,j)+B(t,j));%最后两个元素相加
T(t-1)=r;
T(t)=0;
T=fliplr(sort(T));
t=t-1;
end
B;%输出编码表
END1=sym('[0,1]');%给最后一列的元素编码
END=END1;
forn=m:len
r1=r1+a(n);
end
end
cha(i)=abs(f1-r1);
ifcha(i)<cha(mi)
mi=i;
end
end
<<c=
0
0
dowith(2,mi,a,temp)
<<c=
0
1
dowith(3,mi,a,temp)
<<c=
1
0
dowith(4,mi,a,temp)
<<c=
H =
2.4232
P =
0.9891
>>
三种信源编码的MATLAB实现
香农编码
a =[0.25,0.25,0.2,0.15,0.1,0.05];
len=length(a);
p=zeros(1,len);
for i=2:len
p(i)=p(i-1)+a(i-1);
end
for i=1:len
if -log(a(i))/log(2)-fix(-log(a(i))/log(2))==0
end
y=B(n,j+1);
END(t-1)=[char(END1(y)),'0'];
END(t)=[char(END1(y)),'1'];
t=t+1;
END1=END;
end
A%排序后的原概率序列
END%编码结果
for i=1:n
[a,b]=size(char(END(i)));
L(i)=b;
end
nu=nu-mi;
mi=depart(a);
len=len-mi;
dowith(nu,mi,a,temp);
end
end
else
c=0
a=temp;
len=length(a);
mi=depart(a);
fori=1:len
c(i)=' ';
end
end
end
end
子函数
functionmi=depart(a)
B(i,1)=T(i);%生成编码表的第一列
end
r=B(i,1)+B(i-1,1);%最后两个元素相加
T(n-1)=r;
T(n)=0;
T=fliplr(sort(T));
t=n-1;
for j=2:n-1%生成编Байду номын сангаас表的其他各列
for i=1:t
B(i,j)=T(i);
end
K=find(T==r);
for i=1:len
c(i)=' ';
end
dowith(1,mi,a,temp)
子函数
functiondowith(nu,mi,a,temp)
len=length(a);
ifnu<=mi &&mi>1
c=0
fori=1:mi
b(i)=a(i);
end
a=zeros(1,mi);
fori=1:mi
t=3;
d=1;
for j=n-2:-1:1%从倒数第二列开始依次对各列元素编码
for i=1:t-2
if i>1 & B(i,j)==B(i-1,j)
d=d+1;
else
d=1;
end
B(B(n,j+1),j+1)=-1;
temp=B(:,j+1);
x=find(temp==B(i,j));
END(i)=END1(x(d));
k(i)=-log(a(i))/log(2);
else
k(i)=fix(-log(a(i))/log(2))+1;
end
end
n=0;
for i=1:len
n=n+1;
c(n)=k(i);
for m=1:k(i)
n=n+1;
c(n)=fix(p(i)*2);
p(i)=p(i)*2-fix(p(i)*2);
1
1
0
dowith(5,mi,a,temp)
<<c=
1
1
1
0
dowith(6,mi,a,temp)
<<c=
1
1
1
1
哈弗曼编码
A=[0.25,0.25,0.2,0.15,0.1,0.05];
A=fliplr(sort(A));
T=A;
[m,n]=size(A);
B=zeros(n,n-1);
for i=1:n
len=length(a);
front=0;
rest=0;
j=1;
i=1;
front=a(i);
forn=2:len
rest=rest+a(n);
end
mi=1;
cha(1)=abs(front-rest);
f1=front;
r1=0;
fori=2:len
forq=2:i
f1=f1+a(q);
m=i+1;
c=0;
a=temp;
len=length(a);
mi=depart(a);
fori=1:len
c(i)=' ';
end
else
j=1;
fori=(mi+1):len;
b(j)=a(i);
j=j+1;
end
a=zeros(1,len-mi);
fori=1:(len-mi);
a(i)=b(i);
end
a(i)=b(i);
end
len=mi;
mi=depart(a);
dowith(nu,mi,a,temp);
elseifnu>mi
c=1
ifmi==1&&len==2
c=0;
a=temp;len=length(a);mi=depart(a);
fori=1:len
c(i)=' ';
end
elseifmi==1&&nu==2
avlen=sum(L.*A)%平均码长
H1=log2(A);
H=-A*(H1')%熵
P=H/avlen%编码效率
运行结果:
A =
0.2500 0.2500 0.2000 0.1500 0.1000 0.0500
END =
[ 01, 10, 11, 001, 0000, 0001]
avlen =
2.4500
end
end
>> c
c =
Columns 1 through 13
2 0 0 2 0 1 3 1 0 0 3 1 0
Columns 14 through 25
1 4 1 1 0 1 5 1 1 1 1 0
2,2,3,3,4,5分别表示码长
费诺编码
主程序:
a=[0.25,0.25,0.2,0.15,0.1,0.05];len=length(a);mi=depart(a);temp=zeros(1,len);temp=a;