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

Record DCN (S)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Ex. No.

1 Date:

ERROR DETECTION CODES

AIM:

To implement Error Detection codes such as Parity Checker code and Cyclic
Redundancy Check Code in C Language.

ALGORITHM:

PARITY CHECKER:

1. Open the text editor and save a file in ‘.c’ format.


2. Get the data from the user.
3. Count the number of ones, in case of odd parity. If number of ones is odd, the paritybit is
assigned as 0. If number of ones is even, the parity bit is assigned as 1.
4. In the case of even parity, if the number of ones are odd, the parity bit is assigned as
1. If the number ones are even, the parity bit is assigned as 0.
5. Append the parity bit to data from the control word.
6. Use a parity checker to check the correctness of data.
7. Stop the program.

CYCLIC REDUNDANCY CHECK:

1. Start.
2. Get the data and the divisor as input.
3. Perform XOR division and determine the remainder.
4. Then append the remainder at the end of data bits.
5. Get the received data from the user.
6. Again, perform the XOR division, if the remainder is '0', then the received data bit isnot
corrected.
7. Else the received data bit is corrupted.
8. Stop.

THEORY:

PARITY CHECKER:

Parity checking is the most basic form of error detection in communications. A parity check is a
process that ensures accurate data transmission between nodes duringcommunication. A parity bit is
appended to the original data bits to create an even or odd number, the number of bits to value one. The
source then transmits this data via a link, and bitsare checked and verified at the destination. Data is
considered accurate if the number of bits matches the number transmitted from the source.
//parity code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

char rdata[20]; int datlen;


int check(char data[20]);

int main(){
int i, count =0, tflag, rflag, test;
char data[20];
printf("\nEnter no of bits of data : ");
scanf("%d", &datlen);
printf("\nEnter data : ");
scanf("%s", data);
tflag = check(data);
printf("\nTramsmitted data :\n");
strcpy(rdata,data);
printf(" %s ",rdata);
rdata[0] = (rand()%2 == 0)?'0':'1';
printf("\nthe data : ");
for(i =0;i< datlen; ++i)
printf("%c", rdata[i]);
printf("\nReceived data : %s ", rdata);
rflag = check(rdata);

if(tflag == rflag){
printf("\nThe received message has no error\n");
}
else{
printf("\nThe received message has error !!!\n");
}

return 0;
}

int check(char data[20]){


int i, evenp, oddp, count =0;
for(i =0; i<datlen; ++i){
if(data[i] == '1'){
count++;
}
}
printf("\nCount : %d\n",count);
if(count%2)
printf("\nEven parity : 1 \tOdd parity : 0\n");
else
printf("\nEven parity : 0 \tOdd parity : 1\n");
return (count%2)?'0':'1';
}
THEORY:

PARITY CHECKER:

Parity checking is the most basic form of error detection in communications. A parity check is a
process that ensures accurate data transmission between nodes duringcommunication. A parity bit is
appended to the original data bits to create an even or odd number, the number of bits to value one. The
source then transmits this data via a link, and bitsare checked and verified at the destination. Data is
considered accurate if the number of bits matches the number transmitted from the source.

Parity checking, which was created to eliminate data communication errors, is a simplemethod of
network data verification and has an easy and understandable working mechanism.

As an example, if original data is 1010001, there are three 1's when even parity checkingis used, a
parity bit with value 1 is added to data left side to make the number of ones even; transmitted data becomes
11010001. However, if odd parity checking is used, then the parity bit value is 0,01010001.

If the original data contains an even number of 1's, then parity bit of value 1 is added to the data's
left side to make the number of ones is odd, if odd parity checking is used and datatransmitted becomes
11101001. In case data is transmitted incorrectly, the parity bit value becomes incorrect, thus indicating
an error has occurred during transmission.

Parity checking is used not only in communication but also to test memory storage devices Many
PCs, for example, perform a parity check on memory every time a byte of data is read.
OUTPUT:

Enter no of bits of data : 4

Enter data :1110

Count : 2

Even parity : 1 Odd parity :0

Transmitted data :1110


the data :1110
Received data :1110
Count : 3

Even parity : 1 Odd parity:0

The received message has no error

Enter no of bits of data : 4

Enter data :1010


Count : 2

Even parity : 0 Odd parity : 1

Transmitted data :1010

The data :1010


Received data :1010
Rand:0
Received data after replacing with rand: 0010
Count :1

Even parity : 1 Odd parity :0

The received message has error!!!


CYCLIC REDUNDANCY CHECK:

In data transmission, the ability of a receiving station to correct errors in the received data is called
forward error correction (FEC) and can increase throughput on a data link whenthere is a lot of mouse
present. To enable this, the transmitting station must add extra data (called error corrections bits)to the
transmission. However, the correction may not always represent a cost-saving over that of simply resending
the information.
//cyclic redundancy check
#include <stdio.h>
char data[20], divi[20], temp[20], total[100];
int i,j,datalen,divlen,len,flag =1;
void check();
int main(){
printf("\nEnter no of bits of data: ");
scanf("%d",&datalen);
printf("\nEnter no of bits of divisor: ");
scanf("%d",&divlen);
printf("\nEnter data: ");
scanf("%s",data);
printf("\nEnter divisor: ");
scanf("%s",divi);
len = datalen + divlen -1;
for(i =0; i<datalen; i++){
total[i] = data[i];
temp[i] = data[i];}
for(i=datalen; i<len; i++){
total[i] = '0';}
check();
for(i =0; i<divlen; i++){
temp[i+datalen] = data[i]; }
printf("\nTransmitted data : %s",temp);
for(i=0;i<len;++i)
total[i] = temp[i];
srand(time(NULL));
total[3] = (rand()%2==0)?'0':'1';
check();
for(i=0; i<divlen; i++){
if(data[i] == '1')
{flag =0; break;}}
if(flag ==1)
printf("\nSuccess data has no error :)");
else
printf("\nFailure data has some error:(");}
void check(){
for(j =0; j<divlen; j++)
data[j] = total[j];

while(j<=len){
if(data[0] == '1')
for(i=1;i<divlen; i++)
data[i] = ((data[i] == divi[i])?'0':'1');

for(i=0; i<divlen-1; i++)


data[i] = data[i+1];
data[i] = total[j++];
}
}
CYCLIC REDUNDANCY CHECK (CRC):

