-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathrecord.py
More file actions
541 lines (377 loc) · 11.1 KB
/
record.py
File metadata and controls
541 lines (377 loc) · 11.1 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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import subprocess
from multiprocessing import Process, Queue
import os
import tempfile
import numpy as np
from sensing.siggen import *
import sys
import time
import threading
TEMPDIR="/tmp"
class MeasurementProcess(Process):
def __init__(self, genc, inp, extra=250000):
Process.__init__(self)
self.inp = inp
self.out = Queue(maxsize=2)
self.genc = genc
self.extra = extra
self.setup()
def setup(self):
pass
def run(self):
while True:
kwargs = self.inp.get()
if kwargs is None:
return
kwargs['path'] = self.measure(**kwargs)
self.out.put(kwargs)
class USRPMeasurementProcess(MeasurementProcess):
SLUG = "usrp"
def measure(self, Ns, Np, fc, fs, Pgen, fcgen):
if fcgen is None:
fcgen = fc + fs/4.
N = Ns*Np
self.genc.set(fcgen, Pgen)
handle, path = tempfile.mkstemp(dir=TEMPDIR)
os.close(handle)
args = ["uhd_rx_cfile", "-v",
"--freq=%d" % (fc,),
"--nsamples=%d" % (N + self.extra,),
"--samp-rate=%f" % (fs,),
"-ATX/RX",
path]
subprocess.check_call(args)
self.genc.off()
return path
import vesna.spectrumsensor
sneismtv_do_warmup = True
class AsyncSpectrumSensor:
WARMUP_MIN = 5
def __init__(self, device):
self.sensor = vesna.spectrumsensor.SpectrumSensor("/dev/ttyUSB0")
self.want_stop = False
self.worker = None
self.sample_config = None
self.work_cv = threading.Condition()
self.user_cb = None
def get_config_list(self):
return self.sensor.get_config_list()
def start(self, sample_config):
if self.worker is None:
self.sample_config = sample_config
self.worker = threading.Thread( target=self.sensor.sample_run,
args=(sample_config, self.cb))
sys.stdout.write("begin warmup\n")
self.worker.start()
time.sleep(self.WARMUP_MIN*60)
sys.stdout.write("end warmup\n")
else:
assert self.sample_config.nsamples == sample_config.nsamples
assert self.sample_config.config == sample_config.config
def cb(self, sample_config, data):
self.work_cv.acquire()
if self.user_cb is not None:
r = self.user_cb(sample_config, data)
if not r:
self.user_cb = None
self.work_cv.notify()
else:
self.work_cv.release()
return True
self.work_cv.release()
return not self.want_stop
def record(self, cb):
self.work_cv.acquire()
self.user_cb = cb
self.work_cv.wait()
self.work_cv.release()
def stop(self):
if self.worker is not None:
self.want_stop = True
self.worker.join()
self.worker = None
self.sample_config = None
class SNEESHTERCovarianceMeasurementProcess(MeasurementProcess):
SLUG = "eshtercov"
def setup(self):
self.sensor = AsyncSpectrumSensor("/dev/ttyUSB0")
self.config_list = self.sensor.get_config_list()
def measure(self, Ns, Np, fc, fs, Pgen, fcgen):
if fcgen is None:
fcgen = fc
if fs == 1e6:
device_config = self.config_list.get_config(0, 3)
elif fs == 2e6:
device_config = self.config_list.get_config(0, 2)
else:
assert False
Np2 = Np + int(self.extra/Ns) + 1
sample_config = device_config.get_sample_config(fc, Ns)
assert fs == sample_config.config.bw*2.
sys.stdout.write("recording %d*%d samples at %f Hz\n" % (Ns, Np2, fc))
sys.stdout.write("device config: %s\n" % (sample_config.config.name,))
self.sensor.start(sample_config)
x = np.empty(shape=Ns*Np2)
i = [0]
def cb(sample_config, data):
assert len(data.data) == Ns
x[i[0]*Ns:(i[0]+1)*Ns] = data.data
sys.stdout.write('.')
sys.stdout.flush()
i[0] += 1
if i[0] >= Np2:
return False
else:
return True
self.genc.set(fcgen, Pgen)
self.sensor.record(cb)
self.genc.off()
sys.stdout.write('\n')
# ok, this expects [self.extra][Ns*Np]
N = Ns*Np + self.extra
xa = np.empty(shape=N, dtype=np.dtype(np.complex64))
xa[:self.extra] = x[:self.extra]
xa[self.extra:] = x[-Ns*Np:]
handle, path = tempfile.mkstemp(dir=TEMPDIR)
os.close(handle)
xa.tofile(path)
return path
def run(self):
MeasurementProcess.run(self)
self.sensor.stop()
class SNEISMTVMeasurementProcess(MeasurementProcess):
SLUG = "sneismtv"
WARMUP_MIN = 30
def setup(self):
self.sensor = vesna.spectrumsensor.SpectrumSensor("/dev/ttyUSB0")
config_list = self.sensor.get_config_list()
self.config = config_list.get_config(0, 0)
global sneismtv_do_warmup
if sneismtv_do_warmup:
self.warmup()
sneismtv_do_warmup = False
else:
print("skipping warmup")
def warmup(self):
sample_config = self.config.get_sample_config(850e6, 4000)
start_time = time.time()
stop_time = start_time + self.WARMUP_MIN*60.
i = [0]
def cb(sample_config, data):
if (i[0] % 100) == 0:
print(" %.0f min of warmup left. Sample mean: %.0f" % (
(stop_time - time.time())/60.,
np.mean(data.data)))
i[0] += 1
return time.time() < stop_time
sys.stdout.write("begin warmup\n")
self.sensor.sample_run(sample_config, cb)
sys.stdout.write("end warmup\n")
def measure(self, Ns, Np, fc, fs, Pgen, fcgen=None):
if fcgen is None:
fcgen = fc
N = Ns*Np
N += self.extra
sample_config = self.config.get_sample_config(fc, Ns)
sys.stdout.write("recording %d samples at %f Hz\n" % (N, fc))
sys.stdout.write("device config: %s\n" % (sample_config.config.name,))
x = []
def cb(sample_config, tdata):
x.extend(tdata.data)
sys.stdout.write('.')
sys.stdout.flush()
if len(x) >= N:
return False
else:
return True
self.genc.set(fcgen, Pgen)
self.sensor.sample_run(sample_config, cb)
self.genc.off()
sys.stdout.write('\n')
if len(x) > N:
sys.stdout.write("truncating %d samples\n" % (len(x) - N,))
x = x[:N]
handle, path = tempfile.mkstemp(dir=TEMPDIR)
os.close(handle)
xa = np.array(x, dtype=np.dtype(np.complex64))
xa.tofile(path)
return path
class SimulatedMeasurementProcess(MeasurementProcess):
SLUG = "sim"
def measure(self, Ns, Np, fc, fs, Pgen):
N = Ns*Np
x = self.genc.get(N, fc, fs, Pgen)
handle, path = tempfile.mkstemp(dir=TEMPDIR)
os.close(handle)
xa = np.array(x, dtype=np.dtype(np.complex64))
xa.tofile(path)
return path
def do_campaign(genc, fc, fs, Ns, Pfcgenl, out_path, measurement_cls):
Np = 1000
extra = Ns*5
try:
os.mkdir(out_path)
except OSError:
pass
inp = Queue()
mp = measurement_cls(genc, inp, extra=extra)
mp.start()
for Pgen, fcgen in Pfcgenl:
inp.put({'Ns': Ns,
'Np': Np,
'fc': fc,
'fs': fs,
'Pgen': Pgen/10. if Pgen is not None else None,
'fcgen': fcgen})
for Pgen, fcgen in Pfcgenl:
kwargs = mp.out.get()
if kwargs['Pgen'] is None:
suf = 'off.npy'
else:
m = '%.1f' % (kwargs['Pgen'],)
m = m.replace('-','m')
m = m.replace('.','_')
suf = '%sdbm.npy' % (m,)
if kwargs['fcgen'] is None:
suf2 = ''
else:
suf2 = 'fcgen%dkhz_' % (kwargs['fcgen']/1e3,)
path = '%s/%s_%s_fs%dmhz_Ns%dks_' % (
out_path,
mp.SLUG,
genc.SLUG, fs/1e6, Ns/1000)
path += suf2 + suf
x = np.fromfile(kwargs['path'],
dtype=np.dtype(np.complex64))
# skip leading samples - they are typically not useful
# because they happen while ADC is settling in the receiver
# and other transition effects.
x = x[mp.extra:]
np.save(path, x.real)
os.unlink(kwargs['path'])
mp.inp.put(None)
mp.join()
def do_sampling_campaign_generator(genc, Pfcgenl, fc, fsNs, measurement_cls):
out_path = "samples-usrp"
for fs, Ns in fsNs:
do_campaign(genc, fc=fc, fs=fs, Ns=Ns, Pfcgenl=Pfcgenl, out_path=out_path,
measurement_cls=measurement_cls)
def do_usrp_sampling_campaign_generator(genc, Pfcgenl, measurement_cls):
fsNs = [ (1e6, 25000),
(2e6, 25000),
(10e6, 100000),
]
fc = 864e6
do_sampling_campaign_generator(genc, Pfcgenl, fc, fsNs, measurement_cls)
def do_eshter_sampling_campaign_generator(genc, Pfcgenl, measurement_cls):
fsNs = [ (1e6, 20000),
(2e6, 20000),
]
fc = 850e6
do_sampling_campaign_generator(genc, Pfcgenl, fc, fsNs, measurement_cls)
def do_eshtercov_campaign_generator(genc, Pfcgenl, measurement_cls):
out_path = "samples-eshtercov"
do_campaign(
genc,
fc=700e6,
fs=2e6,
Ns=20,
Pfcgenl=Pfcgenl,
out_path="samples-eshtercov",
measurement_cls=measurement_cls)
def ex_usrp_campaign_dc():
fsNs = [ (1e6, 25000),
(2e6, 25000),
(10e6, 100000),
]
fc = 864e6
Pfcgenl = [ (-600, None) ]
for dc in xrange(10, 101, 2):
dcf = dc * 1e-2
genc = CW(dc=dcf)
do_sampling_campaign_generator(genc, Pfcgenl, fc, fsNs, USRPMeasurementProcess)
def do_sneismtv_campaign_generator(genc, Pfcgenl):
fc = 850e6
measurement_cls = SNEISMTVMeasurementProcess
out_path = "samples-sneismtv"
Ns = 3676
do_campaign(genc, fc=fc, fs=0, Ns=Ns, Pfcgenl=Pfcgenl, out_path=out_path,
measurement_cls=measurement_cls)
def ex_sneismtv_campaign_dc():
Pfcgenl = [ (-600, None) ]
for dc in xrange(10, 100+1, 2):
dcf = dc * 1e-2
genc = CW(dc=dcf)
do_sneismtv_campaign_generator(genc, Pfcgenl)
def ex_sneismtv_campaign_mic_bpsk():
gencl = [
IEEE802514BPSK,
#IEEEMicSilent,
IEEEMicSoftSpeaker,
#IEEEMicLoudSpeaker,
]
Pgenl = [None] + range(-1000, -700, 10)
Pfcgenl = [ (Pgen, None) for Pgen in Pgenl ]
for genc in gencl:
genci = genc()
do_sneismtv_campaign_generator(genci, Pfcgenl)
def ex_usrp_campaign_noise():
genc = Noise()
Pgenl = [None] + range(-700, -100, 20)
Pfcgenl = [ (Pgen, None) for Pgen in Pgenl ]
do_usrp_sampling_campaign_generator(genc, Pfcgenl, USRPMeasurementProcess)
def ex_usrp_campaign_mic_bpsk():
gencl = [
IEEE802514BPSK,
#IEEEMicSilent,
IEEEMicSoftSpeaker,
#IEEEMicLoudSpeaker,
]
Pgenl = [None] + range(-1000, -700, 10)
Pfcgenl = [ (Pgen, None) for Pgen in Pgenl ]
for genc in gencl:
genci = genc()
do_usrp_sampling_campaign_generator(genci, Pfcgenl, USRPMeasurementProcess)
def ex_sim_campaign_mic():
genc = SimulatedIEEEMicSoftSpeaker()
Pgenl = [None] + range(-1000, -700, 10)
Pfcgenl = [ (Pgen, None) for Pgen in Pgenl ]
do_usrp_sampling_campaign_generator(genc, Pfcgenl, SimulatedMeasurementProcess)
def ex_eshter_campaign_noise():
genc = Noise()
Pgenl = [None] + range(-700, -100, 20)
Pfcgenl = [ (Pgen, None) for Pgen in Pgenl ]
do_eshter_sampling_campaign_generator(genc, Pfcgenl, SNEESHTERMeasurementProcess)
def ex_eshter_campaign_mic():
genc = IEEEMicSoftSpeaker()
Pgenl = [None] + range(-1000, -700, 10)
Pfcgenl = [ (Pgen, None) for Pgen in Pgenl ]
do_eshter_sampling_campaign_generator(genc, Pfcgenl, SNEESHTERMeasurementProcess)
def ex_eshtercov_campaign_unb():
genc = UNB()
Pgenl = [None] + range(-1000, -740, 10)
Pfcgenl = [ (Pgen, None) for Pgen in Pgenl ]
do_eshtercov_campaign_generator(genc, Pfcgenl, SNEESHTERCovarianceMeasurementProcess)
def ex_eshtercov_campaign_unb_freq_sweep():
genc = UNB()
Pfcgenl = [ (None, 700e6) ]
Pfcgenl += [ (-900, 700e6 + foff) for foff in np.arange(-1.00e6, .61e6, .05e6) ]
do_eshtercov_campaign_generator(genc, Pfcgenl, SNEESHTERCovarianceMeasurementProcess)
def main():
if len(sys.argv) == 2:
cmd = sys.argv[1]
globals()[cmd]()
else:
print "USAGE: %s <func>" % (sys.argv[0],)
print
print "available functions:"
funcs = []
for name, val in globals().iteritems():
if not callable(val):
continue
if name.startswith("ex_"):
funcs.append(name)
funcs.sort()
for name in funcs:
print " ", name
main()