Chapter 8 Error Control Coding

Example8.1 page 384

In [1]:
from __future__ import division
from numpy import ones,zeros,identity,transpose,hstack,mat

n =5# #block of identical 'n' bits
k =1# #one bit
m = 1## bit value = 1
I = identity(n-k) #Identity matrix
P = ones(n-k)##coefficient matrix
I=mat(I)
P=mat(P)
H = hstack([I,transpose(P)])##parity-check matrix
G = hstack([P, mat([1])])##generator matrix 
x = m*G# #code word
print 'generator matrix\n',G
print '\nparity-check matrix\n',H
print '\ncode word for binary one input\n',x
generator matrix
[[ 1.  1.  1.  1.  1.]]

parity-check matrix
[[ 1.  0.  0.  0.  1.]
 [ 0.  1.  0.  0.  1.]
 [ 0.  0.  1.  0.  1.]
 [ 0.  0.  0.  1.  1.]]

code word for binary one input
[[ 1.  1.  1.  1.  1.]]

Example8.2 page 386

In [1]:
from __future__ import division
from numpy import ones,zeros,identity,multiply,mat,concatenate,hstack,transpose


k = 4# #message bits length
n = 7# #block length
m = n-k##Number of parity bits
I = identity(k)  #identity matrix
I=mat(I)
print 'identity matrix Ik\n',I
P =[[1,1,0],[0,1,1],[1,1,1],[1,0,1]]##coefficient matrix
P=mat(P)
print '\ncoefficient matrix P\n',P
G = hstack([P,I]) #generator matrix
print 'generator matrix G\n',G

H = hstack([identity(k-1),transpose(P)])##parity check matrix
print 'parity chechk matrix H\n',H

#message bits
m = [[0,0,0,0],[0,0,0,1],[0,0,1,0],[0,0,1,1],[0,1,0,0],[0,1,0,1],[0,1,1,0],[0,1,1,1],[1,0,0,0],[1,0,0,1],[1,0,1,0],[1,0,1,1],[1,1,0,0],[1,1,0,1],[1,1,1,0],[1,1,1,1]]

C = m*G#
C = (C%2)#
print 'Code words of (7,4) Hamming code\n',C
identity matrix Ik
[[ 1.  0.  0.  0.]
 [ 0.  1.  0.  0.]
 [ 0.  0.  1.  0.]
 [ 0.  0.  0.  1.]]

coefficient matrix P
[[1 1 0]
 [0 1 1]
 [1 1 1]
 [1 0 1]]
generator matrix G
[[ 1.  1.  0.  1.  0.  0.  0.]
 [ 0.  1.  1.  0.  1.  0.  0.]
 [ 1.  1.  1.  0.  0.  1.  0.]
 [ 1.  0.  1.  0.  0.  0.  1.]]
parity chechk matrix H
[[ 1.  0.  0.  1.  0.  1.  1.]
 [ 0.  1.  0.  1.  1.  1.  0.]
 [ 0.  0.  1.  0.  1.  1.  1.]]
Code words of (7,4) Hamming code
[[ 0.  0.  0.  0.  0.  0.  0.]
 [ 1.  0.  1.  0.  0.  0.  1.]
 [ 1.  1.  1.  0.  0.  1.  0.]
 [ 0.  1.  0.  0.  0.  1.  1.]
 [ 0.  1.  1.  0.  1.  0.  0.]
 [ 1.  1.  0.  0.  1.  0.  1.]
 [ 1.  0.  0.  0.  1.  1.  0.]
 [ 0.  0.  1.  0.  1.  1.  1.]
 [ 1.  1.  0.  1.  0.  0.  0.]
 [ 0.  1.  1.  1.  0.  0.  1.]
 [ 0.  0.  1.  1.  0.  1.  0.]
 [ 1.  0.  0.  1.  0.  1.  1.]
 [ 1.  0.  1.  1.  1.  0.  0.]
 [ 0.  0.  0.  1.  1.  0.  1.]
 [ 0.  1.  0.  1.  1.  1.  0.]
 [ 1.  1.  1.  1.  1.  1.  1.]]

Example8.3 page 389