A cyclic redundancy check (CRC) is an error-detecting code commonly used in digitalnetworks and
storage devices to detect accidental changes to raw data. The CRC was inventedby W. Wesley Peterson in
1961, the 32-bit CRC function of Ethernet and many other standardsis the work of several researchers and
was published in 1975.

CRC is based on binary division. In CRC, instead of adding bits to achieve the desiredparity, a
sequence of redundant bits, called the CRC or CRC remainder, is appended to the endof the data unit so
that the resulting data unit becomes exactly divisible by a second,predetermined binary number. At the
destination, the incoming data unit is assumed to be intactand is therefore accepted. A remainder indicates
that the data unit has been damaged in transitand therefore must be rejected.
OUTPUT:

Enter no of bits of data :5

Enter no of bits of divisor:

Enter data:11101

Enter divisor:110

Transmitted data:11101

Enter the received data:

1110100

Success data has no error

Enter no of bits of data:4

Enter no of bits of divisor3

Enter data:1010

Enter divisor:101

Transmitted data:10100

Enter the received

data:1101

Failed data has some error!!!


RESULT:

Thus, the error-detecting codes such as Parity Checker codes and Cyclic RedundancyCheck
Codes has been implemented and verified successfully.
Ex. No. 2 Date:

ERROR CORRECTION CODE- HAMMING CODE


AIM:
To implement Hamming Error Correction Code in a C language.

ALGORITHM:

1. Start the program.


2. Input the bits for data of certain indexes 0,1,2,4 respectively.
3. Generate the parity bits for the data.
4. Copy the transmitted data to received data .
5. Replace the last bit of received data using random bits.
6. Check the parity bits of received data.
7. Display the error position
8. Correct the error bit by inverting
9. Stop the program.
THEORY:
HAMMING CODE:

Hamming Code is a set of error correction code that can be used to detect and correct it error that can
occur when computer data is moved or stored. Hamming code is named R.W Hamming of Bell Labs.
Hamming codes makes FEC less expensive to implement through the use of block parity mechanism.

1. A minimum number of redundancy bits are needed to correct any single bit error in the
data.
2. A minimum number of 4 redundancy bits is needed if the number of data bits is 4.
3. Redundancy bits in the hamming code are placed in the code word bit positions that are
raised to the power of 2.
4. Each redundancy bit is the parity bit for a different combination of data bits.
5. Each data bits may be included in more than one parity check.
//hamming code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void main(){
int data[7],rec[7],i,c1,c2,c3,c;

printf("\nEnter 4 bit message : ");


scanf("%d %d %d %d",&data[0],&data[1],&data[2],&data[4]);

data[6] = data[0]^data[2]^data[4];
data[5] = data[0]^data[1]^data[4];
data[3] = data[0]^data[1]^data[2];

printf("\nThe encoded bits : ");


for(i=0; i<7;++i){
rec[i] = data[i];
printf("%d",data[i]);
}
srand(time(NULL));
rec[4] = rand()%2;

printf("\nRand = %d\n",rec[4]);
printf("\nReceived Data : ");

for(i=0;i<7;++i)
printf("%d",rec[i]);
c1 = rec[6]^rec[4]^rec[2]^rec[0];
c2 = rec[5]^rec[4]^rec[1]^rec[0];
c3 = rec[3]^rec[2]^rec[1]^rec[0];

c = c3*4+c2*2+c1;

if(c==0)
printf("\nNO ERROR\n");
else{
printf("\nError position : %d\n",7-c);
printf("\nThe correct message : \n");
rec[7-c] = (rec[7-c] == 0)?1:0;

for(i=0;i<7;++i)
printf("%d",rec[i]);
}

}
Hamming codes detect two-bit errors with more than one parity bit, each of which is computed
on different combinations of bits in the data. The number of parity bits required depends on the number
of bits in the data transmission, and is calculated by the hammingrule. d+p+1<=2(1), where d is the no of
data bits and p is the no of parity bits.

HAMMING DISTANCE CALCULATE:


Thus a code with minimum Hamming Distance (d) between its codewords candetect at mostd-
1 errors and can correct [(d-1)/2] errors. In this example, Hamming distance is enough for not only error
detection, but alsoerror correction. Every allowable combination has set of not allowable.
ALLOWABLE NOT ALLOWABLE
000 001
010
100
111 011
101
110

HAMMING CODE WORKING:


OUTPUT:

Enter 4 bit message : 1100

The encoded bits:


1100001

Rand =0

Received Data :

1100001

NO ERROR!!!

Enter 4 bit message :1010

The encoded bits:


1010010

Rand =1

Received Data:
1010110
Error position :4

The correct message :1010010


RESULT:
Thus, the error detection and correction code such as Hamming Code has beenimplemented
and verified successfully.

You might also like