error detection and correction parity and hamming code

20
Error Detection and Correction Parity and Hamming Code

Upload: susan-ross

Post on 30-Dec-2015

248 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Error Detection and Correction Parity and Hamming Code

Error Detection

and Correction

Parity and Hamming Code

Page 2: Error Detection and Correction Parity and Hamming Code

Data Transmission and Interference

00101011 0010101100101111

Page 3: Error Detection and Correction Parity and Hamming Code

Causes of Electrical Interference

Page 4: Error Detection and Correction Parity and Hamming Code

The Effect of ErrorsHi Dave you’re the best. Hi Dove you’re the belt.

LDA #0.loop INC CMP #10 BNE loop

LDA #0.loop DEC CMP #10 BNE loop

(this one might not quite be true!)

Page 5: Error Detection and Correction Parity and Hamming Code

Parity Bits – The SolutionASCII code characters only uses 7 of the 8 bits in a byte so the most signifcant bit (MSB) can be often used as a parity bit.

Two systems: Odd parity – each byte should contain an odd number of ones.Even parity – each byte should contain an even number of ones.

Example – sending an “S” character using even parity:

01 0 11010 1

Sender setsparity bit to 0

Interference changes bit

Receiver counts odd number of 1s, error detected!

ASCII code of “S”

Page 6: Error Detection and Correction Parity and Hamming Code

Parity Bit Exercise

1 0 1101

All these exercises assume an even parity system is in use.

0 0 0101

(1) Work out the parity bits for these two examples:

(2) Which of these bytes have been received correctly?

1 0 11010

0 0 01011

1

0

1

1

1

0

X

Q

Page 7: Error Detection and Correction Parity and Hamming Code

Parity Bits – NOT The Solution

01 0 11010 1

Sender setsparity bit to 0

Interference changes 2 bits

Receiver counts even number of 1s, error NOT detected!

0

01 0 11010 1

Sender setsparity bit to 0

Interference changes bit

Receiver counts odd number of 1s, error detected! BUT which bit is wrong, and how can it be fixed?

No mechanism to detect errors with even number of bits or to correct any errors

Page 8: Error Detection and Correction Parity and Hamming Code

Hamming Codes – The SolutionHamming codes make use of multiple parity bits to enable single bit error correction and two-bit error detection.

HAMMING CODE = DATA BITS + PARITY BITS

Within a Hamming code:• Bits at positions that are powers of 2 are used as parity bits.• Bits at other positions are used to store data bits.

To transmit an 8-bit byte using Hamming code, 12 bits are required:• Bits at positions 1, 2, 4, 8 are used as parity bits.• Bits at other positions are used to store the data to send.

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P1

The bits are usually labelled with letters and numbers to indicate their type (D for Data, P for Parity) and position within the bits of this type. e.g. “D5”

Page 9: Error Detection and Correction Parity and Hamming Code

Hamming Codes – Bit PositionsEach parity bit is calculated based upon the values at certain positions within the Hamming code. The rule to determine which bits to include in the calculation of parity bit number x at bit position n is:

Px: Skip n-1 bits from left, where n is the bit position of Px then include n bits in the check then miss out the next n bits and so on.

For a 12-bit Hamming code the data bits checked by each parity bit are:

KEY POINT: Each data bit is checked by a unique combination of parity bits. e.g. data bit D7 is checked by parity bits P1, P2 and P4 and not P3.

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P1Included in P1:                        Included in P2:                        Included in P3:                        Included in P4:                        

Page 10: Error Detection and Correction Parity and Hamming Code

Hamming Code - TransmissionTransmitting the byte 00100011:

• The data bits are copied into positions D8 down to D1.• Each parity bit (P1 to P4) is calculated in turn, using even

parity.• The parity bits and data bits combine to make the Hamming

code, so the Hamming code for 00100011 is 001010011100.

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Original Data 0 0 1 0   0 0 1   1    2. Calculate P1   0   0   0   1   1   03. Calculate P2   0 1     0 0     1 0  4. Calculate P3 0         0 0 1 1      5. Calculate P4 0 0 1 0 1              Hamming Code Is 0 0 1 0 1 0 0 1 1 1 0 0

Page 11: Error Detection and Correction Parity and Hamming Code

