06.06.2022 Views

B. P. Lathi, Zhi Ding - Modern Digital and Analog Communication Systems-Oxford University Press (2009)

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

790 INTRODUCTION TO INFORMATION THEORY

function [huffcode,n] =huf fmancode (p) ;

% input p is a probability vector consisting of

% probabi lities of source symbols x_i

if min (p) <O,

error ('Negative element cannot be in a probability vector')

return

else if abs (sum (p) -1)>1.e-12 ,

error ('Sum of input probability is not l')

return

end

[psort ,pord] =sort (p) ;

n=length (p) ;

q=p ;

for i=l :n-1

[q, l] =sort (q) ;

m ( i , : ) = [ 1 ( 1 : n - i + 1) , zeros ( 1 , i-1 ) J ;

q= [q(l)+q(2) ,q(3:end) ,1];

end

Cword=blanks (n-2) ;

Cword(n )='O';

Cword(2*n) ='l';

for il=l :n-2

Ctemp=Cword;

idxO=find (m(n- il, :)==l) *n;

Cword (l:n )=[Ctemp (idx0-n+2 :idx0 ) '0'];

Cword (n+l :2*n)= [Cword (l:n-1) '1'];

for i2=2 :il+l

idx2=find (m(n-il, :)==i2) ;

Cword (i2*n+l : (i2+l) *n) =Ctemp (n* (idx2-1)+ 1:n *idx2 ) ;

end

end

for i=l :n

idxl=find (m(l, :)==i) ;

huffcode (i,1 :n) =Cword (n* ( idxl-1)+1:idxl*n) ;

end

end

The second program, huf fmanEx . m, generates a very simple example of Huffman encoding. In

this exercise, we provide an input probability vector of length 8. The MATLAB program huf fmanEx . m

will generate the list of codewords for all the input symbols. The entropy of this source H (x) is computed

and compared against the average Huffman codeword length. Their ratio shows the efficiency of the

code.

% Matlab Program <huffmanEx .m>

% This exercise requires the input of a

% probability vector p that list all the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!