Wednesday, September 18, 2013
0 comments

Concept of Hamming Code( Java program)

8:38 AM
You might be familiar with Richard Ehrenborg’s beautiful game where your friend chooses a number from 0 to 15, and you proceed to show in turn seven cards, asking which side of the card the number is on, and then placing each card down carefully on a base card. Each card has a funny shape, with squares poking out at the edges.

The Base Card

Is your number on this side?
Your friend can lie at most once, and so once the cards have been placed down, the number on the base card which is covered at most once by a protruding square, is the number your friend has chosen. What is so cool about this trick is that you have an excellent information rate — seven questions, sixteen numbers to choose from and one lie. What makes this tick is a perfect code, which was known earlier by Golay but is attributed to Hamming (as he could publish his findings and Golay could not). (See Chapter 1, Section 6 of Thomas Thompson’s book). This code has code length 7, dimension 4 and minimum distance 3. So it can correct single errors and detect two errors. However, in demonstrating the glory of the Hamming code to the uninitiated, I felt something was missing; a crappier precursor.
I’ve come up with a simpler game based on a [6,3,3]-code which has a (poor)  information rate of  0.5, and is certainly not perfect (the Hamming bound here is 64/7). The generator matrix for this linear code is
\begin{bmatrix}1&0&0&0&1&1\\ 0&1&0&1&0&1\\ 0&0&1&1&1&0\end{bmatrix}.
Your friend chooses a number from 0 to 7, and you proceed to present six cards in turn, asking which side of the card the number is on, and then placing each card down carefully on a base card. It still works like the Hamming code, but makes the Hamming code look so much better in comparison. The cards are attached.


Now once we are aware of what  Hamming Code is all about. Here is the program for hamming code in Java.



import java.util.*;
class hamming_code
{
static int set_parity_bit(int a[])
{
int count=0; //........Initialising count to zero which will count the number of 1.
int l=a.length;

for(int i=0;iif(a[i]==1)
++count; //............Incrementing count if value in array "a" is 1.

if((count%2)==0)
return 0;//........Returning 0 if even number of 1 
else
return 1;//........Returning 1 if odd number of 1
}
public static void main(String args[])
{
Scanner scr=new Scanner(System.in);
System.out.println("This is hamming code error detection and correction using EVEN parity");
System.out.println();
System.out.println("Enter 4 data bits.");
int n=4; //set number of data bits
int d[]=new int[4]; //..........Array to accept data bits
for(int i=n-1;i>=0;--i)
{
System.out.println("Enter the value of D"+(i+1));
d[i]=scr.nextInt();
}

/*.............. Formula for calculating 2^k>=n+k+1 ...............*/

int k=0;// k stands for number of parity bits.Initializing it to zero.

while(Math.pow(2,k)<(n+k+1)) // Calculating the value of k(number of parity bits).
{
++k;
}
System.out.println();
System.out.println(k+" parity bits are required for the transmission of data bits.");

int p[]=new int[k]; //..........Array to store parity bits
int h[]=new int[n+k+1];//.........Array to hold the hamming code.(n+k+1) as we start from pos 1.

/********** Initialising array h[] to -1 ************/
for(int i=0;i<7 data-blogger-escaped-br="" data-blogger-escaped-i="">h[i]=-1;

int count=0;
int c=2;
while(count<4 data-blogger-escaped-br="">{
++c;
if(c==4)
continue;

h[c]=d[count];
++count;
}
int p1[]={h[1],h[3],h[5],h[7]};
int p2[]={h[2],h[3],h[6],h[7]};
int p3[]={h[4],h[5],h[6],h[7]};
int parity[]=new int[3];
/************Setting the value of parity bit*************/
parity[0]=set_parity_bit(p1);
parity[1]=set_parity_bit(p2);
parity[2]=set_parity_bit(p3);
/************Inserting the parity bits in the hamming code**********/
h[1]=parity[0];
h[2]=parity[1];
h[4]=parity[2];
System.out.println("\nSENDER:");
System.out.print("\nThe data bits entered are: ");
for(int i=3;i>=0;--i)
System.out.print(d[i]+" ");

System.out.println("\nThe Parity bits are: ");
for(int i=2;i>=0;--i)
System.out.println("Value of P"+(i+1)+" is "+parity[i]+" ");

System.out.print("\nThe Hamming code is as follows ");
for(int i=(n+k);i>0;--i)
System.out.print(h[i]+" ");

System.out.println();
System.out.println("Enter the hamming code with error at any position of your choice.\nNOTE: ERROR should be present only at one bit position");
for(int i=7;i>0;--i)
h[i]=scr.nextInt();

int p4[]={h[1],h[3],h[5],h[7]};
int p5[]={h[2],h[3],h[6],h[7]};
int p6[]={h[4],h[5],h[6],h[7]};

parity[0]=set_parity_bit(p4);
parity[1]=set_parity_bit(p5);
parity[2]=set_parity_bit(p6);

int position=(int)(parity[2]*Math.pow(2,2)+parity[1]*Math.pow(2,1)+parity[0]*Math.pow(2,0));
System.out.println("\nRECEIVER:");
System.out.println("Error is detected at position "+position+" at the receiving end.");
System.out.println("Correcting the error.... ");

if(h[position]==1)
h[position]=0;
else
h[position]=1;

System.out.print("The correct code is ");
for(int i=7;i>0;--i)
System.out.print(h[i]+" ");
}}

/* Output
This is hamming code error detection and correction using EVEN parity

Enter 4 data bits.
Enter the value of D4
1
Enter the value of D3
0
Enter the value of D2
1
Enter the value of D1
0

3 parity bits are required for the transmission of data bits.

SENDER:
The data bits entered are: 1 0 1 0
The Parity bits are:
Value of P3 is 0
Value of P2 is 1
Value of P1 is 0

The Hamming code is as follows 1 0 1 0 0 1 0

Enter the hamming code with error at any position of your choice.
NOTE: ERROR should be present only at one bit position
1 0 1 0 1 1 0

RECEIVER:
Error is detected at position 3 at the receiving end.
Correcting the error....
The correct code is 1 0 1 0 0 1 0
*/
Hope youll liked it.

0 comments:

Post a Comment

Feature

 
Toggle Footer
Top