Exercise: TransmissionTransmitting the byte 11010011:

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity                        1. Original Data                        2. Calculate P1                        3. Calculate P2                        4. Calculate P3                        5. Calculate P4                        Hamming Code Is                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Original Data                        2. Calculate P1                        3. Calculate P2                        4. Calculate P3                        5. Calculate P4                        Hamming Code Is                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Original Data 1 1 0 1   0 0 1   1    2. Calculate P1                        3. Calculate P2                        4. Calculate P3                        5. Calculate P4                        Hamming Code Is                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Original Data 1 1 0 1   0 0 1   1    2. Calculate P1   1   1   0   1   1    3. Calculate P2                        4. Calculate P3                        5. Calculate P4                        Hamming Code Is                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Original Data 1 1 0 1   0 0 1   1    2. Calculate P1   1   1   0   1   1   03. Calculate P2                        4. Calculate P3                        5. Calculate P4                        Hamming Code Is                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Original Data 1 1 0 1   0 0 1   1    2. Calculate P1   1   1   0   1   1   03. Calculate P2   1 0     0 0     1    4. Calculate P3                        5. Calculate P4                        Hamming Code Is                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Original Data 1 1 0 1   0 0 1   1    2. Calculate P1   1   1   0   1   1   03. Calculate P2   1 0     0 0     1 0  4. Calculate P3                        5. Calculate P4                        Hamming Code Is                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Original Data 1 1 0 1   0 0 1   1    2. Calculate P1   1   1   0   1   1   03. Calculate P2   1 0     0 0     1 0  4. Calculate P3 1         0 0 1        5. Calculate P4                        Hamming Code Is                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Original Data 1 1 0 1   0 0 1   1    2. Calculate P1   1   1   0   1   1   03. Calculate P2   1 0     0 0     1 0  4. Calculate P3 1         0 0 1 0      5. Calculate P4                        Hamming Code Is                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Original Data 1 1 0 1   0 0 1   1    2. Calculate P1   1   1   0   1   1   03. Calculate P2   1 0     0 0     1 0  4. Calculate P3 1         0 0 1 0      5. Calculate P4 1 1 0 1                Hamming Code Is                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Original Data 1 1 0 1   0 0 1   1    2. Calculate P1   1   1   0   1   1   03. Calculate P2   1 0     0 0     1 0  4. Calculate P3 1         0 0 1 0      5. Calculate P4 1 1 0 1 1              Hamming Code Is                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Original Data 1 1 0 1   0 0 1   1    2. Calculate P1   1   1   0   1   1   03. Calculate P2   1 0     0 0     1 0  4. Calculate P3 1         0 0 1 0      5. Calculate P4 1 1 0 1 1              Hamming Code Is 1 1 0 1 1 0 0 1 0 1 0 0

The Hamming code for 11010011 is 110110010100

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Original Data                        2. Calculate P1                        3. Calculate P2                        4. Calculate P3                        5. Calculate P4                        Hamming Code Is                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Original Data                        2. Calculate P1                        3. Calculate P2                        4. Calculate P3                        5. Calculate P4                        Hamming Code Is                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Original Data                        2. Calculate P1                        3. Calculate P2                        4. Calculate P3                        5. Calculate P4                        Hamming Code Is                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Original Data                        2. Calculate P1                        3. Calculate P2                        4. Calculate P3                        5. Calculate P4                        Hamming Code Is                        

Q

Page 12: Error Detection and Correction Parity and Hamming Code

Hamming Code – No Error

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Received Data 0 0 1 0 1 0 0 1 1 1 0 02. Recalculate P1   0   0   0   1   1   03. Recalculate P2   0 1     0 0     1 0  4. Recalculate P3 0         0 0 1 1      5. Recalculate P4 0 0 1 0 1              6. Do parity bits match?         ü       ü   ü ü

The Hamming code 001010011100 is received.

The parity bits are all recalculated from the data bits (recalculated parity bits shown in blue text):

• Each of the four recalculated parity bits (blue) match the received parity bits (red) so the Hamming code is valid.

• The received data value, read off from the data bits is 00100011.

Page 13: Error Detection and Correction Parity and Hamming Code

Hamming Code – 1 Bit ErrorThe Hamming code 001010011100 is transmitted but the code 001010001100 is received because interference has corrupted the bit at position 5, changing it from a 1 to a 0.

The parity bits are all recalculated from the data bits (recalculated parity bits shown in blue text):

• Recalculated parity bits P3 and P1 at bit positions 4 and 1 do not match the received parity bits.

