# Chapter 13 : Information Theory and Coding¶

## Example 13.1 Page No : 631¶

In :
import math

#Given probabilities p1  =  p4  =  1/8 & p2  =  p3  =  3/8
p1  =  1./8
p4  =  1./8
p2  =  3./8
p3  =  3./8

#The average information H is p1*math.log2 (1/p1)+p2*math.log2 (1/p2)+p3*math.log2 (1/p3)+p4*math.log2 (1/p4) bits/message
H  =  p1*math.log (1/p1,2)+p2*math.log (1/p2,2)+p3*math.log (1/p3,2)+p4*math.log(1/p4,2)

#information rate R is r*H bits/sec where r is 2*B
#R1  =  R/B
R1  =  2*H

print 'The average information is ',round(H,4),' bits/message'
print 'Information rate ',round(R1,4),'*B bits/sec'

The average information is  1.8113  bits/message
Information rate  3.6226 *B bits/sec


## Example 13.4 Page No : 649¶

In :
import math

#Given bandwidth(B)  =  4000Hertz & Noise PSD(n/2)  =  10**-9 Watt/Hertz
B  =  4000.
n  =  2*10.**-9

#Chanel capacity(C)  =  B*math.log2 (1+S/(n*B))

#case 1
#Signal energy(S)  =   0.1Joule
S  =  0.1

C  =  B*math.log (1+S/(n*B),2)

print 'Channel capacity for bandwidth  =  4000Hertz, Noise PSD  =  10**-9 Watt/Hertz & Signal energy(S)  \
\n=   0.1Joule is ',C,' bits/sec'

#case 2
#Signal energy(S)  =   0.001Joule
S  =  0.001

C  =  B*math.log (1+S/(n*B),2)

print 'Channel capacity for bandwidth  =  4000Hertz, Noise PSD  =  10**-9 Watt/Hertz & Signal energy(S)  =   \
\n0.001Joule is ',round(C,4),' bits/sec'

#case 3
#Signal energy(S)  =   0.001Joule & incresed bandwidth(B)  =  10000Hertz
B  =  10000.
S  =  0.001

C  =  B*math.log (1+S/(n*B),2)

print 'Channel capacity for bandwidth  =  10000Hertz, Noise PSD  =  10**-9 Watt/Hertz & Signal energy(S)  \
\n=   0.001Joule is ',round(C,4),' bits/sec'

Channel capacity for bandwidth  =  4000Hertz, Noise PSD  =  10**-9 Watt/Hertz & Signal energy(S)
=   0.1Joule is  54439.0235417  bits/sec
Channel capacity for bandwidth  =  4000Hertz, Noise PSD  =  10**-9 Watt/Hertz & Signal energy(S)  =
0.001Joule is  27909.1197  bits/sec
Channel capacity for bandwidth  =  10000Hertz, Noise PSD  =  10**-9 Watt/Hertz & Signal energy(S)
=   0.001Joule is  56724.2534  bits/sec


## Example 13.8 Page No : 675¶

In :
#With math.single parity bit added, the code size  =  4. An error evades parity check if any 2 or all symbols ofthe code arrives are erroneous.
#Probability of any symbol from n are erroneous  =  nCm*(p**m)*(1-p)**(n-m)

#Thus, the probability of error undetected, P_undeterr  =  (4C2*(p**2)*(1-p)**2)+4C4*(p**4)  =  6*(p**2)*(1-p)**2)+(p**4)

#Probability of error in detection(P_undeterr1) for p  =  0.1
p  =  0.1
P_undeterr1  =  6*(p**2)*((1-p)**2)+(p**4)

print 'Probability of error in detection for p  =  0.1 is ',P_undeterr1

#Probability of error in detection(P_undeterr2) for p  =  0.01
p  =  0.01
P_undeterr2  =  6*(p**2)*((1-p)**2)+(p**4)

print 'Probability of error in detection for p  =  0.01 is ',round(P_undeterr2,4)

Probability of error in detection for p  =  0.1 is  0.0487
Probability of error in detection for p  =  0.01 is  0.0006


## Example 13.11 Page No : 696¶

In :
from numpy import zeros
import math

#The output equations are as follows v1  =  s1 xor s2 xor s3 & v2  =  s1 xor s3
#the no of bits in output mode(bits_out) is v*(L+K), v  =  no of outputs for commutatot  =  2, L  =  length of input  =  3 & K  =  no of memeory elements  =  3
v  =  2.
L  =  3.
K  =  3.
bits_out  =  v*(L+K)

#Taking in, s1, s2 , s3, v1 & v2 as row matrix where each column represents its corresponding input or output, in means input
in1  =  [0, 1, 0, 1, 0, 0, 0]

s1  =  zeros(7,dtype=int)
s2  =  zeros(7,dtype=int)
s3  =  zeros(7,dtype=int)
v1  =  zeros(7,dtype=int)
v2  =  zeros(7,dtype=int)

for i  in range(1,7):
s3[i]  =  s2[i-1]
s2[i]  =  s1[i-1]
s1[i]  =  in1[i-1]
v1[i-1]  =  s1[i]^(s2[i]^s3[i])
v2[i-1]  =  (s1[i]^s3[i])