In [1]:
from numpy import poly1d, polydiv
#message sequence = [1,0,0,1]
g = poly1d([1,0,1,1]) #generator polynomial
m = poly1d([1,0,0,0])*poly1d([1,0,0,1]) #message sequence
q = polydiv(m,g)[0]
r = polydiv(m,g)[1]
p = r.coeffs
print 'remainder in polynomial form: \n',r
print 'Parity bits are:',p

def rev_coeffs(x):
    X=[]
    for i in reversed(x):
        X.append(i)
    return X


G = [rev_coeffs(g.coeffs),rev_coeffs((g*poly1d([1,0])).coeffs),rev_coeffs((g*poly1d([1,0,0])).coeffs),rev_coeffs((g*poly1d([1,0,0,0])).coeffs)]
M=len(G[-1])
for gg in G:
    while len(gg)<M:
        gg.append(0)
print "G:"        
for gg in G:
    print gg

def fun1(a,x,y):
    import numpy as np
    z=[]
    for xx,yy in np.nditer([a[x-1],a[y-1]]):
        z.append(xx+yy)
    a[x-1]=z
    return a    

def modulo(a,i):
    bb=[]
    for aa in a[i-1]:
        bb.append(aa%2)
    a[i-1]=bb    
    return a

G=fun1(G,3,1)#G(3,:) = G(3,:)+G(1,:);
G=modulo(G,3)#G(3,:) = modulo(G(3,:),2);
G=fun1(G,4,1)
G=fun1(G,4,2)#G(4,:) = G(1,:)+G(2,:)+G(4,:);
G=modulo(G,4)#G(4,:) = modulo(G(4,:),2);
print '\nGenerator Matrix G ='
for ggg in G:
    print ggg


#h = 1+D^-1+D^-2+D^-4;
#H_D = [D^4*h;D^5*h;D^6*h];
H_D=[poly1d([1,1,1,0,1]),poly1d([1,1,1,0,1,0]),poly1d([1,1,1,0,1,0,0])] 


#H_num =numer(H_D);
#H = coeff(H_num);
H=[rev_coeffs(aa.coeffs) for aa in H_D]

M=len(H[-1])
for hh in H:
    while len(hh)<M:
        hh.append(0)
H=fun1(H,1,3)
H= modulo(H,1)        
print '\nPartiy Check matrix H =\n'
for hh in H:
    print hh
remainder in polynomial form: 
   2
1 x + 1 x
Parity bits are: [ 1.  1.  0.]
G:
[1, 1, 0, 1, 0, 0, 0]
[0, 1, 1, 0, 1, 0, 0]
[0, 0, 1, 1, 0, 1, 0]
[0, 0, 0, 1, 1, 0, 1]

Generator Matrix G =
[1, 1, 0, 1, 0, 0, 0]
[0, 1, 1, 0, 1, 0, 0]
[1, 1, 1, 0, 0, 1, 0]
[1, 0, 1, 0, 0, 0, 1]

Partiy Check matrix H =

[1, 0, 0, 1, 0, 1, 1]
[0, 1, 0, 1, 1, 1, 0]
[0, 0, 1, 0, 1, 1, 1]

Example8.4 page 395

In [1]:
from numpy import poly1d,polydiv
#message sequence = [1,0,0,1]
g = poly1d([1,0,1,1]) #generator polynomial
m = poly1d([1,0,0,0])*poly1d([1,0,0,1])# #message sequence
q= polydiv(m,g)[0]
r= polydiv(m,g)[1]
p = r.coeffs
print 'remainder in polynomial form: \n',r
print 'Parity bits are:',p
print 'Table 8.3 Contents of the Shift Register in the Encoder of fig8.7 for Message Sequence(1001)'
print '__________________________________________________________________________________________'
print 'Shift            Input            Register Contents'
print '__________________________________________________________________________________________'
print '1                  1              1 1 0'
print '2                  0              0 1 1'
print '3                  0              1 1 1'
print '4                  1              0 1 1'
print '____________________________________________________________________________________________'
remainder in polynomial form: 
   2
1 x + 1 x
Parity bits are: [ 1.  1.  0.]
Table 8.3 Contents of the Shift Register in the Encoder of fig8.7 for Message Sequence(1001)
__________________________________________________________________________________________
Shift            Input            Register Contents
__________________________________________________________________________________________
1                  1              1 1 0
2                  0              0 1 1
3                  0              1 1 1
4                  1              0 1 1
____________________________________________________________________________________________

Example8.5 page 396

In [1]:
from numpy import poly1d,polydiv