• An error has been successfully detected!

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Received Data 0 0 1 0 1 0 0 0 1 1 0 02. Recalculate P1   0   0   0   0   1   13. Recalculate P2   0 1     0 0     1 0  4. Recalculate P3 0         0 0 0 0      5. Recalculate P4 0 0 1 0 1              

6. Do parity bits match?         ü       x   ü x

Page 14: Error Detection and Correction Parity and Hamming Code

Hamming Code – CorrectionNow the magic bit!

The two parity bits with errors are P1 and P3. The only bit that is checked by both these parity bits and only these two parity bits is D2, so this is where the error is.

This can be worked out more easily using the bit positions (this is why the parity bits weren’t all put at the end). The parity bits with errors are at bit positions 1 and 4. Add these two numbers together to give the answer 5, which is the bit position where the error is.

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Received Data 0 0 1 0 1 0 0 0 1 1 0 02. Recalculate P1   0   0   0   0   1   13. Recalculate P2   0 1     0 0     1 0  4. Recalculate P3 0         0 0 0 0      5. Recalculate P4 0 0 1 0 1              

6. Do parity bits match?         ü       x   ü x

Page 15: Error Detection and Correction Parity and Hamming Code

Hamming Code – CorrectionNow we know the error is at bit position 5 which has been received as a 0. As the binary system is used, there are only two possible bit values (0 and 1) so if 0 is incorrect, 1 must be correct.

Therefore the error can be corrected by simply NOTing the bit value at the error position:

The data value received, after correction, is 00100011which is the value that was sent, despite the interference having occurred. Error corrected!

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Received Data 0 0 1 0 1 0 0 0 1 1 0 02. Recalculate P1   0   0   0   0   1   13. Recalculate P2   0 1     0 0     1 0  4. Recalculate P3 0         0 0 0 0      5. Recalculate P4 0 0 1 0 1              

6. Do parity bits match?         ü       x   ü x

7. Calculate position of errorParity bits at bit positions 1 and 4 do not match so error position is

1+4=5

8. Flip error bit               1        

9. Corrected code is 0 0 1 0 1 0 0 1 1 1 0 0

Page 16: Error Detection and Correction Parity and Hamming Code

Exercise: Received Correctly?The Hamming code 101100100111 is received, that appears to contain the data value 10110101. What data value was transmitted?

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Received Data 1 0 1 1 0 0 1 0 0 1 1 12. Recalculate P1                        3. Recalculate P2                        4. Recalculate P3                        5. Recalculate P4                        6. Do parity bits match?                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Received Data 1 0 1 1 0 0 1 0 0 1 1 12. Recalculate P1   0   1   0   0   1    3. Recalculate P2                        4. Recalculate P3                        5. Recalculate P4                        

6. Do parity bits match?                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Received Data 1 0 1 1 0 0 1 0 0 1 1 12. Recalculate P1   0   1   0   0   1   03. Recalculate P2                        4. Recalculate P3                        5. Recalculate P4                        

6. Do parity bits match?                       x

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Received Data 1 0 1 1 0 0 1 0 0 1 1 12. Recalculate P1   0   1   0   0   1   03. Recalculate P2   0 1     0 1     1    4. Recalculate P3                        5. Recalculate P4                        

6. Do parity bits match?                       x

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Received Data 1 0 1 1 0 0 1 0 0 1 1 12. Recalculate P1   0   1   0   0   1   03. Recalculate P2   0 1     0 1     1 1  4. Recalculate P3                        5. Recalculate P4                        

6. Do parity bits match?                     ü x

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Received Data 1 0 1 1 0 0 1 0 0 1 1 12. Recalculate P1   0   1   0   0   1   03. Recalculate P2   0 1     0 1     1 1  4. Recalculate P3 1         0 1 0        5. Recalculate P4                        

6. Do parity bits match?                     ü x

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Received Data 1 0 1 1 0 0 1 0 0 1 1 12. Recalculate P1   0   1   0   0   1   03. Recalculate P2   0 1     0 1     1 1  4. Recalculate P3 1         0 1 0 0      5. Recalculate P4                        

6. Do parity bits match?                 ü   ü x

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Received Data 1 0 1 1 0 0 1 0 0 1 1 12. Recalculate P1   0   1   0   0   1   03. Recalculate P2   0 1     0 1     1 1  4. Recalculate P3 1         0 1 0 0      5. Recalculate P4 1 0 1 1                

