Dis76crete Ch911
Dis76crete Ch911
9 THEORY OF
CODES
After studying this chapter you should
9.0
Introduction
In this chapter you will look in some depth at the way in which
codes of varying construction can be used both to detect and
sometimes correct errors. You have already seen in Chapter 8
how check digits for ISBN numbers and bar codes are used to
detect errors. In this chapter you will look at codes relevant to
data transmission, for example the transmission of pictures from
Mars to the Earth, and see how such codes are designed.
9.1
Noise
137
Encoder
message
Decoder
transmitted
signal
noise
received
signal
2
4
1,
2
since each codeword in the original message had only two digits
(called bits).
138
received message
Activity 1
Consider a code designed to specify one of four possible directions
up
down
left
right
(0 0 0)
(1 1 0)
(0 1 1)
(1 0 1)
Can this code detect any single error made during the transmission
of a codeword? Can it correct it?
Often codes include a parity check so that, for example, the code
C is transformed to C2 as shown below.
C
C2
00
000
01
011
10
101
11
110
The extra last digit in C2 (or check digit) is 0 if the sum of the
digits modulo 2 is zero (or the number of 1's is even), or is 1 if the
sum of the digits modulo 2 is 1 (or the number of 1's is odd).
(Modulo 2 means 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, 1 + 1 = 0 .)
Can Code C2 detect errors now?
Using the previous definition, the efficiency of Code C2 is
2
.
3
Activity 2
Design a code containing 4 codewords, each of length 5, which can
detect and correct a single error.
9.2
Error correction
Clearly codes which can both detect and correct errors are of far
greater use but the efficiency will decrease, since extra
essentially redundant information will have to be transmitted.
139
down
left
right
(0 0 0 0 0 0) (1 1 1 0 0 0) (0 0 1 1 1 0) (1 1 0 0 1 1)
2
6
1
3
errors.
All the codes in this Chapter are binary codes consisting of just 2
code symbols, 1 and 0. The number of codewords in a full code is
always a power of 2, for example, 2k, where k is called the
dimension of the code. k is the essential number of message bits in
the code. There are 4 codewords in the code and since 4 = 2 2 , the
dimension is 2 and the essential number of message bits is 2.
Activity 3
The following words from the above code have been received.
Assuming that only one error has been made in the transmission of
each codeword, determine if possible the actual codeword
transmitted:
(a) (1 0 0 0 0 0)
(b) (1 1 0 0 0 0 )
(c) (0 1 0 0 1 1)
Can the code above detect if 2 errors have been made in the
transmission of a codeword?
Activity 4
Codes
(b) (0 1 1 1 1 1 1)
(c) (1 0 0 0 1 0 0)
140
( (111), (010) )
= 2,
( (0101), (1011) )
= 3.
Example
Determine the Hamming distance for the code with codewords
(1 1 0 0 0), (0 0 1 0 1), (1 0 1 0 1), (1 1 1 1 1)
Solution
You must first find distances between all the codewords.
d
= 3
= 3
( (0 0101), (10101) )
= 1
( (0 0101), (11111) )
= 3
( (10101), (11111) )
= 2
Hamming distance = 1
(minimum of 1, 2, 3 and 4)
Activity 5
Hamming distance
Activity 6
Copy and complete this table.
Errors
corrected
detected
Code
Hamming distance
...
...
...
...
...
...
...
...
...
Also add on to the table any other codes considered so far. Can
you see a pattern?
The first thing that you probably noticed about the data in the table
for Activity 6 is that the results are different depending on whether
n, the number of bits, is even or odd. It looks as if, for
= 2,
= 3,
Activity 7
Construct a code for which = 4 . How many errors can it detect
or correct? Similarly, construct a code for which = 5 and again
determine how many errors it can detect or correct. Can you
suggest a generalisation of the results?
odd,
Activity 8
1
2
1
2
( 2 )
1
2
( 1)
errors.
errors.
142
Exercise 9A
1. The '2 out of 5' code consists of all possible words 3. Determine the Hamming distance for Code 7 in
of length 5 which have exactly two 1 s;
for
Appendix 5. Hence find out how many errors
example, (1 0 1 0 0) belongs to the code but (1 1
this code can detect and correct.
0 1 0) does not.
4. Show that the code
List all possible codewords and explain why this
C4 = {( 00000 ), (11000 ), ( 00011), (11111)}
code is particularly useful for the transmission of
numeric data. What is the Hamming distance for
can detect but not correct single errors in
this code?
transmission.
2. Analyse the '3 out of 7' code, defined in a similar
way to the '2 out of 5' code in Question 1.
Determine its Hamming distance and hence find out
how many errors it can detect and correct.
9.3
143
Example
Show that Code 4 is a (4, 2) code.
Solution
Since the codewords of Code 4 are
0000
1100
0011
1111
then n = 4 , and k = 2 since two columns are repeated.
(Alternatively you might like to think of it in terms of 4
codewords, which could be coded using codewords of length 2;
e.g. (0 0), (1 1 ), (1 0), (0 1); thus k = 2 and two more bits
have been added to give n = 4 .)
Thus Code 4 is a (4, 2) code.
A vector and matrix notation will be adopted, writing a
codeword x as a row vector; for example [1 1 0 1] .
1
1
The transpose of x is a column vector, x' =
0
1
x =
[ x1 x2 ... xn ]
(1 n matrix)
and
x1
x
x' = 2
...
xn
H x' = 0 (modulo 2)
and when no row of H consists just of zeros.
An alternative way of finding k is to write (when possible) the
number of codewords in the code as a power of 2. This power is
k, the dimension;
i.e. no. of codewords = 2 k.
Check this result for Code 3 and Code 4.
There are 4 codewords, so 2 k = 4 = 2 2 and hence k = 2.
144
Example
Show that
1 1 0
H =
1 1 1
0
1
and
0
0 0
0
1 0 = 0
0
H x'
1
1
= 1
1 0
1 1
H x'
2
1
= 1
1 0
1 1
H x'
3
1
= 1
1 0
1 1
0
0 0
0
0
1 1 = 1 + 1 = 0
1
H x'
4
1
= 1
1 0
1 1
1 + 1
0
0 1
=
= 0
1 1
1 + 1 + 1 + 1
1
1
0 1
0
1 + 1
1 0 = 1 + 1 = 0
0
(arithmetic is modulo 2)
1 1 0
1 1 1
x1
0 x2
=
1 x3
x 4
0
0
(modulo 2)
x1 + x2 = 0
x1 + x2 + x3 + x 4 = 0 (modulo 2)
Activity 9
Find all solutions, modulo 2, of the two equations above.
145
x 2 + x3 + x 4 = 0
x1 + x3 + x5 = 0
x1 + x2 + x6 = 0
(modulo 2)
(modulo 2)
(modulo 2).
0
1
1
0
1
1
1
0
1
0
0
0
1
0
0
0
x1
x2
0
x
3
= 0
x4
0
x5
x6
Example
Find a parity check matrix for Code 3 from Appendix 5.
Solution
For Code 3, n = 4 and k = 2 , so it is a (4, 2) Code and H will
be a 2 4 matrix. Now for all codewords in Code 3,
x1 + x3 = 0
x2 + x 4 = 0
x1 + x2 + x3 + x 4 = 0
(modulo 2)
(modulo 2)
(modulo 2).
1 0 1 0
H =
.
0 1 0 1
Activity 10
Find two other possible parity check matrices for Code 3.
Activity 11
Find a parity check matrix for Code 5 in Appendix 5.
146
9.4
Example
Show that Code 3 is linear.
Solution
A parity check matrix for Code 3 is given by
1 0 1 0
H =
(see Activity 10)
0 1 0 1
Now the equation H x' = 0 (modulo 2) can be written as
1
0
or
0
1
1
0
x1
0 x2
0
=
1 x3
0
x 4
x1 + x3 = 0
x2 + x 4 = 0 .
147
1010
1111
0101
0000
which is Code 3.
The importance of linear codes is that if x and y are two
codewords in the linear code with parity check matrix H,
H ( x + y)' = H ( x' +y')
= H x' +H y'
= 0+ 0
= 0.
Activity 12
Show that the code with codewords
0000000000
0110110010
1001001101
1111111111
is a linear code.
= H x' +H e'
= 0+ H e'
H r' = H e'
148
Example
Suppose a codeword from Code 2 is received as
r = (1 1 0 1 1 1 ) . What is the most likely codeword sent?
Solution
0 1 1 1 0 0
H r = 1 0 1 0 1 0
1 1 0 0 0 1
1
1
0
0 = 0
1
1
1
1
Activity 13
For the following words
(a)
1110000
(b)
0111011
9.5
Cyclic codes
x = (x1 x2 ... xn )
is a codeword, then so is x* defined by
x* = (xn x1 x2
x n 2 xn 1 ).
149
Example
Show that Code 1 in Appendix 5 is a cyclic code.
Solution
0000
1100
1010
1001
0110
0101
0011
here x = x*
1111
here x = x*
Activity 14
Show that Code 3 is a cyclic code.
Exercise 9B
1. Consider the linear code whose eight codewords
are as follows:
0011101
0101011
0110110
1000111
1011010
1101100
1110001
0 0 0 0 0 0 0.
150
101011
000111
101100
011001
110010
011110
110101
9.6
Miscellaneous Exercises
0 0
H = 0 1
1 0
0 1 1 1 1
1 0 0 1 1
1 0 1 0 1
1
0
H = 1
1
0
0
0 0 1 0 0 0 0 0 0
1 0 0 1 0 0 0 0 0
0 1 0 0 1 0 0 0 0
1 0 0 0 0 1 0 0 0
1 1 0 0 0 0 1 0 0
1 1 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0 1
0 1 1 0 0 0
1 1 0 1 0 0
1 0 0 0 1 0
1 1 0 0 0 1
151
152