Chapter 2 Fundamental Limit On Performance

Example 2.1 page 18

In [20]:
from numpy import arange, zeros
from math import log
%matplotlib inline
from matplotlib.pyplot import plot,xlabel,ylabel,title,show

Po = arange(0,1+0.01,0.01)
H_Po = zeros(len(Po))
for i in range(1,len(Po)-1):
  H_Po[i]=-Po[(i)]*log(Po[(i)],2)-(1-Po[(i)])*log(1-Po[(i)],2)

#plot
#plot2d(Po,H_Po)
plot(Po,H_Po)
xlabel('Symbol Probability, Po')
ylabel('H(Po)')
title('Entropy function H(Po)')
show()

Example 2.2 page 19

In [1]:
from __future__ import division
from math import log
P0 = 1/4# #probability of source alphabet S0
P1 = 1/4# #probability of source alphabet S1
P2 = 1/2# #probability of source alphabet S2
H_Ruo = P0*log(1/P0,2)+P1*log(1/P1,2)+P2*log(1/P2,2)
print 'Entropy of Discrete Memoryless Source'
print 'bits : ',H_Ruo
#Second order Extension of discrete Memoryless source
P_sigma = [P0*P0,P0*P1,P0*P2,P1*P0,P1*P1,P1*P2,P2*P0,P2*P1,P2*P2]#
print 'Table 2.1 Alphabet Particulars of Second-order Extension of a Discrete Memoryless Source'
print '_________________________________________________________________________________'
print 'Sequence of Symbols of ruo2:'
print '  S0*S0     S0*S1     S0*S2     S1*S0     S1*S1    S1*S2    S2*S0     S2*S1  S2*S2'
print 'Probability p(sigma), i =0,1.....8 : ',P_sigma
print '_________________________________________________________________________________'
print '   '
H_Ruo_Square =0
for i in range(0,len(P_sigma)):
    H_Ruo_Square=(H_Ruo_Square+P_sigma[i]*log(1/P_sigma[i],2))
print 'H(Ruo_Square)=', H_Ruo_Square,' bits'
print 'H(Ruo_Square) = 2*H(Ruo)'
Entropy of Discrete Memoryless Source
bits :  1.5
Table 2.1 Alphabet Particulars of Second-order Extension of a Discrete Memoryless Source
_________________________________________________________________________________
Sequence of Symbols of ruo2:
  S0*S0     S0*S1     S0*S2     S1*S0     S1*S1    S1*S2    S2*S0     S2*S1  S2*S2
Probability p(sigma), i =0,1.....8 :  [0.0625, 0.0625, 0.125, 0.0625, 0.0625, 0.125, 0.125, 0.125, 0.25]
_________________________________________________________________________________
   
H(Ruo_Square)= 3.0  bits
H(Ruo_Square) = 2*H(Ruo)

Example 2.3 page 20

In [2]:
from __future__ import division
from math import log

# (a)Average code-word length 'L'
#(b)Entropy 'H'
P0 = 0.4# #probability of codeword '00'
L0 = 2#   #length of codeword S0
P1 = 0.2# #probability of codeword '10'
L1 = 2#   #length of codeword S1
P2 = 0.2# #probility of codeword '11'
L2 = 2#   #length of codeword S2
P3 = 0.1# #probility of codeword '010'
L3 = 3#   #length of codeword S3
P4 =0.1# #probility of codeword '011'
L4 = 3#   #length of codeword S4
L = P0*L0+P1*L1+P2*L2+P3*L3+P4*L4#
H_Ruo = P0*log(1/P0,2)+P1*log(1/P1,2)+P2*log(1/P2,2)+P3*log(1/P3,2)+P4*log(1/P4,2)
print 'Average code-word Length L',L,'bits'
print 'Entropy of Huffman coding result H %0.2f'%H_Ruo, 'bits'
print 'Average code-word length L exceeds the entropy H(Ruo) by only %0.3f'%(((L-H_Ruo)/H_Ruo)*100),'%'
sigma_1 = P0*(L0-L)**2+P1*(L1-L)**2+P2*(L2-L)**2+P3*(L3-L)**2+P4*(L4-L)**2
print 'Varinace of Huffman code : ',sigma_1
Average code-word Length L 2.2 bits
Entropy of Huffman coding result H 2.12 bits
Average code-word length L exceeds the entropy H(Ruo) by only 3.679 %
Varinace of Huffman code :  0.16

