-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCDMA.java
More file actions
124 lines (96 loc) · 3.62 KB
/
Copy pathCDMA.java
File metadata and controls
124 lines (96 loc) · 3.62 KB
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
package cdma;
import java.util.*;
/**
*
* @author Mike
*/
public class CDMA {
private int[][] wtable;
private int[][] copy;
private int[] channel_sequence;
private void setUp(int[] data, int num_stations) {
wtable = new int[num_stations][num_stations];
copy = new int[num_stations][num_stations];
buildWalshTable(num_stations, 0, num_stations - 1, 0, num_stations - 1, false);
showWalshTable(num_stations);
for (int i = 0; i < num_stations; i++) {
for (int j = 0; j < num_stations; j++) {
//Making a opy of walsh table
//to be used later
copy[i][j] = wtable[i][j];
// each row in table is code for one station.
// So we multiply each row with station data
wtable[i][j] *= data[i];
}
}
channel_sequence = new int[num_stations];
for (int i = 0; i < num_stations; i++) {
for (int j = 0; j < num_stations; j++) {
// Adding all sequances to get channel sequance
channel_sequence[i] += wtable[j][i];
}
}
}
public void listenTo(int sourceStation, int num_stations) {
int innerProduct = 0;
for (int i = 0; i < num_stations; i++) {
// multiply channel sequance and source station code
innerProduct += copy[sourceStation][i] * channel_sequence[i];
}
System.out.println("The data received is: " + (innerProduct / num_stations));
}
public int buildWalshTable(int len, int i1, int i2, int j1, int j2, boolean isBar) {
// len = size of matrix. (i1, j1), (i2, j2) are
// starting and ending indices of wtable.
// isBar represents whether we want to add simple entry
// or complement(southeast submatrix) to wtable.
if (len == 2) {
if (!isBar) {
wtable[i1][j1] = 1;
wtable[i1][j2] = 1;
wtable[i2][j1] = 1;
wtable[i2][j2] = -1;
} else {
wtable[i1][j1] = -1;
wtable[i1][j1] = -1;
wtable[i1][j1] = -1;
wtable[i1][j1] = 1;
}
return 0;
}
int midi = (i1 + i2) / 2;
int midj = (j1 + j2) / 2;
buildWalshTable(len / 2, i1, midi, j1, midj, isBar);
buildWalshTable(len / 2, i1, midi, midj + 1, j2, isBar);
buildWalshTable(len / 2, midi + 1, i2, j1, midj, isBar);
buildWalshTable(len / 2, midi + 1, i2, midj + 1, j2, !isBar);
return 0;
}
public void showWalshTable(int num_stations)
{
System.out.print("\n");
for (int i = 0; i < num_stations; i++) {
for (int j = 0; j< num_stations; j++) {
System.out.print(wtable[i][j] + " ");
}
System.out.print("\n");
}
System.out.println("-----------------------");
System.out.print("\n");
}
public static void main(String[] args)
{
int num_stations = 4;
int [] data = new int[num_stations];
//data bits corresponding to each station
data[0] = -1;
data[1] = -1;
data[2] = 0;
data[3] = 1;
CDMA channel = new CDMA();
channel.setUp(data, num_stations);
// station you want to listen to
int sourceStation = 3;
channel.listenTo(sourceStation, num_stations);
}
}