-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMultiple Access.py
344 lines (318 loc) · 11.9 KB
/
Multiple Access.py
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import random
import numpy as np
import sys
sys.setrecursionlimit(10000)
from termcolor import cprint
class Node:
def __init__(self, Name, Size, BitRate):
self.Name = Name
self.Size = Size
self.BitRate = BitRate
self.Array = []
self.Pointer = 0
self.Finished = False
self.Slots = 0.0
self.Share = 0
self.E = 2
def initiate_Part1():
node_list = []
node_A = Node('A', 110, 5)
node_A.Array = [1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 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, 1, 0, 0, 1, 0, 0]
node_list.append(node_D)
return node_list
def initiate_Part2():
node_list = []
for i in range(1, 101):
node_A = Node(str(i) + "A", 110, 5)
node_A.Array = np.random.randint(2, size=node_A.Size)
node_list.append(node_A)
for i in range(101, 201):
node_B = Node(str(i) + "B", 75, 3)
node_B.Array = np.random.randint(2, size=node_B.Size)
node_list.append(node_B)
for i in range(201, 301):
node_C = Node(str(i) + "C", 94, 2)
node_C.Array = np.random.randint(2, size=node_C.Size)
node_list.append(node_C)
for i in range(301, 400):
node_D = Node(str(i) + "D", 60, 4)
node_D.Array = np.random.randint(2, size=node_D.Size)
node_list.append(node_D)
return node_list
def send_TDMA(node):
data = ""
rand_number = random.uniform(0, 1)
P = 0.5
data_size = node.BitRate + node.Pointer
if rand_number > P:
data = "NULL"
else:
while node.Pointer < data_size:
if node.Size - node.Pointer < node.BitRate:
node.Finished = True
data += str(node.Array[node.Pointer])
node.Pointer += 1
return data
def fixed_TDMA(node_list):
time_frame = node_list
sent_data = []
while True:
if len(time_frame) == 0:
break
for i in time_frame:
if i.Finished:
print()
cprint("Node ", 'magenta', end='')
cprint(i.Name, 'magenta', end='')
cprint(" Data transformation has ended", 'magenta')
print()
time_frame.remove(i)
# remove completed node from node list
for j in node_list:
if j.Name == i.Name:
node_list.remove(j)
# remove completed node from time frame
for k in time_frame:
if k.Name == i.Name:
time_frame.remove(k)
else:
sent_data.append(send_TDMA(i))
if len(sent_data) != 0:
cprint("TDMA Sent Data: ", 'blue')
for t in sent_data:
print(t, end=' ')
print()
sent_data.clear()
def dynamic_TDMA(node_list):
time_frame = dynamic(node_list)
sent_data = []
while True:
if len(time_frame) == 0:
break
time_frame = dynamic(node_list)
for i in time_frame:
if i.Finished:
print()
cprint("Node ", 'magenta', end='')
cprint(i.Name, 'magenta', end='')
cprint(" Data transformation has ended", 'magenta')
print()
time_frame.remove(i)
# remove completed node from node list
for j in node_list:
if j.Name == i.Name:
node_list.remove(j)
# remove completed node from time frame
for k in time_frame:
if k.Name == i.Name:
time_frame.remove(k)
else:
sent_data.append(send_TDMA(i))
'''if len(sent_data) != 0:
cprint("D-TDMA Sent Data: ", 'blue')
for t in sent_data:
print(t, end=' ')
print()'''
sent_data.clear()
def dynamic(node_list):
Total = 0.0
tdma_fr = []
for i in node_list:
i.Slots = ((i.Size - i.Pointer) / i.BitRate) * i.E
Total += i.Slots
if Total != 0:
for i in node_list:
i.Share = int((i.Slots / Total) * 1000)
for i in node_list:
for j in range(i.Share):
tdma_fr.append(i)
'''for i in tdma_fr:
print(i.Name, i.Share, i.Size - i.Pointer)'''
return tdma_fr
def CDMA(node_list, size):
if size == 4:
w_table = [[0 for m in range(4)] for n in range(4)]
build_walsh_table(w_table, 4, 0, 3, 0, 3)
show_walsh_table(w_table, 4)
elif size == 400:
w_table = [[0 for m in range(512)] for n in range(512)]
build_walsh_table(w_table, 512, 0, 511, 0, 511)
#show_walsh_table(w_table, 512)
cprint("Sending Bits...", 'magenta')
capacity = 30
sent = []
i = 0
while True:
if finish(node_list):
break
else:
summation = []
temp_2 = []
if size == 4:
for j in range(4):
bit = int(get_bit(node_list[j]))
if bit == 2: # null/silence
summation.append(bit_code_multiplication(0, w_table, j))
elif bit == 1:
summation.append(bit_code_multiplication(1, w_table, j))
elif bit == 0:
summation.append(bit_code_multiplication(-1, w_table, j))
else:
for j in range(512):
if j > 398:
summation.append(bit_code_multiplication(0, w_table, j))
else:
bit = int(get_bit(node_list[j]))
if bit == 2: # null/silence
summation.append(bit_code_multiplication(0, w_table, j))
elif bit == 1:
summation.append(bit_code_multiplication(1, w_table, j))
elif bit == 0:
summation.append(bit_code_multiplication(-1, w_table, j))
for m in range(len(summation)):
temp_1 = 0
for n in range(len(summation[m])):
temp_1 += summation[n][m]
temp_2.append(temp_1)
if size == 4:
cprint(i, 'magenta', end='')
cprint(" -> Signal Summation Code: ", 'green', end='')
print_array(temp_2)
print()
for k in range(len(node_list)):
cprint('d', 'cyan', end='')
cprint(k, 'cyan', end='')
cprint(" = ", end='')
print_array(temp_2)
cprint('.', 'cyan', end='')
print_array(get_code(w_table, k))
print(" = ", end='')
print(code_code_multiplication(temp_2, get_code(w_table, k)))
print()
if len(sent) < 30:
sent.append(temp_2)
capacity -= 1
else:
sent.clear()
cprint("FULL CAPACITY! Channel Cleared", 'red')
print()
capacity = 30
sent.append(temp_2)
i += 1
def build_walsh_table(array, size, i1, i2, j1, j2, flag=False):
if size == 2:
if not flag:
array[i1][j1] = 1
array[i1][j2] = 1
array[i2][j1] = 1
array[i2][j2] = -1
else:
array[i1][j1] = -1
array[i1][j2] = -1
array[i2][j1] = -1
array[i2][j2] = 1
return
midi = (i1 + i2) // 2
midj = (j1 + j2) // 2
build_walsh_table(array, size // 2, i1, midi, j1, midj, flag)
build_walsh_table(array, size // 2, i1, midi, midj + 1, j2, flag)
build_walsh_table(array, size // 2, midi + 1, i2, j1, midj, flag)
build_walsh_table(array, size // 2, midi + 1, i2, midj + 1, j2, not flag)
return
def show_walsh_table(array, num_nodes):
print()
for i in range(num_nodes):
for j in range(num_nodes):
print(format(array[i][j], "2d"), " ", end='')
print()
print()
def get_code(w_table, node_number):
array = []
for i in range(len(w_table)):
if i == node_number:
for j in range(len(w_table)):
array.append(w_table[i][j])
return array
def get_bit(node):
data = ""
rand_number = random.uniform(0, 1)
P = 0.5
if rand_number > P or node.Finished:
data = "2" # null/silence
return data
else:
data += str(node.Array[node.Pointer])
if node.Pointer + 1 == node.Size:
node.Finished = True
node.Pointer += 1
return data # 0/1
def bit_code_multiplication(bit, w_table, node_number):
code_array = get_code(w_table, node_number)
summation = []
for i in range(len(code_array)):
summation.append(bit * code_array[i])
return summation
def code_code_multiplication(signal_sum, code):
summation = 0
for i in range(len(signal_sum)):
summation += signal_sum[i] * code[i]
if summation >= 1:
return "1"
elif summation <= -1:
return "0"
else:
return "NULL" # silence
def finish(node_list):
for i in node_list:
if not i.Finished:
return False
return True
def print_array(array):
cprint("(", 'cyan', end='')
for i in range(len(array) - 1):
print(format(array[i], "2d"), " ", end='')
print(format(array[len(array) - 1], "2d"), end='')
cprint(" )", 'cyan', end='')
def main():
Part = eval(input("1. Project Part A\n2. Project Part B\nYour Choice: "))
if Part == 1:
node_list = initiate_Part1()
Method = eval(input("1. Fixed TDMA\n2. Dynamic TDMA\n3. CDMA\nPlease Choose Sending Method: "))
if Method == 1:
print()
fixed_TDMA(node_list)
elif Method == 2:
print()
dynamic_TDMA(node_list)
else:
CDMA(node_list, 4)
if Part == 2:
node_list = initiate_Part2()
Method = eval(input("1. Fixed TDMA\n2. Dynamic TDMA\n3. CDMA\nPlease Choose Sending Method: "))
if Method == 1:
print()
fixed_TDMA(node_list)
elif Method == 2:
print()
dynamic_TDMA(node_list)
else:
CDMA(node_list, 400)
print("Finished")
main()