#Output matrix is out
out  =  zeros(12)
for i  in range(0,11,2):
out[i]  =  v1[(i+3)/2]
out[i+1]  =  v2[(i+3)/2]

print ('output is'),
print (out)

output is [ 1.  1.  1.  0.  0.  0.  1.  0.  1.  1.  0.  0.]


## Example 13.12 Page No : 697¶

In :
import math
from numpy import zeros,array

#The qeneratr matrix requires impulse response of the coder.
#This is the ourput generated when the initially reset coder is fed with a math.single 1.
#The no of bits in the output code  =  2(1+3)  =  8

#Taking in, s1, s2 , s3, v1 & v2 as row matrix where each column represents its corresponding input or output, in means input
in1  =  [0, 1, 0, 0, 0]

s1  =  zeros(5,dtype=int)
s2  =  zeros(5,dtype=int)
s3  =  zeros(5,dtype=int)
v1  =  zeros(5,dtype=int)
v2  =  zeros(5,dtype=int)

for i  in range(1,5):
s3[i]  =  s2[i-1]
s2[i]  =  s1[i-1]
s1[i]  =  in1[i-1]
v1[i-1]  =  s1[i]^(s2[i]^s3[i])
v2[i-1]  =  (s1[i]^s3[i])

#Output matrix is out
out  =  zeros(8)
for i  in range(0,7,2):
out[i]  =  v1[(i+3)/2]
out[i+1]  =  v2[(i+3)/2]

print ('impulse response is'),
print (out)

#Then generator matrix is G
G  =  array([[1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0],[0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0],[0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0]])

#Note that, in G, impulse responses appear in staggered apper in a staggered manner in each row while the rest of the elements are 0.

#Now, output is b_o  =  b_i*G where input b_i  = [1 0 1]
b_i  =  array([, , ])

b_o  =  b_i*G
print b_o

#Here multiplication means Exor operation so whereever two occurs it should be changed to 1

for i  in range(12):
if  b_o[2,i] > 1:
b_o[2,i]  =  0;

print ('output is '),
print (b_o)
#print ('The output obtained is exactly same as example 13.1')

impulse response is [ 1.  1.  1.  0.  1.  1.  0.  0.]
[[1 1 1 0 1 1 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 1 1 1 0 1 1 0 0]]
output is  [[1 1 1 0 1 1 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 1 1 1 0 1 1 0 0]]


## Example 13.14 Page No : 701¶

In :
#Given, Tw  =  10microsec, BCH(1023973) code is used implies k as 973 & n as 1023, P_A  =  0.99, T1  =  40microsec & N  =  4
Tw  =  10.*10**-6
k  =  973.
n  =  1023.
P_A  =  0.99
T1  =  40.*10**-6
N  =  4.

#efficiency of Stop-and-Wait ARQ(n_SandW)  =  (k/n)*(P_A/(1+(T1/Tw)))
n_SandW  =  (k/n)*(P_A/(1+(T1/Tw)))

#efficiency of Go-Back-N ARQ(n_GBN)  =  (k/n)*(1/(1+(N*(1-P_A)/P_A)))
n_GBN  =  (k/n)*(1/(1+(N*(1-P_A)/P_A)))

#efficiency of Selective Repeat ARQ(n_SR)  =  (k/n)*P_A
n_SR  =  (k/n)*P_A

print 'efficiency of Stop-and-Wait ARQ is %.4f'%n_SandW
print 'efficiency of Go-Back-N ARQ is %.4f'%n_GBN
print 'efficiency of Selective Repeat ARQ is %.4f'%n_SR

efficiency of Stop-and-Wait ARQ is 0.1883
efficiency of Go-Back-N ARQ is 0.9142
efficiency of Selective Repeat ARQ is 0.9416


## Example 13.15 Page No : 718¶

In :
#Bit interval T  =  1/10**6  =  10**-6 sec
T  =  10.**-6

#White Noise Power Spectral Density n/2  =  10**-9 W/Hz
n  =  2.*10**-9

#Power required Ps  =  Eb/T, where Eb  =  energy per bit

#For information system feedback system Eb  =  n
Ps  =  n/T

print 'power required for information system feedback system is ',Ps,' Watt'

#For optimal system Ps  =  (0.69 * n)/T
Ps  =  (0.69 * n)/T;

print 'power required for optimal system is ',Ps,' Watt'

power required for information system feedback system is  0.002  Watt
power required for optimal system is  0.00138  Watt


## Example 13.16 Page No : 719¶

In :
#Given, Eb  =  10**-9Joule, n/2  =  10**-9 Watt/Hertz
Eb  =  10.**-8
n  =  2.*10**-9

#Probability of error for trellis-decoded modulation(Pe)  =  (1/2)*erfc(math.sqrt(1.5*Eb/n))
Pe  =  (1./2)*math.erfc(math.sqrt(1.5*Eb/n))

print 'Probability of error for trellis-decoded modulation is %.3e'%Pe

#Probability of error for Qpsk modulation(Pe)  =  (1/2)*erfc(math.sqrt(Eb/n))
Pe  =  (1./2)*math.erfc(math.sqrt(Eb/n))

print 'Probability of error for Qpsk modulation is %f'%Pe

Probability of error for trellis-decoded modulation is 5.376e-05
Probability of error for Qpsk modulation is 0.000783