This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
executable file
·371 lines (303 loc) · 14.1 KB
/
generator.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/python3
## python imports
import argparse
import json
import numpy as np
import os
from datetime import datetime
import ctypes
import sys
## internal imports
from utils import *
buf = 1024
halfbuf = 512
## load c modules
clinear = ctypes.CDLL(os.path.abspath("./cmodules/linear_modulate"))
cam = ctypes.CDLL(os.path.abspath("./cmodules/am_modulate"))
cfm = ctypes.CDLL(os.path.abspath("./cmodules/fm_modulate"))
cfsk = ctypes.CDLL(os.path.abspath("./cmodules/fsk_modulate"))
ctx = ctypes.CDLL(os.path.abspath("./cmodules/rrc_tx"))
cchan = ctypes.CDLL(os.path.abspath("./cmodules/channel"))
def generate_linear(idx_start, mod, config):
verbose = ctypes.c_int(config["verbose"])
modtype = ctypes.c_int(mod[0])
n_samps = ctypes.c_int(config["n_samps"]+buf)
sig_params = [(_sps, _beta, _delay, _dt) for _sps in config["symbol_rate"] for _beta in config["rrc_filter"]["beta"] for _delay in config["rrc_filter"]["delay"] for _dt in config["rrc_filter"]["dt"]]
idx = np.random.choice(len(sig_params), config["n_captures"])
sig_params = [sig_params[_idx] for _idx in idx]
idx = np.random.choice(len(config["channel_params"]), config["n_captures"])
channel_params = [config["channel_params"][_idx] for _idx in idx]
for i in range(0, config["n_captures"]):
seed = ctypes.c_int(np.random.randint(1e9))
snr = ctypes.c_float(channel_params[i][0])
fo = ctypes.c_float(2.*channel_params[i][1]*np.pi)
po = ctypes.c_float(channel_params[i][2])
order = ctypes.c_int(mod[1])
sps = ctypes.c_int(sig_params[i][0])
n_sym = n_sym = ctypes.c_int(int(np.ceil(n_samps.value/sps.value)))
beta = ctypes.c_float(sig_params[i][1])
delay = ctypes.c_uint(int(sig_params[i][2]))
dt = ctypes.c_float(sig_params[i][3])
## create return arrays
s = (ctypes.c_uint * n_sym.value)(*np.zeros(n_sym.value, dtype=int))
smI = (ctypes.c_float * n_sym.value)(*np.zeros(n_sym.value))
smQ = (ctypes.c_float * n_sym.value)(*np.zeros(n_sym.value))
xI = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
xQ = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
yI = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
yQ = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
clinear.linear_modulate(modtype, order, n_sym, s, smI, smQ, verbose, seed)
ctx.rrc_tx(n_sym, sps, delay, beta, dt, smI, smQ, xI, xQ, verbose)
cchan.channel(snr, n_sym, sps, fo, po, xI, xQ, yI, yQ, verbose, seed)
metadata = {"modname":mod[-1],
"modclass":modtype.value,
"order":order.value,
"n_samps":n_samps.value-buf,
"channel_type":config["channel_type"],
"snr":snr.value,
"filter_type":"rrc",
"sps":sps.value,
"delay":delay.value,
"beta":beta.value,
"dt":dt.value,
"fo":fo.value,
"po":po.value,
"savepath":config["savepath"],
"savename":config["savename"]}
## convert to numpy arrays
I = np.array([_i for _i in yI])
I = I[halfbuf:-halfbuf]
Q = np.array([_q for _q in yQ])
Q = Q[halfbuf:-halfbuf]
## save record in sigmf format
save_sigmf(I, Q, metadata, idx_start+i)
return idx_start+i+1
def generate_am(idx_start, mod, config):
verbose = ctypes.c_int(config["verbose"])
modtype = ctypes.c_int(mod[0])
n_samps = ctypes.c_int(config["n_samps"]+buf)
sig_params = config["am_defaults"]["modulation_index"]
idx = np.random.choice(len(sig_params), config["n_captures"])
sig_params = [sig_params[_idx] for _idx in idx]
idx = np.random.choice(len(config["channel_params"]), config["n_captures"])
channel_params = [config["channel_params"][_idx] for _idx in idx]
for i in range(0, config["n_captures"]):
seed = ctypes.c_int(np.random.randint(1e9))
snr = ctypes.c_float(channel_params[i][0])
fo = ctypes.c_float(2.*channel_params[i][1]*np.pi)
po = ctypes.c_float(channel_params[i][2])
modtype = ctypes.c_int(mod[1])
mod_idx = ctypes.c_float(sig_params[i])
sps = ctypes.c_int(1)
## create return arrays
x = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
xI = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
xQ = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
yI = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
yQ = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
## calls to c code
cam.am_modulate(modtype, mod_idx, n_samps, x, xI, xQ, verbose, seed)
cchan.channel(snr, n_samps, sps, fo, po, xI, xQ, yI, yQ, verbose, seed)
metadata = {"modname":mod[-1],
"modclass":mod[0],
"modvariant":mod[1],
"mod_idx":mod_idx.value,
"n_samps":n_samps.value-buf,
"channel_type":config["channel_type"],
"snr":snr.value,
"fo":fo.value,
"po":po.value,
"savepath":config["savepath"],
"savename":config["savename"]}
## convert to numpy arrays
I = np.array([_i for _i in yI])
I = I[halfbuf:-halfbuf]
Q = np.array([_q for _q in yQ])
Q = Q[halfbuf:-halfbuf]
## save record in sigmf format
save_sigmf(I, Q, metadata, idx_start+i)
return idx_start+i+1
def generate_fm(idx_start, mod, config):
verbose = ctypes.c_int(config["verbose"])
modtype = ctypes.c_int(mod[0])
n_samps = ctypes.c_int(config["n_samps"]+buf)
if mod[1] == 0:
## narrowband
sig_params = config["fmnb_defaults"]["modulation_factor"]
elif mod[1] == 1:
## wideband
sig_params = config["fmwb_defaults"]["modulation_factor"]
idx = np.random.choice(len(sig_params), config["n_captures"])
sig_params = [sig_params[_idx] for _idx in idx]
idx = np.random.choice(len(config["channel_params"]), config["n_captures"])
channel_params = [config["channel_params"][_idx] for _idx in idx]
for i in range(0, config["n_captures"]):
seed = ctypes.c_int(np.random.randint(1e9))
mod_factor = ctypes.c_float(sig_params[i])
snr = ctypes.c_float(channel_params[i][0])
fo = ctypes.c_float(2.*channel_params[i][1]*np.pi)
po = ctypes.c_float(channel_params[i][2])
sps = ctypes.c_int(1)
## create return arrays
x = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
xI = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
xQ = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
yI = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
yQ = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
## calls to c code
cfm.fm_modulate(mod_factor, n_samps, x, xI, xQ, verbose, seed)
cchan.channel(snr, n_samps, sps, fo, po, xI, xQ, yI, yQ, verbose, seed)
metadata = {"modname":mod[-1],
"modclass":mod[0],
"modvariant":mod[1],
"mod_factor":mod_factor.value,
"n_samps":n_samps.value-buf,
"channel_type":config["channel_type"],
"snr":snr.value,
"fo":fo.value,
"po":po.value,
"savepath":config["savepath"],
"savename":config["savename"]}
## convert to numpy arrays
I = np.array([_i for _i in yI])
I = I[halfbuf:-halfbuf]
Q = np.array([_q for _q in yQ])
Q = Q[halfbuf:-halfbuf]
## save record in sigmf format
save_sigmf(I, Q, metadata, idx_start+i)
return idx_start+i+1
def generate_fsk(idx_start, mod, config):
verbose = ctypes.c_int(config["verbose"])
modtype = ctypes.c_int(mod[0])
n_samps = ctypes.c_int(config["n_samps"]+buf)
sig_params = [(_sps, _beta, _delay, _dt) for _sps in config["symbol_rate"] for _beta in config["gaussian_filter"]["beta"] for _delay in config["gaussian_filter"]["delay"] for _dt in config["gaussian_filter"]["dt"]]
idx = np.random.choice(len(sig_params), config["n_captures"])
sig_params = [sig_params[_idx] for _idx in idx]
idx = np.random.choice(len(config["channel_params"]), config["n_captures"])
channel_params = [config["channel_params"][_idx] for _idx in idx]
for i in range(0, int(config["n_captures"])):
seed = ctypes.c_int(np.random.randint(1e9))
snr = ctypes.c_float(channel_params[i][0])
fo = ctypes.c_float(2.*channel_params[i][1]*np.pi)
po = ctypes.c_float(0.0) ## assume po = 0.0
bps = ctypes.c_int(int(np.log2(mod[1])))
modidx = ctypes.c_float(mod[2])
sps = ctypes.c_int(sig_params[i][0])
n_sym = n_sym = ctypes.c_int(int(np.ceil(n_samps.value/sps.value)))
pulseshape = ctypes.c_int(mod[3])
beta = ctypes.c_float(sig_params[i][1])
delay = ctypes.c_uint(int(sig_params[i][2]))
dt = ctypes.c_float(sig_params[i][3])
## create return arrays
s = (ctypes.c_uint * n_sym.value)(*np.zeros(n_sym.value, dtype=int))
xI = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
xQ = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
yI = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
yQ = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
## calls to c code
cfsk.fsk_modulate(n_sym, bps, modidx, pulseshape, sps, delay, beta, s, xI, xQ, verbose, seed)
cchan.channel(snr, n_sym, sps, fo, po, xI, xQ, yI, yQ, verbose, seed)
if mod[2] == 0.5:
cs = 2.5e3
elif mod[2] == 1.0:
cs = 5e3
elif mod[2] == 15.0:
cs = 15e3
else:
cs = None
if pulseshape.value == 0:
ft = "square"
b = "none"
else:
ft = "gaussian"
b = beta.value
metadata = {"modname":mod[-1],
"modclass":mod[0],
"order":mod[1],
"mod_idx":modidx.value,
"carrier_spacing":cs,
"n_samps":n_samps.value-buf,
"channel_type":config["channel_type"],
"snr":snr.value,
"filter_type":ft,
"sps":sps.value,
"beta":b,
"delay":delay.value,
"dt":dt.value,
"fo":fo.value,
"po":po.value,
"savepath":config["savepath"],
"savename":config["savename"]}
## convert to numpy arrays
I = np.array([_i for _i in yI])
I = I[halfbuf:-halfbuf]
Q = np.array([_q for _q in yQ])
Q = Q[halfbuf:-halfbuf]
## save record in sigmf format
save_sigmf(I, Q, metadata, idx_start+i)
return idx_start+i+1
def generate_noise(idx_start, mod, config):
verbose = ctypes.c_int(config["verbose"])
modtype = ctypes.c_int(mod[0])
n_samps = ctypes.c_int(config["n_samps"]+buf)
idx = np.random.choice(len(config["channel_params"]), config["n_captures"])
channel_params = [config["channel_params"][_idx] for _idx in idx]
for i in range(0, config["n_captures"]):
seed = ctypes.c_int(np.random.randint(1e9))
snr = ctypes.c_float(channel_params[i][0])
fo = ctypes.c_float(2.*channel_params[i][1]*np.pi)
po = ctypes.c_float(0.0)
sps = ctypes.c_int(1)
xI = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
xQ = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
yI = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
yQ = (ctypes.c_float * n_samps.value)(*np.zeros(n_samps.value))
cchan.channel(snr, n_samps, sps, fo, po, xI, xQ, yI, yQ, verbose, seed)
metadata = {"modname":mod[-1],
"modclass":modtype.value,
"n_samps":n_samps.value-buf,
"channel_type":config["channel_type"],
"snr":snr.value,
"sps":sps.value,
"fo":fo.value,
"po":po.value,
"savepath":config["savepath"],
"savename":config["savename"]}
## convert to numpy arrays
I = np.array([_i for _i in yI])
I = I[halfbuf:-halfbuf]
Q = np.array([_q for _q in yQ])
Q = Q[halfbuf:-halfbuf]
## save record in sigmf format
save_sigmf(I, Q, metadata, idx_start+i)
return idx_start+i+1
def run_tx(config):
idx = 0
## loop through config
for _mod in config["modulation"]:
start_idx = idx
if mod_int2modem[_mod[0]] is None:
idx = generate_noise(start_idx, _mod, config)
elif mod_int2modem[_mod[0]] == "linear":
idx = generate_linear(start_idx, _mod, config)
elif mod_int2modem[_mod[0]] == "amplitude":
idx = generate_am(start_idx, _mod, config)
elif mod_int2modem[_mod[0]] == "frequency":
idx = generate_fm(start_idx, _mod, config)
elif mod_int2modem[_mod[0]] == "freq_shift":
idx = generate_fsk(start_idx, _mod, config)
else:
raise ValueError("Undefined modem.")
print(_mod[-1] + ": " + str(idx-start_idx))
if config["archive"]:
archive_sigmf(config["savepath"])
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("config_file", type=str, help="Path to configuration file to use for data generation.")
args = parser.parse_args()
with open(args.config_file) as f:
config = json.load(f)
with open("./configs/defaults.json") as f:
defaults = json.load(f)
config = map_config(config, defaults)
run_tx(config)