ع نظرية 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

%Huffman code

clc
clear all
close all

p=[0.4 0.18 0.1 0.1 0.07 0.06 0.05 0.04];


p=sort(p,'descend');
disp(['p(x)=' num2str(p)])

[li,l]=huffmandict(1:length(p),p);

%the codeword
for i=1:length(p)
disp(['codeword(' num2str(i) ')=' num2str(li{i,2}(1,:))])
end

%the length codeword


for i=1:length(p)
disp(['li(' num2str(i) ')=' num2str(length(li{i,2}(1,:)))])
end

%average codeword
disp(['L=' num2str(l) ' bits/symbol'])

%source entropy
h=0;
for i=1:length(p)
h=h-p(i)*log2(p(i));
end
disp(['H(x)=' num2str(h) ' bits/symbol'])

%code efficiency
n=(h/l)*100;
disp(['n=' num2str(n) '%'])

%0i
oi=zeros(1,length(p));
for i=1:length(p)
for j=1:length(li{i,2}(1,:))
if li{i,2}(1,j)==0
oi(i)=oi(i)+1;
end
end
end
disp(['0i=' num2str(oi)])

%p(0)
p0=(sum(oi.*p))/l;
disp(['p(0)=' num2str(p0)])

1
%binary shannon code
clc
clear all
close all

p=[0.3 0.2 0.15 0.12 0.1 0.08 0.05];


p=sort(p,'descend');
disp(['p(x)= ' num2str(p)])

%the codeword length


li=ceil(-log2(p));
disp(['li = ' num2str(li)])

%the average codeword length


l=0;
for i=1:length(p)
l=l+li(i)*p(i);
end
disp(['Lc = ' num2str(l) ' bits/symbol'])

%the source entropy


h=0;
for i=1:length(p)
h=h-p(i)*log2(p(i));
end
disp(['H(x)=' num2str(h) ' bits/symbol'])

%code efficiency
n=(h/l)*100;
disp(['n=' num2str(n) '%'])

%fi
fi=zeros(1,length(p));
for i=1:length(p)-1
fi(i+1)=fi(i)+p(i);
end
disp(['Fi=' num2str(fi)])

%the codeword
ci=cell(1,length(p));
for i=1:length(li)
ci(i)={[zeros(1,li(i))]};
end
for i=1:length(li)
di=fi(i);
for j=1:li(i)
if (di*2)>=1
ci{i}(:,j)=floor(di*2);
di=(di*2)-1;
else
ci{i}(:,j)=floor(di*2);
di=di*2;
end
end
end

2
for i=1:length(ci)
disp(['codeword(' num2str(i) ')=' sprintf('%d ',ci{i})])
end

%0i
oi=zeros(1,length(p));
for i=1:length(li)
for j=1:li(i)
if ci{i}(:,j)==0
oi(i)=oi(i)+1;
end
end
end
disp(['0i=' num2str(oi)])

%p(0)
p0=(sum(oi.*p))/l;
disp(['p(0)=' num2str(p0)])

%ternary shannon code


clc
clear all
close all

p=[0.3 0.2 0.15 0.12 0.1 0.08 0.05];


p=sort(p,'descend');
disp(['p(x)= ' num2str(p)])

%the codeword length


li=ceil(-log(p)./log(3));
disp(['li = ' num2str(li)])

%the average codeword length


l=0;
for i=1:length(p)
l=l+li(i)*p(i);
end
disp(['Lc = ' num2str(l) ' bits/symbol'])

%the source entropy


h=0;
for i=1:length(p)
h=h-p(i)*(log(p(i))/log(3));
end
disp(['H(x)=' num2str(h) ' bits/symbol'])

%code efficiency
n=(h/l)*100;
disp(['n=' num2str(n) '%'])

%fi
fi=zeros(1,length(p));
for i=1:length(p)-1
fi(i+1)=fi(i)+p(i);
end
disp(['Fi=' num2str(fi)])

3
%the codeword
ci=cell(1,length(p));
for i=1:length(li)
ci(i)={[zeros(1,li(i))]};
end
for i=1:length(li)
di=fi(i);
for j=1:li(i)
if ((di*3)>=1)&&((di*3)<2)
ci{i}(:,j)=floor(di*3);
di=(di*3)-1;
elseif (di*3)>=2
ci{i}(:,j)=floor(di*3);
di=(di*3)-2;
else
ci{i}(:,j)=floor(di*3);
di=di*3;
end
end
end

for i=1:length(ci)
disp(['codeword(' num2str(i) ')=' sprintf('%d ',ci{i})])
end

%0i
oi=zeros(1,length(p));
for i=1:length(li)
for j=1:li(i)
if ci{i}(:,j)==0
oi(i)=oi(i)+1;
end
end
end
disp(['0i=' num2str(oi)])

%p(0)
p0=(sum(oi.*p))/l;
disp(['p(0)=' num2str(p0)])

You might also like