Ex.
No:10 Simulation of an error correction code (like CRC)
Aim: To simulate the behaviour of Error Correction Code in java
Algorithm:
Step 1: Get the input in the form of bits.
Step 2: Append zeros as redundancy bits.
Step 3: Divide the appended data using a divisor polynomial.
Step 4: The resulting data should be transmitted to the receiver.
Step 5: At the receiver the received data is entered.
Step 6: The same process is repeated at the receiver.
Step 7: If the remainder is zero there is no error otherwise there is some error in the received
bits
Step 8: Run the program.
Program:
import [Link];
class CRC{
public static void main(String args[]){
Scanner sc = new Scanner([Link]);
//Input Data Stream
[Link]("Enter data stream: ");
String datastream = [Link]();
[Link]("Enter generator: ");
String generator = [Link]();
int data[] = new int[[Link]() + [Link]() - 1];
int divisor[] = new int[[Link]()];
for(int i=0;i<[Link]();i++)
data[i] = [Link]([Link](i)+"");
for(int i=0;i<[Link]();i++)
divisor[i] = [Link]([Link](i)+"");
//Calculation of CRC
for(int i=0;i<[Link]();i++){
if(data[i]==1)
for(int j=0;j<[Link];j++)
data[i+j] ^= divisor[j];
//Display CRC
[Link]("The CRC code is: ");
for(int i=0;i<[Link]();i++)
data[i] = [Link]([Link](i)+"");
for(int i=0;i<[Link];i++) [Link](data[i]);
[Link]();
//Check for input CRC code
[Link]("Enter CRC code: ");
datastream = [Link]();
[Link]("Enter generator: ");
generator = [Link]();
data = new int[[Link]() + [Link]() - 1];
divisor = new int[[Link]()];
for(int i=0;i<[Link]();i++)
data[i] = [Link]([Link](i)+"");
for(int i=0;i<[Link]();i++)
divisor[i] = [Link]([Link](i)+"");
//Calculation of remainder
for(int i=0;i<[Link]();i++){
if(data[i]==1)
for(int j=0;j<[Link];j++)
data[i+j] ^= divisor[j];
//Display validity of data
boolean valid = true;
for(int i=0;i<[Link];i++)
if(data[i]==1){
valid = false;
break;
if(valid==true) [Link]("Data stream is valid");
else [Link]("Data stream is invalid. CRC error occured.");
}
//[Link]
//[Link]
Output:
/*Output
Enter data stream: 101010
Enter generator: 11111
The CRC code is: 1010101010
Enter CRC code: 1010100010
Enter generator: 11111
Data stream is invalid. CRC error occurred.
*/
Result:
Thus the java program to simulate the behavior of Error Correction Code is executed
successfully.