6. Do parity bits match?                 ü   ü x

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Received Data 1 0 1 1 0 0 1 0 0 1 1 12. Recalculate P1   0   1   0   0   1   03. Recalculate P2   0 1     0 1     1 1  4. Recalculate P3 1         0 1 0 0      5. Recalculate P4 1 0 1 1 1              

6. Do parity bits match?         x       ü   ü x

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Received Data 1 0 1 1 0 0 1 0 0 1 1 12. Recalculate P1   0   1   0   0   1   03. Recalculate P2   0 1     0 1     1 1  4. Recalculate P3 1         0 1 0 0      5. Recalculate P4 1 0 1 1 1              

6. Do parity bits match?         x       ü   ü x

7. Calculate position of errorParity bits at bit positions 1 and 8 do not match so error position is

1+8=9

8. Flip error bit                        

9. Corrected code is                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Received Data 1 0 1 1 0 0 1 0 0 1 1 12. Recalculate P1   0   1   0   0   1   03. Recalculate P2   0 1     0 1     1 1  4. Recalculate P3 1         0 1 0 0      5. Recalculate P4 1 0 1 1 1              

6. Do parity bits match?         x       ü   ü x

7. Calculate position of errorParity bits at bit positions 1 and 8 do not match so error position is

1+8=9

8. Flip error bit       0                

9. Corrected code is                        

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Received Data 1 0 1 1 0 0 1 0 0 1 1 12. Recalculate P1   0   1   0   0   1   03. Recalculate P2   0 1     0 1     1 1  4. Recalculate P3 1         0 1 0 0      5. Recalculate P4 1 0 1 1 1              

6. Do parity bits match?         x       ü   ü x

7. Calculate position of errorParity bits at bit positions 1 and 8 do not match so error position is

1+8=9

8. Flip error bit       0                

9. Corrected code is 1 0 1 0 0 0 1 0 0 1 1 1

The data value that was transmitted is 10100101. The transmission was corrupted but the original data value was recovered by error correction.Q

Page 17: Error Detection and Correction Parity and Hamming Code

Hamming Code – 2 Bit ErrorYou won’t need to be able to do this for the exam but Hamming codes can also detect 2-bit errors, but cannot correct them:

Hamming Code 001010011100 is transmitted but received as 011010001100 :

The calculated bit position of the error is 14, but there are only 12 bits.The error has been detected but cannot be corrected.

Bit Position 12 11 10 9 8 7 6 5 4 3 2 1Data/Parity D8 D7 D6 D5 P4 D4 D3 D2 P3 D1 P2 P11. Received Data 0 1 1 0 1 0 0 0 1 1 0 02. Recalculate P1   1   0   0   0   1   03. Recalculate P2   1 1     0 0     1 1  4. Recalculate P3 0         0 0 0 0      5. Recalculate P4 0 1 1 0 0              

6. Do parity bits match?         x       x   x ü

7. Calculate position of error Parity bits at positions 2,4 and 8 do not match so error at bit position 14

Page 18: Error Detection and Correction Parity and Hamming Code

Comparing Parity and Hamming A simple parity system with one parity bit can

detect single bit errors but cannot correct them.

The advantages of Hamming codes over a simple parity system are that they can correct a single bit error and can also detect 2-bit errors.

The major disadvantage of Hamming codes is that some of the bandwidth of the transmission medium is used to send the parity bits. In our example, to send an 8-bit byte, 12 bits were required. So, one third of the bandwidth was used sending error correction data. For a communication channel with a bit rate of 600Mbit/second, this would effectively be slowed to 400Mbit/second.

Page 19: Error Detection and Correction Parity and Hamming Code

For The ExamYou need to:be able to calculate parity bits using odd and even

parity.know the role of the sending and receiving devices

when a parity system is used.be able to calculate Hamming codes.be able to detect and correct an error using a

Hamming code.know the advantages and disadvantages of

Hamming codes when compared to a simple parity bit system.

You don’t need to:know the causes of interference.know how to detect 2-bit errors.

Page 20: Error Detection and Correction Parity and Hamming Code

A Bit More...Hamming codes and parity bits are

typically used on small amounts of data e.g. single bytes.

More advanced techniques such as cyclic redundancy checks and checksums are used on bigger blocks of data such as packets transmitted on networks and sectors saved on disks. These involve calculating a hash value from all of the data in the block – you can find out lots more about these online.