Example2.4 page 20

In [3]:
from __future__ import division
from math import log

def log2(x):
    return log(x,2)

# Calculation of (a)Average code-word length 'L' (b)Entropy 'H'
P0 = 0.4# #probability of codeword '1'
L0 = 1#   #length of codeword S0
P1 = 0.2# #probability of codeword '01'
L1 = 2#   #length of codeword S1
P2 = 0.2# #probility of codeword '000'
L2 = 3#   #length of codeword S2
P3 = 0.1# #probility of codeword '0010'
L3 = 4#   #length of codeword S3
P4 =0.1# #probility of codeword '0011'
L4 = 4#   #length of codeword S4
L = P0*L0+P1*L1+P2*L2+P3*L3+P4*L4#
H_Ruo = P0*log2(1/P0)+P1*log2(1/P1)+P2*log2(1/P2)+P3*log2(1/P3)+P4*log2(1/P4)#
print 'Average code-word Length L :',L,'bits'
print 'Entropy of Huffman coding result H : %0.2f bits'%H_Ruo
sigma_2 = P0*(L0-L)**2+P1*(L1-L)**2+P2*(L2-L)**2+P3*(L3-L)**2+P4*(L4-L)**2
print 'Varinace of Huffman code = %0.2f'%sigma_2
Average code-word Length L : 2.2 bits
Entropy of Huffman coding result H : 2.12 bits
Varinace of Huffman code = 1.36

Example2.5 page 20

In [32]:
p = 0.4# #probability of correct reception
pe = 1-p##probility of error reception (i.e)transition probility
print 'probility of 0 receiving if a 0 is sent = probility of 1 receiving if a 1 is sent=',p
print 'Transition probility'
print 'probility of 0 receiving if a 1 is sent = probility of 1 receiving if a 0 is sent=',pe
probility of 0 receiving if a 0 is sent = probility of 1 receiving if a 1 is sent= 0.4
Transition probility
probility of 0 receiving if a 1 is sent = probility of 1 receiving if a 0 is sent= 0.6

Example2.6 page 21

In [1]:
from numpy import arange
from math import log
%matplotlib inline
from matplotlib.pyplot import plot,title,show,xlabel,ylabel



def log2(x):
    return log(x,2)


p = arange(0,0.5+0.01,0.01)
C=[]
for i in range(0,len(p)):
    if(i!=0):
        C.append(1+p[i]*log2(p[i])+(1-p[i])*log2((1-p[i])))
    elif(i==0):
        C.append(1)
    elif(i==len(p)):
        C.append(0)
  

plot(p,C)
xlabel('Transition Probility, p')
ylabel('Channel Capacity, C')
title('Figure 2.10 Variation of channel capacity of a binary symmetric channel with transition probility p')
show()

Example2.7 page 21

In [1]:
from __future__ import division
%matplotlib inline
from matplotlib.pyplot import plot,title,show,xlabel,ylabel,legend


#Average Probility of Error of Repetition Code
p =10**-2#
pe_1 =p# #Average Probility of error for code rate r = 1
pe_3 = 3*p**2*(1-p)+p**3##probility of error for code rate r =1/3
pe_5 = 10*p**3*(1-p)**2+5*p**4*(1-p)+p**5##error for code rate r =1/5
pe_7 = ((7*6*5)/(1*2*3))*p**4*(1-p)**3+(42/2)*p**5*(1-p)**2+7*p**6*(1-p)+p**7##error for code rate r =1/7
r = [1,1/3,1/5,1/7]#
pe = [pe_1,pe_3,pe_5,pe_7]#
plot(r,pe)
xlabel('Code rate, r')
ylabel('Average Probability of error, Pe')
title('Figure 2.12 Illustrating significance of the channel coding theorem')
#xgrid(1)
show()
print 'Table 2.3 Average Probility of Error for Repetition Code'
print '_______________________________________________________________'
print 'Average Probility of Error, Pe ='
for pp in pe: print pp
print 'Code Rate, r =1/n = '
for rr in r:print '%0.2f'%rr
print '_______________________________________________________________'
Table 2.3 Average Probility of Error for Repetition Code
_______________________________________________________________
Average Probility of Error, Pe =
0.01
0.000298
9.8506e-06
3.416698e-07
Code Rate, r =1/n = 
1.00
0.33
0.20
0.14
_______________________________________________________________