Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Hamming Code

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

2.

Implement the Data Link layer error detection and correction techniques using
Hamming Code.
Procedure:

Hamming code is a popular error detection and error correction method in data communication.
Hamming code can only detect 2 bit error and correct a single bit error which means it is unable to
correct burst errors if may occur while transmission of data.

Hamming code uses redundant bits (extra bits) which are calculated according to the below
formula:-

2r ≥ m+r+1
Where r is the number of redundant bits required and m is the number of data bits.
R is calculated by putting r = 1, 2, 3 … until the above equation becomes true.
R1 bit is appended at position 20
R2 bit is appended at position 21
R3 bit is appended at position 22 and so on.
These redundant bits are then added to the original data for the calculation of error at receiver’s
end.

At receiver’s end with the help of even parity (generally) the erroneous bit position is identified
and since data is in binary we take complement of the erroneous bit position to correct received
data.

Respective index parity is calculated for r1, r2, r3, r4 and so on.

Advantages of Hamming Code


1. Easy to encode and decode data at both sender and receiver end.
2. Easy to implement.
Disadvantages of Hamming Code
1. Cannot correct burst errors.
2. Redundant bits are also sent with the data therefore it requires more bandwidth to send
the data.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
char data[5];
int encoded[8],edata[7],syndrome[5];
int hmatrix[3][7]={
1,0,0,0,1,1,1,
0,1,0,1,0,1,1,
0,0,1,1,1,0,1,
};
char gmatrix[4][8]={"0111000","1010100","1100010","1110001"};
int main()
{
int i,j;
clrscr();
printf("\n Hamming code-----Encoding");
printf("\n Enter 4 bit data:");
scanf("%s",data);
printf("\n Generator matrix:");
for(i=0;i<4;i++)
printf("\n%s",gmatrix[i]);
printf("\n Encoded data:");
for(i=0;i<7;i++)
{
for(j=0;j<4;j++)
encoded[i]+=((data[j]-'0')*(gmatrix[j][i]-'0'));
encoded[i]=encoded[i]%2;
printf("%d",encoded[i]);
}
printf("\n Hamming code---Decoding");
printf("\n Enter encoded bits as received:");
for(i=0;i<7;i++)
scanf("%d",&edata[i]);
for(i=0;i<3;i++)
{
for(j=0;j<7;j++)
syndrome[i]+=(edata[j]*hmatrix[i][j]);
syndrome[i]=syndrome[i]%2;
printf("syndrome:%d\t",syndrome[i]);
}
for(j=0;j<7;j++)
if((syndrome[0]==hmatrix[0][j] && (syndrome[1]==hmatrix[1][j] &&
(syndrome[2]==hmatrix[2][j]))))
break;
if(j==7)
printf("\n error free");
else
{
printf("\n error received at bit number %d of data",j+1);
edata[j]=!edata[j];
printf("\n correct data should be:");
for(i=0;i<7;i++)
printf("%d",edata[i]);
}
getch();
return 0;
}

output:
Hamming code-----Encoding
Enter 4 bit data:1011
Generator matrix:
0111000
1010100
1100010
1110001
Encoded data:0101011
Hamming code---Decoding
Enter encoded bits as received:0 1 0 1 0 1 1
syndrome:0 syndrome:0 syndrome:0
error free
Hamming code-----Encoding
Enter 4 bit data:1011

Generator matrix:
0111000
1010100
1100010
1110001
Encoded data:0101011
Hamming code---Decoding
Enter encoded bits as received:0 1 0 1 1 1 1
syndrome:1 syndrome:0 syndrome:1
error received at bit number 5 of data
correct data should be:0101011

You might also like