#message sequence = [0,1,1,1,0,0,1]

g = poly1d([1,0,1,1]) # #generator polynomial
C1 = poly1d([1,0,0,1,1,1,0]) #error free codeword
C2 = poly1d([1,0,0,0,1,1,0]) #middle bit is error
#[r1,q1] = pdiv(C1,g)#

q1 = polydiv(C1,g)[0]
r1 = polydiv(C1,g)[1]

S1 = (r1).coeffs
S1 = [xx%2 for xx in S1]
print 'remainder in polynomial form : \n',r1
print 'Syndrome bits for error free codeword are:',S1
q2 = polydiv(C2,g)[0]
r2 = polydiv(C2,g)[1]
S2 = (r2).coeffs
S2 = [xx%2 for xx in S2]
print 'remainder in polynomial form for errored codeword : \n',r2
print 'Syndrome bits for errored  codeword are:',S2
remainder in polynomial form : 
   2
2 x + 2 x
Syndrome bits for error free codeword are: [0.0, 0.0, 0.0]
remainder in polynomial form for errored codeword : 
   2
2 x + 3 x + 1
Syndrome bits for errored  codeword are: [0.0, 1.0, 1.0]

Example8.6 page 399

In [1]:
from __future__ import division
#Single-error-correcting RS code with a 2-bit byte

m =2# #m-bit symbol
k = 1**2# #number of message bits
t =1# #single bit error correction
n = 2**m-1# #code word length in 2-bit byte
p = n-k# #parity bits length in  2-bit byte
r = k/n# #code rate
print 'n =',n
print 'n-k =',p
print 'Code rate:r = k/n = %.3f'%r
print 'It can correct any error upto =',(2*t)
n = 3
n-k = 2
Code rate:r = k/n = 0.333
It can correct any error upto = 2

Example8.7 page 401

In [1]:
from numpy import convolve,ones
g1 = [1,1,1] # The input Top Adder Sequence
g2 = [1,0,1] #The input Bottom Adder Sequence
m =[1,1,0,0,1] # The message sequence
x1 = [round(xx) for xx in convolve(g1,m)]
x2 = [round(xx) for xx in convolve(g2,m)]
x1 = [xx%2 for xx in x1]
x2 = [xx%2 for xx in x2]
N = len(x1)
x=[]
for i in range(0,len(x1)):
  x.append([x1[N-i-1],x2[N-i-1]])
print 'Result:'  
for xx in x:
    print xx,'\n'
Result:
[1.0, 1.0] 

[1.0, 0.0] 

[1.0, 1.0] 

[1.0, 1.0] 

[0.0, 1.0] 

[0.0, 1.0] 

[1.0, 1.0] 

Example8.8 page 404

In [1]:
from numpy import poly1d
g1D=poly1d([1,1,1]) #generator polynomial 1
g2D=poly1d([1,0,1]) #generator polynomial 2
mD=poly1d([1,1,0,0,1]) #message sequence polynomial representation
x1D=(g1D*mD) #top output polynomial
x2D=(g2D*mD) #bottom output polynomial
x1=x1D.coeffs
x2=x2D.coeffs
x1=x1.tolist()
X1=[]
for i in reversed(x1):
    X1.append(i)
X2=[]
for i in reversed(x2):
    X2.append(i)
print 'top output sequence'
for xx in X1:
    print xx%2,'\t',
print '\nbottom output sequence'    
for xx in X2:
    print xx%2,'\t',
top output sequence
1 	1 	1 	1 	0 	0 	1 	
bottom output sequence
1 	0 	1 	1 	1 	1 	1 	

Example8.11 page 409

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

r = 1/2# #code rate
n =2# #number of bits
pe = 0.04# #transition probility 
p = 1-pe## probability of correct reception
gama_1 = 2*log(p,2)+2*(1-r)# #branch metric for correct reception
gama_2 = log(pe*p,2)+1# #branch metric for any one correct recption
gama_3 = 2*log(pe,2)+1# #branch metric for no correct reception
print 'branch metric for correct reception : %.4f'%gama_1
print 'branch metric for any one correct recption: %.4f'%gama_2
print 'branch metric for no correct reception : %.4f'%gama_3
branch metric for correct reception : 0.8822
branch metric for any one correct recption: -3.7027
branch metric for no correct reception : -8.2877