LTTT Cau 3
LTTT Cau 3
clc;
clear all;
close all;
disp('String: Huynh Thanh Duoc Lam Thanh Hao Nguyen Cong Hau Duong Quoc Huy Tang Minh Man Le
Hong Phu Dai hoc Ton Duc Thang Ly Thuyet Thong Tin');
ss=[26 16 15 10 9 9 9 7 5 5 4 3 3 3 3 1 1];
disp('summary: space=26 h:15 n:15 u:10 a:9 o:9 t:9 g:7 c:5 y:5 d:4 e:3 i:3 l:3 m:3 p:1 q:1')
%outputs = string of codewords,average codeword length
ss=ss./sum(ss); %if occurrences are inputted, probabilities are gained
ss=sort(ss,'descend'); %the probabilities are sorted in descending order
sf=0;
fano=0;
%initializations for Pk
for i=1:length(ss)
Hx=Hx+ ss(i)*log2(1/ss(i)); %solving for entropy
end
for k=1:length(ss)
info(k)=-(log2(ss(k))); %Information
end
for j=1:length(ss)-1
fano=fano+ss(j);
sf=[sf 0]+[zeros(1,j) fano]; %solving for Information for every codeword
siling=[siling 0]+[zeros(1,j) ceil(log2(1/ss(j+1)))]; %solving for length every codeword
end
for r=1:length(sf)
esf=sf(r);
for p=1:siling(r)
esf=mod(esf,1)*2;
h(p)=esf-mod(esf,1); %converting Pk into a binary number
end
hh(r)=h(1)*10^(siling(r)-1); %initializtion for making the binary a whole number
for t=2:siling(r)
hh(r)=hh(r)+h(t)*10^(siling(r)-t); %making the binary a whole number
end %e.g. 0.1101 ==> 1101
end
c={'0','1'};
disp('Codeword');
for i=1:length(hh)
u=1; %converting the codes into a string
for t=siling(i):-1:1
f=floor(hh(i)/10^(t-1)); %1001 ==>1 (getting the first highest unit of a number)
hh(i)=mod(hh(i),10^(t-1)); %1001 ==>001(eliminating the first highest unit of a number)
if f==1
if u==1
d=c{2}; %conversion part (num(1001) to str(1001))
else
d=[d c{2}];
end
else
if u==1
d=c{1};
else
d=[d c{1}];
end
end
codex{i,:}={d};
u=u+1;
end
disp([d])
end
disp(B)
disp(['Entropy H(X) = ',num2str(Hx),'bits/symbol'])
disp(['Average length,L = ',num2str(T),'bits/symbol'])
redu=100-eff; %Redundancy
disp(['Redundancy=',num2str(redu),'%'])