postheadericon Block Parity Check - Error Detection and Correction Techniques



Sender / Block Parity Generator 


//block parity sender
#include<stdio.h>
#include<string.h>
#include<fcntl.h>

// one bit for NULL char and
// second for Parity bit

#define Frame_Size 32 // size of frame in bit

#define ROW 4+1 //segments
#define COL 8+2 // m bits in a segment [ +2 for '*' and '\0' ]


int getBit(int frameData,int pos)
{
//get binary equavalient of our Frame Data by setting on bit by bit

int code=1; // take base of binary 1 [ 00000001 ]

code=code<<(pos-1); // do the left shift to find  postion

if(frameData & code)  // if both bits are 1 then set bit otherwise return ZERO
return 1;
else
return 0;

}


void blockParity(char myframe[ROW][COL])
{
int i=0,j=0,ROW_count=0,COL_count=0;

//[ Horizontal parity ]
for(i=0;i<ROW;i++)//ROW's Parity Check
{
ROW_count=0;
for(j=0;j<COL-2;j++) //we r using -2 because we want to exclude last two bit that is * and NULL
{
if(myframe[i][j] == '1')//count ROW Parity
{
ROW_count++;
}
}
if((ROW_count % 2) == 0) //set ROW parity bit
myframe[i][j] = '0' ;
else
myframe[i][j] = '1';
}

//[ Virtical parity ]
for(i=0;i<COL-2;i++)//COL's Parity
{
COL_count=0;
for(j=0;j<ROW;j++)
{
if(myframe[j][i] == '1')//count COL Parity
{
COL_count++;
}

}
if((COL_count % 2) == 0) //set COL parity bit
myframe[ROW-1][i] = '0' ;
else
myframe[ROW-1][i] = '1';

}

}



void main()
{
int i,k=0,j=0,pid,num,frameData;

char targeted_frameData[Frame_Size+(COL)+(ROW-1)];
char codeword[Frame_Size+(COL)+(ROW-1)];
char myframe[ROW][COL];
system(">pipe");
system("clear");
pid=open("pipe",O_WRONLY);

//Enter Frame Data in decimal Number
printf("\nEnter Frame Data : ");
scanf("%d",&frameData);

//create decimal to binary data to row bits
  for(i=Frame_Size;i>0;i--)
{
targeted_frameData[k++]=getBit(frameData,i) + 48;
}
targeted_frameData[k++]='\0';

printf("\nRow bit : %s",targeted_frameData);

//sub devide FRAME in to [ m ] bit Segment
k=0;
for(i=0;i<ROW;i++)
{

for(j=0;j<COL;j++)
{
if(i==ROW-1)//Make reserved LAST ROW for Parity of Columns
{
if(j==COL-1)
{
myframe[i][j]='\0'; //complete last string by assigning NULL
}
else
{
myframe[i][j]='*';
}
}
else
{
if(j==COL-2)
{
myframe[i][j]='*'; // make reserved last Column for parity bit
}
else if(j==COL-1)
{
myframe[i][j]='\0'; //Complete string[i] by assigning NULL
}
else
{
myframe[i][j]=targeted_frameData[k++]; //copy charechter by Charecter from raw bits to 2D array
}
}
}

}

printf("\n\nBefore Parity check segments........................ ");
for(i=0;i<ROW;i++)
{
printf("\nSegmant of data    [ %d ] : %s",i,myframe[i]);
}
blockParity(myframe);
printf("\n\nBefore Parity check segments........................ ");
for(i=0;i<ROW;i++)
{
printf("\nsegmat of Codeword [ %d ] : %s",i,myframe[i]);
strcat(codeword,myframe[i]);//combine all segment in two one codeword
}
printf("\nFrame to be send : %s\n",codeword);
write(pid,&codeword,sizeof(codeword));
}


Receiver



//block parity reciver
#include<stdio.h>
#include<string.h>
#include<fcntl.h>

// one bit for NULL char and
// second for Parity bit

#define Frame_Size 32 // size of frame in bit

#define ROW 4+1 //segments
#define COL 8+2 // m bits in a segment [ +2 for '*' and '\0' ]


