-
Notifications
You must be signed in to change notification settings - Fork 0
/
prelude.py
342 lines (280 loc) · 11.2 KB
/
prelude.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
import itertools
import multiprocessing
import os
import pathlib
import numpy as np
import tensorflow as tf
from mpmath import besselj
from global_settings import *
QPSK_CANDIDATE_SIZE = 2 ** (2 * NUM_ANT)
QPSK_CANDIDATES = np.array([x for x in itertools.product([1, -1], repeat=2 * NUM_ANT)]).T / np.sqrt(2)
def jbtest(data):
mean_value = np.mean(data)
s = np.mean((data - mean_value) ** 3) / (np.mean((data - mean_value) ** 2) ** (3 / 2))
k = np.mean((data - mean_value) ** 4) / (np.mean((data - mean_value) ** 2) ** (4 / 2))
return s ** 2 / 6 + (k - 3) ** 2 / 24
def get_bits(x):
return np.where(x < 0, 0, 1)
def mkdir(file_path):
folder = os.path.dirname(file_path)
if not os.path.exists(folder):
os.makedirs(folder)
def mkfile(file_path):
mkdir(file_path)
filename = pathlib.Path(file_path)
filename.touch(exist_ok=True)
def concatenate(total, part):
return part if total is None else np.concatenate((total, part))
def tf_concat(a, b, axis):
if a is None:
return b
elif b is None:
return a
else:
return tf.concat([a, b], axis=axis)
def random_h_batch():
h_batch = None
temp = 2 * np.pi * NORMALIZED_DOPPLER_FREQUENCY
rho_h = float(besselj(0, temp))
prev_h = None
for _ in range(PACKETS_PER_BATCH):
for t in range(TRANSMIT_TIMES_PER_PACKET):
# 由于tensorflow不支持复数直接运算,所以我们需要分割为实部和虚部的形式
real = np.random.randn(NUM_ANT, NUM_ANT)
imag = np.random.randn(NUM_ANT, NUM_ANT)
h = np.row_stack(
(
np.column_stack((real, -imag)),
np.column_stack((imag, real)),
)
)
h = h.reshape([1, 2 * NUM_ANT, 2 * NUM_ANT])
if t == 0:
h_batch = concatenate(h_batch, h)
prev_h = h
else:
doppler_h = rho_h * prev_h + np.sqrt(1 - rho_h * rho_h) * h
h_batch = concatenate(h_batch, doppler_h)
prev_h = doppler_h
return h_batch
def random_s_batch():
s_batch = None
one_hot_batch = np.zeros([TRANSMIT_TIMES_PER_BATCH, QPSK_CANDIDATE_SIZE])
random_indexes = np.random.uniform(low=0, high=QPSK_CANDIDATE_SIZE, size=TRANSMIT_TIMES_PER_BATCH)
for t in range(TRANSMIT_TIMES_PER_BATCH):
i = int(random_indexes[t])
one_hot_batch[t, i] = 1
s = QPSK_CANDIDATES[:, i:i + 1]
s = s.reshape([1, 2 * NUM_ANT, 1])
s_batch = concatenate(s_batch, s)
return s_batch, one_hot_batch
def time_correlated_interference_batch(rho: float):
w_batch = None
for t in range(TRANSMIT_TIMES_PER_BATCH):
u = np.random.randn(1, 2 * NUM_ANT, 1)
if t == 0:
w_batch = concatenate(w_batch, u)
else:
w_prev = w_batch[t - 1:t, :, :]
w = np.sqrt(rho) * w_prev + np.sqrt(1 - rho) * u
w_batch = concatenate(w_batch, w)
return w_batch
def mld_batch(y, h):
s_estimated_batch = None
dst = np.sum(np.square(y - h @ QPSK_CANDIDATES), axis=1)
min_indexes_on_axis_1 = np.unravel_index(dst.argmin(1), dst.shape)[1]
for t in range(TRANSMIT_TIMES_PER_BATCH):
index = min_indexes_on_axis_1[t]
s_estimated = QPSK_CANDIDATES[:, index]
s_estimated = s_estimated.reshape([1, 2 * NUM_ANT, 1])
s_estimated_batch = concatenate(s_estimated_batch, s_estimated)
return s_estimated_batch
def produce_data_batch(rho, sir):
power = 10 ** (sir / 10)
h = np.sqrt(power / NUM_ANT) * random_h_batch()
s, one_hot = random_s_batch()
w = time_correlated_interference_batch(rho)
y = h @ s + w
hat_s = mld_batch(y, h)
hat_w = y - h @ hat_s
return y, h, s, one_hot, w, hat_s, hat_w
class DataSet:
def __init__(self, flag, rho: float, sir: float):
self.flag = flag
self.rho = rho
self.sir = sir
def __open_file(self, name, mode):
if self.flag == 0:
file_name = "savedData/rho{:.1f}_sir{}/train/{}".format(
self.rho, self.sir, name)
elif self.flag == 1:
file_name = "savedData/rho{:.1f}_sir{}/valid/{}".format(
self.rho, self.sir, name)
else:
file_name = "savedData/rho{:.1f}_sir{}/test/{}".format(
self.rho, self.sir, name)
mkfile(file_name)
return open(file_name, mode)
def produce_func(self, _idx):
return produce_data_batch(self.rho, self.sir)
def __open_all(self, mode):
file_y = self.__open_file("y", mode)
file_h = self.__open_file("h", mode)
file_s = self.__open_file("s", mode)
file_one_hot = self.__open_file("one_hot", mode)
file_w = self.__open_file("w", mode)
file_s_mld = self.__open_file("s_mld", mode)
file_w_mld = self.__open_file("w_mld", mode)
return file_y, file_h, file_s, file_one_hot, file_w, file_s_mld, file_w_mld
def __delete_file(self, name):
if self.flag == 0:
file_name = "savedData/rho{:.1f}_sir{}/train/{}".format(
self.rho, self.sir, name)
elif self.flag == 1:
file_name = "savedData/rho{:.1f}_sir{}/valid/{}".format(
self.rho, self.sir, name)
else:
file_name = "savedData/rho{:.1f}_sir{}/test/{}".format(
self.rho, self.sir, name)
if os.path.exists(file_name):
os.remove(file_name)
def delete_all(self):
self.__delete_file("y")
self.__delete_file("h")
self.__delete_file("s")
self.__delete_file("one_hot")
self.__delete_file("w")
self.__delete_file("s_mld")
self.__delete_file("w_mld")
def produce_all(self):
file_y, file_h, file_s, file_one_hot, file_w, file_s_mld, file_w_mld = self.__open_all("wb")
if self.flag == 0:
total_batch = TRAIN_TOTAL_BATCH
elif self.flag == 1:
total_batch = VALID_TOTAL_BATCH
else:
total_batch = TEST_TOTAL_BATCH
if NUM_WORKERS > 0:
pool = multiprocessing.pool.Pool(NUM_WORKERS, maxtasksperchild=MAX_TASKS_PER_CHILD)
else:
pool = multiprocessing.pool.Pool(maxtasksperchild=MAX_TASKS_PER_CHILD)
idx = 0
for ret_value in pool.imap(self.produce_func, range(total_batch)):
if self.flag == 0:
print("Train set,batch {}/{}".format(idx + 1, total_batch), end="\r")
elif self.flag == 1:
print("Valid set,batch {}/{}".format(idx + 1, total_batch), end="\r")
else:
print("Test set,batch {}/{}".format(idx + 1, total_batch), end="\r")
ret_value[0].astype(np.float32).tofile(file_y)
ret_value[1].astype(np.float32).tofile(file_h)
ret_value[2].astype(np.float32).tofile(file_s)
ret_value[3].astype(np.float32).tofile(file_one_hot)
ret_value[4].astype(np.float32).tofile(file_w)
ret_value[5].astype(np.float32).tofile(file_s_mld)
ret_value[6].astype(np.float32).tofile(file_w_mld)
file_y.flush()
file_h.flush()
file_s.flush()
file_one_hot.flush()
file_w.flush()
file_s_mld.flush()
file_w_mld.flush()
idx += 1
pool.close()
file_y.close()
file_h.close()
file_s.close()
file_one_hot.close()
file_w.close()
file_s_mld.close()
file_w_mld.close()
print()
print("数据集生成完毕")
def fetch(self):
file_y, file_h, file_s, file_one_hot, file_w, file_s_mld, file_w_mld = self.__open_all("rb")
if self.flag == 0:
total_batch = TRAIN_TOTAL_BATCH
elif self.flag == 1:
total_batch = VALID_TOTAL_BATCH
else:
total_batch = TEST_TOTAL_BATCH
for i in range(total_batch):
file_y.seek(i * TRANSMIT_TIMES_PER_BATCH * 2 * NUM_ANT * 1)
file_h.seek(i * TRANSMIT_TIMES_PER_BATCH *2 * NUM_ANT * 2 * NUM_ANT)
file_s.seek(i * TRANSMIT_TIMES_PER_BATCH * 2 * NUM_ANT * 1)
file_one_hot.seek(i * TRANSMIT_TIMES_PER_BATCH * QPSK_CANDIDATE_SIZE)
file_w.seek(i * TRANSMIT_TIMES_PER_BATCH * 2 * NUM_ANT * 1)
file_s_mld.seek(i * TRANSMIT_TIMES_PER_BATCH * 2 * NUM_ANT * 1)
file_w_mld.seek(i * TRANSMIT_TIMES_PER_BATCH * 2 * NUM_ANT * 1)
y = np.fromfile(
file_y,
dtype=np.float32,
count=TRANSMIT_TIMES_PER_BATCH * 2 * NUM_ANT * 1
).reshape([-1, 2 * NUM_ANT, 1])
h = np.fromfile(
file_h,
dtype=np.float32,
count=TRANSMIT_TIMES_PER_BATCH * 2 * NUM_ANT * 2 * NUM_ANT
).reshape([-1, 2 * NUM_ANT, 2 * NUM_ANT])
s = np.fromfile(
file_s,
dtype=np.float32,
count=TRANSMIT_TIMES_PER_BATCH * 2 * NUM_ANT * 1
).reshape([-1, 2 * NUM_ANT, 1])
one_hot = np.fromfile(
file_one_hot,
dtype=np.float32,
count=TRANSMIT_TIMES_PER_BATCH * QPSK_CANDIDATE_SIZE
).reshape([-1, QPSK_CANDIDATE_SIZE])
w = np.fromfile(
file_w,
dtype=np.float32,
count=TRANSMIT_TIMES_PER_BATCH * 2 * NUM_ANT * 1
).reshape([-1, 2 * NUM_ANT, 1])
hat_s = np.fromfile(
file_s_mld,
dtype=np.float32,
count=TRANSMIT_TIMES_PER_BATCH * 2 * NUM_ANT * 1
).reshape([-1, 2 * NUM_ANT, 1])
hat_w = np.fromfile(
file_w_mld,
dtype=np.float32,
count=TRANSMIT_TIMES_PER_BATCH * 2 * NUM_ANT * 1
).reshape([-1, 2 * NUM_ANT, 1])
yield y, h, s, one_hot, w, hat_s, hat_w
file_y.close()
file_h.close()
file_s.close()
file_one_hot.close()
file_w.close()
file_s_mld.close()
file_w_mld.close()
def test_mld_batch(rho, sir_db):
err_mld = 0.0
total_bits = 0.0
idx = 0
while idx < 1000:
y, h, s, one_hot, w, s_mld, w_mld = produce_data_batch(rho, sir_db)
bits = get_bits(s)
bits_mld = get_bits(s_mld)
err_mld += len(np.argwhere(bits_mld != bits))
total_bits += bits.size
ber_mld = err_mld / total_bits
print("MLD, rho={}, sir={}dB, batch={:04}/1000, BER={:e}({:,.0f}/{:,.0f})".format(rho, sir_db, idx + 1, ber_mld, err_mld, total_bits), end="\r")
idx += 1
print("")
def check_data_set_sir(rho:float, sir_db:float):
hs_batch = None
w_batch = None
for i in range(100):
y, h, s, one_hot, w, s_mld, w_mld = produce_data_batch(rho, sir_db)
print("正在生成数据集 {}/100".format(i + 1), end="\r")
hs_batch = concatenate(hs_batch, h @ s)
w_batch = concatenate(w_batch, w)
test_sir = np.sum(hs_batch ** 2) / np.sum(w_batch ** 2)
test_sir_db = 10 * np.log10(test_sir)
print("")
print("数据集rho={}, 目标SIR={}dB, 检验SIR={:.2f}dB".format(rho, sir_db, test_sir_db))
if __name__ == "__main__":
test_mld_batch(rho=0.5, sir_db=10)