您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 信息论与编码实验程序与结果图(matlab)
1信源熵实验程序:clc;closeall;clear;linwidd=1fontt=20p0=0;pd=1;N=20p=linspace(p0,pd,N);I=-log2(p);plot(p,I,'k');title('I=-log2(p)函数图');xlabel('p');ylabel('I');clc;closeall;clear;linwidd=1fontt=20p0=0;pd=1;N=20p=linspace(p0,pd,N);H=-p.*log2(p)-(1-p).*log2(1-p);plot(p,H,'k');title('H=-p.*log2(p)-(1-p).*log2(1-p)函数图');xlabel('p');ylabel('H');信道容量实验程序:clc;closeall;clear;linwidd=1fontt=20p0=0;pd=1;N=20p=linspace(p0,pd,N);r=4c=log2(r)+(1-p).*log2(1-p)+p.*log2(p/(r-1));plot(p,c,'k');title('强对称信道容量数值模拟图');结果图2有噪信道编码--费诺不等式程序:结果图clc;closeall;clear;r=3;p0=0.00001;pd=0.99999;N=2000;p=linspace(p0,pd,N);q=1-p;H=-p.*log2(p)-q.*log2(q);holdonHH=H+p.*log2(r-1)title('费诺不等式示意图');boxonxlabel('PE');ylabel('H(X/Y)');plot(p,HH,'k:')holdonholdonfill([p,1],[HH,0],[0.6,0.6,0.6])stem((r-1)/r,1.59,'--.r')text(0.66,1.6,'最大值')香农编码程序:clc;clearall;closeall;p=[0.20.190.180.170.150.10.01];ifsum(p)1||sum(p)1error('输入概率不符合概率分布')end[pindex]=sort(p,'descend');n=length(p);pa=zeros(n,1);forii=2:npa(ii)=pa(ii-1)+p(ii-1);endk=ceil(-log2(p));%码字长度计算c=cell(1,n);%生成元胞数组,用来存不同长度的码字forii=1:nc{ii}='';tmp=pa(ii);forjj=1:k(ii)tmp=tmp*2;iftmp=1tmp=tmp-1;%c{ii}{jj}='1';c{ii}=[char(c{ii}),'1'];else%c{ii}{jj}='0';c{ii}=[char(c{ii}),'0'];endendendc(index)=c;%换回原来的顺序codelength=zeros(1,n);%码长初始化forii=1:nfprintf(['第',num2str(ii),'个消息对应为']);disp(c{ii});%显示码字codelength(ii)=length(c{ii});%endn_average=sum(codelength.*p)%平均码长fprintf('平均码长为');disp(n_average);H=-sum(p.*log2(p));fprintf('信源熵');disp(H);x=H/(n_average.*log2(2))fprintf('编码效率');disp(x);figureh=stem(1:n,codelength);%axis([0n+10n+1]);set(h,'MarkerFaceColor','blue','linewidth',2)实验结果结果图第1个消息对应为000第2个消息对应为001第3个消息对应为011第4个消息对应为100第5个消息对应为101第6个消息对应为1110第7个消息对应为1111110n_average=3.1400平均码长为3.1400信源熵2.6087x=0.8308编码效率0.83083费诺编码程序:endforrr=2:2:needgroupnum*2index2=index_aftergroup(rr,:);forii=index2(1):index2(2)c{ii}=[char(c{ii}),'1'];endendflag=0;index_p=[];forrr=1:needgroupnum*2indextmp=index_aftergroup(rr,:);if(indextmp(2)-indextmp(1)+11)flag=1;index_p=[index_p;indextmp];endendjj=jj+1;endc(index)=c;codelength=zeros(1,N);forii=1:Nfprintf(['第',num2str(ii),'个消息对应为']);disp(c{ii});codelength(ii)=length(c{ii});endn_average=sum(codelength.*p)fprintf('平均码长为');disp(n_average);H=-sum(p.*log2(p));fprintf('信源熵');disp(H);x=H/(n_average.*log2(2))fprintf('编码效率');disp(x);figureh=stem(1:N,codelength);axis([0N+10N+1]);set(h,'MarkerFaceColor','blue','linewidth',2)endfunctionindex_aftergroup=func_group(p,index_p)index=index_p(1):index_p(2);n=length(index);p0=p(index);sump0=sum(p0);half_sump0=sump0/2;forii=1:n-1tmpsum=sum(p0(1:ii));ifabs(tmpsum-half_sump0)=abs(tmpsum-half_sump0+p0(ii+1))index_aftergroup=[index(1)index(ii);index(ii+1)index(n)];break;endendend实验结果结果图第1个消息对应为00第2个消息对应为010第3个消息对应为011第4个消息对应为10第5个消息对应为110第6个消息对应为1110第7个消息对应为1111n_average=2.7400平均码长为2.7400信源熵2.6087x=0.9521编码效率0.9521霍夫曼编码程序:clc;clearall;closeall;A=[0.40.20.20.10.1];A=sort(A,'descend');T=A;[m,n]=size(A);B=zeros(n,n-1);B(:,1)=T;r=B(n,1)+B(n-1,1);T(n-1)=r;T(n)=0;T=sort(T,'descend');t=n-1;forj=2:n-1B(1:t,j)=T(1:t);K=find(T==r);%B(n,j)=K(end);B(n,j)=K(1);r=(B(t-1,j)+B(t,j));T(t-1)=r;T(t)=0;T=sort(T,'descend');t=t-1;endB;ENDc1=sym('[1,0]');ENDc=ENDc1;t=3;d=1;forj=n-2:-1:1fori=1:t-2ifi1&&B(i,j)==B(i-1,j)4d=d+1;elsed=1;endB(B(n,j+1),j+1)=-1;temp=B(:,j+1);x=find(temp==B(i,j));ENDc(i)=ENDc1(x(d));endy=B(n,j+1);ENDc(t-1)=[char(ENDc1(y)),'1'];ENDc(t)=[char(ENDc1(y)),'0'];t=t+1;ENDc1=ENDc;endA%排序后的原概率序列ENDc%编码结果fori=1:n[a,b]=size(char(ENDc(i)));L(i)=b;endavlen=sum(L.*A)%平均码长selen=(L-avlen).^2%?mselen=sum((selen).*A)%码长均方差H=-A*(log2(A'))%?P=H/avlen%?figure;subplot(2,1,1)h=stem(1:n,selen);%axis([0n+10max(selen)+0.1]);set(h,'MarkerFaceColor','blue','linewidth',2)xlabel('信源向上排');ylabel('方差值selen');holdonplot(0:n+1,mselen*ones(1,n+2),'r','linewidth',2);holdofflegend('每个信源码长与平均码长的方差','码长均方差');A=[0.40.20.20.10.1];A=sort(A,'descend');T=A;[m,n]=size(A);B=zeros(n,n-1);B(:,1)=T;r=B(n,1)+B(n-1,1);T(n-1)=r;T(n)=0;T=sort(T,'descend');t=n-1;forj=2:n-1B(1:t,j)=T(1:t);K=find(T==r);B(n,j)=K(end);%B(n,j)=K(1);r=(B(t-1,j)+B(t,j));T(t-1)=r;T(t)=0;T=sort(T,'descend');t=t-1;endB;ENDc1=sym('[1,0]');ENDc=ENDc1;t=3;d=1;forj=n-2:-1:1fori=1:t-2ifi1&&B(i,j)==B(i-1,j)d=d+1;elsed=1;endB(B(n,j+1),j+1)=-1;temp=B(:,j+1);x=find(temp==B(i,j));ENDc(i)=ENDc1(x(d));endy=B(n,j+1);ENDc(t-1)=[char(ENDc1(y)),'1'];ENDc(t)=[char(ENDc1(y)),'0'];t=t+1;ENDc1=ENDc;endA%排序后的原概率序列ENDc%编码结果fori=1:n[a,b]=size(char(ENDc(i)));L(i)=b;endavlen=sum(L.*A)%平均码长selen=(L-avlen).^2%?mselen=sum((selen).*A)%码长均方差H=-A*(log2(A'))%?P=H/avlen%?subplot(2,1,2)h=stem(1:n,selen);%axis([0n+10max(selen)+0.1]);set(h,'MarkerFaceColor','blue','linewidth',2)xlabel('信源向下排');ylabel('方差值selen');holdonplot(0:n+1,mselen*ones(1,n+2),'r','linewidth',2);holdofflegend('每个码长与平均码长的方差','码长均方差');实验结果A=0.40000.20000.20000.10000.1000ENDc=[11,1,0,101,100]avlen=1.8000selen=0.04000.64000.64001.44001.4400mselen=0.5600H=2.1219P=1.1788A=0.40000.20000.20000.10000.1000ENDc=[0,10,111,1101,1100]avlen=2.2000selen=1.44000.04000.64003.24003.2400mselen=1.3600H=2.1219P=0.9645
本文标题:信息论与编码实验程序与结果图(matlab)
链接地址:https://www.777doc.com/doc-6392324 .html