-
Notifications
You must be signed in to change notification settings - Fork 292
/
Copy pathrectparity.java
150 lines (145 loc) · 4.79 KB
/
rectparity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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;i<seq.length;i++)
{
if(seq[i]==1)
count++;
}
if(count%2==0)
return 0;
else
return 1;
}
static int ans=-1;
static int row_parity[];
static int col_parity[];
//checking rectangular parity
static int[][] rect_parity(int codeword[][],int nrows,int ncols)
{
int c=0,rcount=0,ccount=0,col=0,row=0,m=0,getparity=0;
for(int rows[]:codeword)
{
getparity=even_parity(rows);
if(getparity!=row_parity[c])
{
rcount++;
row=c;
}
c++;
if(rcount>1)
{
ans=2;return codeword;
}
}
for(int i=0;i<ncols;i++)
{
int colsarray[]=new int[nrows];
m=0;
for(int j=0;j<nrows;j++)
{
colsarray[m]=codeword[j][i];
m++;
}
getparity=even_parity(colsarray);
if(getparity!=col_parity[i])
{
ccount++;
col=i;
}
if(ccount>1)
{
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));
}
}
}