-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConvolCode.java
157 lines (141 loc) · 4.04 KB
/
ConvolCode.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
151
152
153
154
155
156
157
import java.util.Random;
import java.util.Scanner;
/**
* Program to create convolutional code word for
* binary data streams.
* @author Katherine Cabezas
* @version 2.0
*/
public class ConvolCode {
private double[] x;
private Matrix matrixA0;
private Matrix matrixA1;
private Vector yZero;
private Vector yOne;
/**
* Constructor for Encoder. Randomly generates
* binary stream x.
* @param length Desired length for binary stream x
*/
public ConvolCode(int length) {
matrixA0 = new Matrix(length, 3);
matrixA1 = new Matrix(length, 3);
yZero = new Vector(length);
yOne = new Vector(length);
x = new double[length];
Random randNumList = new Random();
for (int i = 0; i < length; i++) {
x[i] = randNumList.nextInt(2);
}
}
/**
* Constructor for Encoder.
* @param stream Assigns this array of integers to x
*/
public ConvolCode(double[] stream) {
matrixA0 = new Matrix(3, 3);
matrixA1 = new Matrix(3, 3);
yZero = new Vector(stream.length);
yOne = new Vector(stream.length);
x = stream;
}
/**
* Returns x binary data stream.
* @return x
*/
public double[] getX() {
return x;
}
/**
* Calculates the ouput stream y0.
* @return output stream y0
*/
public double[] calculateY0() {
double[] y0 = new double[x.length];
for (int i = 0; i < y0.length; i++) {
if ((i - 2) < 0) {
y0[i] = x[i];
matrixA0.set(i, 0, x[i]);
matrixA0.set(i, 1, 0);
matrixA0.set(i, 2, 0);
} else if ((i - 3) < 0) {
y0[i] = x[i] + x[i - 2];
matrixA0.set(i, 0, x[i]);
matrixA0.set(i, 1, x[i - 2]);
matrixA0.set(i, 2, 0);
} else {
y0[i] = x[i] + x[i - 2] + x[i - 3];
matrixA0.set(i, 0, x[i]);
matrixA0.set(i, 1, x[i - 2]);
matrixA0.set(i, 2, x[i - 3]);
}
y0[i] = y0[i] % 2;
}
yZero = new Vector(y0);
return y0;
}
/**
* Calculates the output stream y1.
* @return output stream y1
*/
public double[] calculateY1() {
double[] y1 = new double[x.length];
for (int i = 0; i < y1.length; i++) {
if ((i - 1) < 0) {
y1[i] = x[i];
matrixA1.set(i, 0, x[i]);
matrixA1.set(i, 1, 0);
matrixA1.set(i, 2, 0);
} else if ((i - 3) < 0) {
y1[i] = x[i] + x[i - 1];
matrixA1.set(i, 0, x[i]);
matrixA1.set(i, 1, x[i - 1]);
matrixA1.set(i, 2, 0);
} else {
y1[i] = x[i] + x[i - 1] + x[i - 3];
matrixA1.set(i, 0, x[i]);
matrixA1.set(i, 1, x[i - 1]);
matrixA1.set(i, 2, x[i - 3]);
}
y1[i] = y1[i] % 2;
}
yOne = new Vector(y1);
return y1;
}
public Vector getY0() {
return yZero;
}
public Vector getY1() {
return yOne;
}
public Matrix getMatrixA0() {
return matrixA0;
}
public Matrix getMatrixA1() {
return matrixA1;
}
/**
* Method to create convolutional code word y obtained.
* @param stream Binary data stream
* @return convolutional code word y
*/
public String[] encode() {
double[] y0 = calculateY0();
double[] y1 = calculateY1();
String[] y = new String[x.length];
for (int i = 0; i < x.length; i++) {
if (y0[i] == 1 && y1[i] == 1) {
y[i] = "11";
} else if (y0[i] == 0 && y1[i] == 1) {
y[i] = "01";
} else if (y0[i] == 1 && y1[i] == 0) {
y[i] = "10";
} else {
y[i] = "00";
}
}
return y;
}
public static void main(String[] args) {
}
}