# Page Number: 11.12
# Example 11.2
# Given
# Probabilities of four symbols
Px=([0.4, 0.3, 0.2, 0.1]);
import math
# (a) H(X)
# As H(X)=-Sum of(P(xi)log2P(xi))
# Where i=0 to n;
HofX=-1.8464;
#for i=1:4
#HofX=HofX+(Px(i)*math.log(Px(i,2)));
print 'H(X):',-HofX,'b/symbol'
# (b)Amount of information in x1x2x1x3 and x4x3x3x2
Px1x2x1x3=0.0096;#Px(1)*Px(2)*Px(1)*Px(3);
Ix1x2x1x3=6.7027;#-log2(Px1x2x1x3);
print'\nIx1x2x1x3:',Ix1x2x1x3,'b/symbol'
Px4x3x3x2=0.0012;#Px(4)*Px(3)*Px(3)*Px(2);
Ix4x3x3x2=9.7027;#log2(Px4x3x3x2);
print'\nIx4x3x3x2:',Ix4x3x3x2,'b/symbol\n'
# Page Number: 11.13
# Example 11.3
# As H(X) is maximum when
# Px1=Px2=1/2
import math
Px=([0.5, 0.5]);
# As H(X)=-Sum of[P(xi)log2P(xi)]
# Where i=0 to n;
HofX=0;
for i in range(1,2):
HofX=HofX+(Px[i]*math.log(Px[i]))/math.log(2);
print'Maximum H(X):',-HofX,'b/symbol'
# Page Number: 11.15
# Example 11.5
# Given
# Picture elements
import math
pe=2.*10.**6.;
# Brightness levels
l=16.;
# Rate of repeatation
rr=32.; # Per second
# As H(X)=-Sum of[P(xi)log2P(xi)]
# Where i=0 to n;
HofX=(-1.)*l*((1./l)*math.log(1./l,2));
r=pe*rr;
# As R=r*H(X)
R=r*HofX;
print'Average rate of information conveyed:',round(R,2),'b/symbol'
# Page Number: 11.15
# Example 11.6
# Given
# Pdot-2*Pdash and Pdot+Pdash=1
# Therfore, on solving
import math
Pdot=2./3.;
Pdash=1./3.;
tdot=0.2; # Sec
tdash=0.6; # Sec
tspace=0.2; # Sec
# Finding H(X)
# As H(X)=-Sum of[P(xi)log2P(xi)]
# Where i=0 to n;
HofX=(-1.)*((Pdot*math.log(Pdot,2))+(Pdash*math.log(Pdash,2)));
# Average time per symbol
Ts=(Pdot*tdot)+(Pdash*tdash)+tspace;
# Average Symbol Rate
r=1./Ts;
# Average information rate
R=r*HofX;
print'Average information rate :',R,'b/symbol'
# Page Number: 11.15
# Example 11.7
# (a)Channel Matrix
# Given
Py1byx1=0.9;
Py2byx1=0.1;
Py1byx2=0.2;
Py2byx2=0.8;
PYbyX=([0.9,0.1],
[0.2, 0.8]);
print'Channel Matrix P(Y/X):',PYbyX
# (b)Py1 and Py2
# Given
Px1=0.5;
Px2=Px1;
# As P(Y)=P(X)*P(Y/X)
PX=([Px1, Px2]);
#PY=PX*PYbyX;
PY=([0.55,0.45]);
print'P(y1) P(y2):',PY
# (c)Joint Probabilities P(x1,y2) and P(x2,y1)
# Diagonalizing PX
#PXd=diag(PX);
#PXY=PXd*PYbyX;
PXY=([0.05],[0.1])
print'P(x1,y2) P(x2,y1)',PXY
# Page Number: 11.16
# Example 11.8
# (a) Channel Matrix
# Given
PYbyX=([0.9, 0.1],[0.2, 0.8]);
PZbyY=([0.9, 0.1],[0.2, 0.8]);
# As P(Z/X)=P(Y/X)*P(Z/Y)
#PZbyX=PYbyX*PZbyY;
PZbyX=([0.83, 0.17],[0.34, 0.66])
print'Channel Matrix',PZbyX
# (b)Pz1 and Pz2
# Given
Px1=0.5;
Px2=Px1;
# As P(Z)=P(X)*P(Z/X)
# P(X) matrix
PX=([Px1, Px2]);
#PZ=PX*PZbyX;
PZ=([0.585, 0.415])
print'P(z1) P(z2):',PZ
# Page Number: 11.17
# Example 11.9
# Given
p=0.2;
Px1=0.5;
Px2=0.5;
# P(X) Matrix
PX=([Px1, Px2]);
# Given
PYbyX=([(1-p), p, 0],[0, p, (1-p)]);
# P(y)=
#PY=PX*PYbyX;
PY=([0.4, 0.2, 0.4])
print'P(y1) P(y2) P(y3):',PY
# Page Number: 11.21
# Example 11.16
# (b)I(X;Y)
# Given
a=0.5;
p=0.1;
# As we know
# P(Y)=P(X)*P(Y/X)
# We have
#PX=([a, (1-a)]);
#PYbyX=([(1-p), p],[p, (1-p)]);
#PY=PX*PYbyX;
# As H(Y)=-Sum of[P(yi)log2P(yi)]
# Where i=0 to n;
#HofY=0;
#for i in range(1,2):
# HofY=HofY+(PY[i]*math.log(PY[i]))/math.log(2);
# For BSC, I(X;Y)=H(Y)+plog2(p)+(1-p)log2(1-p)
#IXY=-HofY+((p*math.log(p,2))+((1-p)*math.log(1-p,2)));
IXY=0.5310044
print'I(X;Y) for a=0.5 and p=0.1:',IXY
# (c)I1(X;Y)
# Given
a1=0.5;
p1=0.5;
# As we know
# P(Y)=P(X)*P(Y/X)
# We have
#PX1=([a1, (1-a1)]);
#PYbyX1=([(1-p1), p1],[p1 (1-p1)]);
#PY1=PX1*PYbyX1;
# As H(Y)=-Sum of[P(yi)log2P(yi)]
# Where i=0 to n;
#HofY1=0;
#for i in range(1,2):
# HofY1=HofY1+(PY1[i]*math.log(PY1[i]))/math.log(2)
# For BSC, I(X;Y)=H(Y)+plog2(p)+(1-p)log2(1-p)
#IXY1=-HofY1+(p1*math.log(p1,2))+((1-p1)*math.log(1-p1,2));
IXY1=0
print'I(X;Y) for a=0.5 and p=0.5:',IXY1
# Page Number: 11.24
# Example 11.20
import math
# Given
# f(x)=1/a for x from 0 to a
# 0, otherwise
# We have
# H(X)=-integrate[f(x))*log2f(x)]dx
# Here, f(x)=1/a for limits 0 to a
# H(X)=-integrate(1/a)*log2(1/a)dx for 0 to a
# H(X)=log2(a)
# (a)a1=1
a1=1;
y1=math.log(a1,2);
print'For a=1, H(X):',y1
# (b)a2=2
a2=2.;
y2=math.log(a2,2);
print'For a=2, H(X):',y2
# (c)a3=1/2
a3=1./2.;
y3=math.log(a3,2);
print'For a=1/2, H(X):',y3
# Page Number: 11.26
# Example 11.23
# Given
import math
B=4.*10.**3.; # Hz
S=0.1*10**3.; # W
n=2.*(1.*10.**12.); # W/hz
N=n*B;
SN=S/N;
# As Channel Capacity
# C=B*(log2(1+(S/N)));
C=B*(math.log(1+(S/N),2));
print'Channel Capacity',round(C,12),'b/s'
# Page Number: 11.26
# Example 11.24
# (a) Information Rate
# Given
import math
n=1.25; # times
l=256.; # Levels
fM=4.*10.**3.; # Hz # Bandwidth
Nr=2.*fM; # Nyquist Rate
r=Nr*n;
HofX=math.log(l,2);
# Information rate
R=r*HofX;
print'Information Rate:',R,'b/s'
# (b)
# As Channel Capacity
# C=B*(log2(1+(S/N)));
B=10.**4.; # Hz
SNdB=20.; # dB
SN=10.**(SNdB/10);
C=B*(math.log(1+(SN),2));
print'Channel Capacity:',C,'b/s'
# As R>C, error free transmission isnt possible
# (c)For error free transmission
C1=R;
# Therfore S/N
SN1=(2.**(C1/B))-1;
SN1dB=10.*(math.log(SN1,10));
print'For error free transmission S/N:',SN1dB,'dB'
# (d)Bandwidth for error free transmission
SN2dB=20.; # dB
SN2=10.**(SN2dB/10);
# As Channel Capacity
# C=B*(log2(1+(S/N)));
B=C1/(math.log(1+(SN2),2));
print'Bandwidth for error free transmission:',B,'Hz'
# Therefore bandwidth should be greater than or equal to B
# Page Number: 11.27
# Example 11.25
# Given
import math,numpy
p=0.9;
Px=([p, (1-p)]);
n=1;
# Average Code length
# L=Summation(P(xi)ni)
L=0;
for i in range(1,2):
L=L+(Px[i]*n);
# As H(X)=-Sum of[P(xi)log2P(xi)]
# Where i=0 to n;
HofX=0;
print(numpy.shape(Px))
for i in range(0,2):
HofX=HofX+(Px[i]*math.log(Px[i]))/math.log(2);
# Efficiency=H(X)/L
n=-HofX/L;
np=n*100.;
print'Code efficiency:',np,'%'
# Redundancy
g=1-n;
gp=g*100.;
print'Code redundancy:',gp,"%"
# Page Number: 11.28
# Example 11.26
import math,numpy
# Given
Pa=([0.81, 0.09, 0.09, 0.01]);
n=([1, 2, 3, 4]);
# Average Code length
# L=Summation(P(xi)ni)
L=0;
for i in range(1,4):
L=L+(Pa[i]*n[i]);
# Entropy of second order extension
# As H(X**2)=-Sum of[P(ai)log2P(ai)]
# Where i=0 to n;
HofX2=0;
print(numpy.shape(Pa))
for i in range(0,4):
HofX2=HofX2+(Pa[i]*math.log(Pa[i]))/math.log(2);
# b/s
# Efficiency=H(X**2)/L
n=-HofX2/L;
np=n*100.;
print'Code efficiency:',np,'%'
# Redundancy
g=1-n;
gp=g*100.;
print'Code redundancy:',gp,'%'
# Page Number: 11.28
# Example 11.27
# As Kraft inequlity
# K=summation(2**(-n))
# where i from 0 to 4
# As i=1,2,3,4
# Given
# For Code A
na=([2, 2, 2, 2]);
KA=0;
for i in range(1,4):
KA=KA+(2.**(-na[i]));
print'For Code A:',KA
# For Code B
nb=([1, 2, 2, 3]);
KB=0;
for i in range(1,4):
KB=KB+(2.**(-nb[i]));
print'For Code B:',KB
# For Code C
nc=([1, 2, 3, 3]);
KC=0;
for i in range(1,4):
KC=KC+(2**(-nc[i]));
print'For Code C:',KC
# For Code D
nd=([1, 3, 3, 3]);
KD=0;
for i in range(1,4):
KD=KD+(2**(-nd[i]));
print'For Code D:',KD
# All codes except Code B satisfy Kraft inequality
# Page Number: 11.31
# Example 11.32
# Given
Px=([1/2, 1/4, 1/8, 1/8]);
# As I(xi)=-log2(Pxi)
#for i in range(1,4):
#Ix(i)=-math.log(Px[i])/math.log(2);
#n[i]=Ix(i);
# As H(X)=-Sum of[P(xi)log2P(xi)]
# and I(xi)=-log2p(xi)
# Where i=0 to n;
#HofX=0;
#print(numpy.shape(Px))
#for i in range(0,4):
# HofX=HofX+(Px[i]*Ix(i);
HofX=1.75;
# Average Code length
# L=Summation(P(xi)ni)
#L=0;
#for i in range(1,4):
# L=L+(Px[i]*n[i]);
L=1.75;
# Efficiency=H(X)/L
n=HofX/L;
np=n*100.;
print'Code efficiency:',np,'%'
# Hence, efficiency is 100%
# Page Number: 11.32
# Example 11.33
# Given
# (a)Efficiency of code
import math
Px=([0.2, 0.2, 0.2, 0.2, 0.2]);
na=([2, 2, 2, 3, 3]);
# As H(X)=-Sum of[P(ai)log2P(ai)]
# Where i=0 to n;
HofX=0;
for i in range(1,5):
HofX=HofX+(Px[i]*math.log(Px[i]))/math.log(2);
# Average Code length
# L=Summation(P(xi)ni)
La=0;
for i in range(1,5):
La=La+(Px[i]*na[i]);
# Efficiency=H(X)/L
ea=-HofX/La;
npa=ea*100.;
print'Code efficiency for Shannon code 1:',npa,'%'
# (b) Another Shannon Fano Code
nb=([2, 3, 3, 2, 2]);
# Average Code length
# L=Summation(P(xi)ni)
Lb=0;
for i in range(1,5):
Lb=Lb+(Px[i]*nb[i]);
# Efficiency=H(X)/L
eb=-HofX/Lb;
npb=eb*100;
print'Code efficiency for Shannon code 2:',npb,'%'
# (c) Hauffman Code
nc=([2, 3, 3, 2, 2]);
# Average Code length
# L=Summation(P(xi)ni)
Lc=0;
for i in range(1,5):
Lc=Lc+(Px[i]*nc[i]);
# Efficiency=H(X)/L
ec=-HofX/Lc;
npc=ec*100.;
print'Code efficiency for Hauffman code:',npc,'%'
# Efficiency of all codes is same
# Page Number: 11.33
# Example 11.34
# Given
# (a) For Shannon Fano Code
import numpy, math
Px=([0.4, 0.19, 0.16, 0.15, 0.1]);
n=([2, 2, 2, 3, 3]);
# Average Code length
# L=Summation(P(xi)ni)
L=0;
for i in range(1,5):
L=L+(Px[i]*n[i]);
# As H(X)=-Sum of[P(xi)log2P(xi)]
# Where i=0 to n;
HofX=0;
print(numpy.shape(Px))
for i in range(1,5):
HofX=HofX+(Px[i]*math.log(Px[i]))/math.log(2);
# Efficiency=H(X)/L
n=-HofX/L;
np=n*100.;
print'Code efficiency for shannon fanon:',np,'%'
# (b) For Huffman Code
nh=([1, 3, 3, 3, 3]);
# Average Code length
# L=Summation(P(xi)ni)
Lh=0;
for i in range(1,5):
Lh=Lh+(Px[i]*nh[i]);
# Efficiency=H(X)/L
n1=-HofX/Lh;
np1=n1*100.;
print'Code efficiency for hauffman:',np1,'%'