a). Program for error correction using
CRC.
b). Program for error detection using
Hamming Code.
A cyclic redundancy check (CRC) is an error-detecting code commonly used in digital networks and storage devices to detect accidental changes to raw data. Blocks of data entering these systems get a short check value attached, based on the remainder of apolynomial division of their contents; on retrieval the calculation is repeated, and corrective action can be taken against presumed data corruption if the check values do not match.
Lets start...
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],c[20],d[20],aux[20],r,i,j,l,in,flag,choice,key;
clrscr();
printf("\n ENTER THE SIZE OF DATA WORD : ");
scanf("%d",&l);
printf("\n ENTER THE DATA WORD : ");
for(i=0; i<l; i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF REDUNDANT BITS : ");
scanf("%d",&r);
printf("\n ENTER THE DIVISOR : ");
for(i=0; i<(r+1); i++)
scanf("%d",&d[i]);
for(i=0; i<(l+r); i++){
if(i<l) c[i]=a[i];
else c[i]=0;
}
printf("\n\n ... GENERATOR MODULE ... \n");
printf("\n\n THE INTERMEDIATE CODE WORD IS : ");
for(i=0; i<(l+r); i++)
printf("%d ",c[i]);
for(i=0; i<(l+r); i++)
aux[i]=c[i];
for(i=0; i<l; i++){
in=1;
if(c[i]==1){
for(j=i+1; j<(i+l); j++){
c[j]=c[j]^d[in];
in++;
}
}
else{
for(j=i+1; j<(i+l); j++)
c[j]=c[j]^0;
}
}
printf("\n\n THE REMAINDER AFTER DIVISION IS : ");
for(i=l; i<(l+r); i++)
printf("%d ",c[i]);
printf("\n\n SENDER CODE WORD : ");
for(i=0; i<(l+r); i++){
if(i<l) c[i]=aux[i];
printf("%d ",c[i]);
}
printf("\n\n\n PRESS 1 IF YOU WANT TO CHANGE A BIT OR 0 TO CONTINUE : ");
scanf("%d",&choice);
if(choice==1){
printf("\n\n ENTER THE BIT YOU WANT TO CHANGE :");
scanf("%d",&key);
for(i=0; i<(l+r); i++){
if(i==(key-1)){
if(c[key-1]==0) c[key-1]=1;
else c[key-1]=0;
}
}
printf("\n\n\n CODE WORD AFTER ERROR : ");
for(i=0; i<(l+r); i++)
printf("%d ",c[i]);
}
else printf("\n\n\n NO ERROR INSERTED IN CODE WORD... ");
printf("\n\n\n\n ... CHECKER MODULE ... ");
for(i=0; i<l; i++){
in=1;
if(c[i]==1){
for(j=i+1; j<(i+l); j++){
c[j]=c[j]^d[in];
in++;
}
}
else{
for(j=i+1; j<(i+l); j++)
c[j]=c[j]^0;
}
}
printf("\n\n\n THE SYNDROME ARRAY IS : ");
for(i=l; i<(l+r); i++)
printf("%d ",c[i]);
flag=0;
for(i=l; i<(l+r); i++){
if(c[i]!=0){
flag=1;
break;
}
}
if(flag==1) printf("\n\n\n ERROR DETECTED !!!");
else printf("\n\n\n NO ERROR FOUND");
getch();
}
Hamming Code Program
(1 bit error correction)
Lab Exams are coming and this program is very vital for CSE Students. This program can run on any Linux distribution OS. so, if you want to run it on turbo C then give header file <conio.h> and getch(); at end (before } ). And also write clrscr(); to clear the screen(remove system("clear");)
lets start...
#include<stdio.h>
#include<math.h>
int main()
{
int a[20],b[20],c[20],d[20],i,k,m,f,n,j,r,p,x,y,z,ch,key,q,v,sum=0;
system("clear");
printf("\n ENTER THE LENGTH OF DATA WORD :");
scanf("%d",&k);
printf("\n ENTER THE DATA WORD \n");
for(i=1; i<=k; i++){
scanf("%d",&a[i]);
}
m=1;
while((k+m+1)>=pow(2,m)){
m++;
}
printf("\n Value of m is : %d",m);
n=k+m;
j=1; r=0;
for(i=1; i<=n; i++){
p=pow(2,r);
if(i==p){
b[i]=0;
r++;
}
else{
b[i]=a[j];
j++;
}
}
printf("\n INTERMEDIATE CODE WORD IS \n");
for(i=1; i<=n; i++)
printf("%d",b[i]);
p=0;
for(i=1; i<=m; i++)
{
x=pow(2,p); r=1;
for(j=x; j<=n; j=j+(x*2)){
for(y=j; y<(j+x); y++){
c[r]=b[y];
r++;
}
}
z=0;
for(y=1; y<=(r-1); y++)
{
if(c[y]==1) z++;
}
if(z%2==0)
b[x]=0;
else
b[x]=1;
for(y=1; y<=20; y++)
c[y]=0;
p++;
}
printf("\n\n THE HAMMING CODE IS \n");
for(i=1; i<=n; i++)
printf("%d",b[i]);
while(1){
printf("\n\n PRESS 1 TO ALTER A BIT 0 to EXIT \n\n");
scanf("%d",&ch);
if(ch==1){
printf("\n ENTER THE BIT YOU WANT TO CHANGE \n");
scanf("%d",&key);
for(i=1; i<=n; i++){
if(i==key){
if(b[key]==1) b[key]=0;
else b[key]=1;
break;
}
}
printf("\n THE NEW CODE IS \n\n");
for(i=1; i<=n; i++)
printf("%d",b[i]);
}
else
break;
}
p=0; q=0;
for(i=1; i<=m; i++)
{
x=pow(2,p); r=1;
for(j=x; j<=n; j=j+(x*2)){
for(y=j; y<(j+x); y++){
c[r]=b[y];
r++;
}
}
z=0;
for(y=1; y<=(r-1); y++)
{
if(c[y]==1) z++;
}
v=z%2;
d[q]=v;
sum=sum+(v*pow(2,q));
q++;
for(y=1; y<=20; y++)
c[y]=0;
p++;
}
if(sum==0)
printf("\n\n NO ERROR FOUND....... \n\n");
else
printf("\n\n ERROR AT POSITION %d",sum);
printf("\n");
return 0;
}
0 comments:
Post a Comment