int getBit(int frameData,int pos)
{
//get binary equavalient of our Frame Data by setting on bit by bit

int code=1; // take base of binary 1 [ 00000001 ]

code=code<<(pos-1); // do the left shift to find  postion

if(frameData & code)  // if both bits are 1 then set bit otherwise return ZERO
return 1;
else
return 0;

}


int blockParity(char myframe[ROW][COL])
{
int i=0,j=0,ROW_count=0,COL_count=0;

//[ Horizontal parity ]
for(i=0;i<ROW;i++)//ROW's Parity Check
{
ROW_count=0;
for(j=0;j<COL-2;j++) //we r using -2 because we want to exclude last two bit that is * and NULL
{
if(myframe[i][j] == '1')//count ROW Parity
{
ROW_count++;
}
}

if( myframe[i][COL-1] =='0' && ROW_count%2!=0 ) //check ROW parity bit with newly found parity
{
//if row count of ONE having odd ONEs and perivous parity bit is ZERO
//ERROR in Data

return 1; //error in data
}
}

//[ Virtical parity ]
for(i=0;i<COL-2;i++)//COL's Parity
{
COL_count=0;
for(j=0;j<ROW;j++)
{
if(myframe[j][i] == '1')//count COL Parity
{
COL_count++;
}

}

if(myframe[ROW-1][i]=='0' && COL_count%2!=0) //check ROW parity bit with newly found parity
{
//if column count of ONE having odd ONEs and perivous parity bit is ZERO
//ERROR in Data
return 1;
}
}
return 0; //no error found in data

}
void main()
{
int i,k=0,j=0,pid,num,frameData,ans;

char targeted_frameData[Frame_Size+(COL)+(ROW-1)];
char myframe[ROW][COL];

system("clear");

pid=open("pipe",O_RDONLY);
read(pid,&targeted_frameData,sizeof(targeted_frameData));


printf("\nRow bit : %s",targeted_frameData);

//sub devide FRAME in to [ m ] bit Segment
k=0;
for(i=0;i<ROW;i++)
{

for(j=0;j<COL;j++)
{
if(j==COL-1)
{
myframe[i][j]='\0'; //Complete string[i] by assigning NULL
}
else
{
myframe[i][j]=targeted_frameData[k++]; //copy charechter by Charecter from raw bits to 2D array
}
}

}
printf("\n\nBefore Parity check segments........................ ");
for(i=0;i<ROW;i++)
{
printf("\nSegmant of data    [ %d ] : %s",i,myframe[i]);
}
ans=blockParity(myframe);
if(ans==1)//error
printf("\nError in Data....!");
else
printf("\nNo Error found ACCEPT DATA");
printf("\n");

}

Error Generator - Generate Error in Frame Data 


#include<stdio.h>
#include<fcntl.h>
#include<string.h>
void dispFrameData(char targeted_frameData[])
{
int i=0;
//print data from named pipe
while(targeted_frameData[i]!='\0')
{
printf("%c",targeted_frameData[i++]);
}

}

void generateError(char targeted_frameData[])
{
int f=1,pos;
do
{
printf("Enter bit Position : ");
scanf("%d",&pos);
if(pos>=0 && pos<45)
{
if(targeted_frameData[pos]=='1')
targeted_frameData[pos]='0';
else
targeted_frameData[pos]='1';
}
else
{
printf("\n\t Invalid Position...!\n");
}
dispFrameData(targeted_frameData);
printf("\nDo u want to generate more ERROR ? [1 / 0] : ");
scanf("%d",&f);
}while(f==1);
}
void main()
{
int pid,i=0,parrity;
char targeted_frameData[45];
pid=open("pipe",O_RDONLY);
read(pid,&targeted_frameData,sizeof(targeted_frameData));
close(pid);
printf("\n\nFrame Data\n\n");

dispFrameData(targeted_frameData);

generateError(targeted_frameData);

pid=open("pipe",O_WRONLY);
write(pid,&targeted_frameData,sizeof(targeted_frameData));
close(pid);
}

0 comments:

Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.