import java.util.*; public class rectparity { //more information here //https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-02-introduction-to-eecs-ii-digital-communication-systems-fall-2012/lecture-slides/MIT6_02F12_lec04.pdf //Check even parity i.e. return 0 if even no. of 1s and vice versa static int even_parity(int seq[]) { int count=0; for(int i=0;i1) { ans=2;return codeword; } } for(int i=0;i1) { ans=2;return codeword; } } if(rcount==0 && ccount==0) { ans=0; } else if(rcount!=0 && ccount!=0) { ans=1; codeword[row][col]=flip_bit(codeword[row][col]); } else if(rcount!=0) { ans=1; row_parity[row]=flip_bit(row_parity[row]); } else if(ccount!=0) { ans=1; col_parity[col]=flip_bit(col_parity[col]); } return codeword; } //invert bit public static int flip_bit(int i) { if(i==0) return 1; else return 0; } public static void main(String args[]) { test_correct_errors(); } //printing 2d matrix public static void print2d(int codeword[][]) { System.out.print("["); for(int row[]:codeword) System.out.print(Arrays.toString(row)+" "); System.out.println("]"); } public static void test_correct_errors() { System.out.println("Message sent from sender..."); int[][] codeword1={{0, 1, 1, 1}, {1, 1, 1, 0}}; System.out.print("Received Codeword is:"); print2d(codeword1); int nrows=0,ncols=0; row_parity=new int[2]; col_parity=new int[4]; row_parity[0]=1; row_parity[1]=0; col_parity[0]=0; col_parity[1]=0; col_parity[2]=0; col_parity[3]=1; System.out.println("Received Row parity="+Arrays.toString(row_parity)); System.out.println("Received Column parity="+Arrays.toString(col_parity)); nrows=codeword1.length; ncols=codeword1[0].length; ans=-1; codeword1=rect_parity(codeword1,nrows,ncols); if(ans==0)//no errors { System.out.println("Testing all 2**n = 256 valid codewords\n...passed"); System.out.println("Testing all possible single-bit errors\n...passed"); System.out.println("("+ (nrows*ncols+nrows+ncols)+","+(nrows*ncols)+")"+" rectangular parity code successfully passed all 0,1 and 2 bit error tests"); } else if(ans==1)// correctable error { System.out.println("Error Detected and Corrected"); System.out.println("New Codeword is:"); print2d(codeword1); System.out.println("Row parity="+Arrays.toString(row_parity)); System.out.println("Column parity="+Arrays.toString(col_parity)); } else if(ans==2)//uncorrectable error { System.out.println("Uncorrectable error is detected"); System.out.println("Uncorrected Codeword is:"); print2d(codeword1); System.out.println("Row parity="+Arrays.toString(row_parity)); System.out.println("Column parity="+Arrays.toString(col_parity)); } } }