forked from luigifcruz/PyRadio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_wbfm.py
More file actions
84 lines (67 loc) · 2.13 KB
/
multi_wbfm.py
File metadata and controls
84 lines (67 loc) · 2.13 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
#!/usr/bin/env python3
from SoapySDR import SOAPY_SDR_RX, SOAPY_SDR_CF32
import SoapySDR
import signal
import queue
import numpy as np
from radio.analog import WBFM
from radio.tools import Tuner
import cusignal as sig
import sounddevice as sd
# Demodulator Settings
cuda = True
tau = 75e-6
sfs = int(240e3)
afs = int(48e3)
sdr_buff = 2400
radios = [
{ "freq": 97.5e6, "bw": sfs },
{ "freq": 96.9e6, "bw": sfs },
]
# Queue and Shared Memory Allocation
que = queue.Queue()
tuner = Tuner(radios, sfs, cuda=cuda)
demod = [ WBFM(tau, sfs, afs, cuda=cuda) for _ in radios ]
afile = [ open("FM_{}.if32".format(int(f["freq"])), "bw") for f in radios ]
print("# Tuner Settings:")
print(" Bandwidth: {}".format(tuner.bw))
print(" Mean Frequency: {}".format(tuner.mdf))
print(" Offsets: {}".format(tuner.foff))
print(" Stations: {}".format(len(radios)))
# SoapySDR Configuration
args = dict(driver="lime")
sdr = SoapySDR.Device(args)
sdr.setGainMode(SOAPY_SDR_RX, 0, True)
sdr.setSampleRate(SOAPY_SDR_RX, 0, tuner.bw)
sdr.setFrequency(SOAPY_SDR_RX, 0, tuner.mdf)
# Declare the memory buffer
if cuda:
import cusignal as sig
buff = sig.get_shared_mem(tuner.size, dtype=np.complex64)
else:
buff = np.zeros([tuner.size], dtype=np.complex64)
# Demodulation Function
def process(outdata, f, t, s):
tuner.load(que.get())
for i, _ in enumerate(radios):
L, R = demod[i].run(tuner.run(i))
LR = np.ravel(np.column_stack((L, R)))
LR = LR.astype(np.float32)
afile[i].write(LR.tobytes())
outdata[:] = np.dstack((L, R))
# Graceful Exit Handler
def signal_handler(signum, frame):
sdr.deactivateStream(rx)
sdr.closeStream(rx)
exit(-1)
signal.signal(signal.SIGINT, signal_handler)
# Start Collecting Data
plan = [(i*sdr_buff) for i in range(tuner.size//sdr_buff)]
rx = sdr.setupStream(SOAPY_SDR_RX, SOAPY_SDR_CF32)
sdr.activateStream(rx)
with sd.OutputStream(blocksize=afs, callback=process,
samplerate=afs, channels=2):
while True:
for i in plan:
sdr.readStream(rx, [buff[i:]], sdr_buff, timeoutUs=int(1e9))
que.put(buff.astype(np.complex64))