-
Notifications
You must be signed in to change notification settings - Fork 19
/
cali.py
199 lines (154 loc) · 6.78 KB
/
cali.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
import argparse
import os
from tqdm import tqdm
import time
import calibratesdr as cali
def main(input):
print("let's find your SDR's oscillator precision")
show_graph = input["graph"]
verbose = input["verbose"]
samplerate = input["rs"]
mode = input["m"]
if input["f"] is not None:
print("loading file...")
filename = input["f"]
if os.path.exists(filename):
data = cali.utils.load_data(filename, offset=44)
if mode == "dab":
print("starting mode: dab")
ppm = cali.dabplus.dab.get_ppm(data, samplerate=samplerate, show_graph=show_graph, verbose=verbose)
print("your sdr's precision is", ppm, "ppm")
elif mode == "dvbt":
print("starting mode: dvbt")
cali.dvbt.dvbt.main()
elif mode == "gsm":
print("starting mode: gsm")
cali.gsm.gsm.main()
else:
print("ending")
else:
print("sorry, file not found. try again.")
elif input["s"] is not None:
# scanning with your sdr
print("scanning...")
if mode == "dab":
print("starting mode: dab")
from rtlsdr import RtlSdr
filename = "tmp.dat"
device = input["rd"]
sdr = RtlSdr(device_index=device)
rs = input["rs"]
rg = input["rg"]
ns = rs * input["nsec"] #seconds
c = input["c"]
result = []
dabchannels = cali.dabplus.dab.channels()
if c == "all":
print("Scanning all channels")
for channel in tqdm(range(len(dabchannels["dab"])),
desc="Scanning…",
ascii=False,
ncols=75):
channel, block, cf, dab_snr, dab_block_detected, dab_ppm = \
cali.utils.scan_one_dab_channel(dabchannels, channel, sdr, rs, ns, rg, filename, samplerate,
show_graph, verbose)
result.append([channel, block, cf, dab_snr, dab_block_detected, dab_ppm])
else:
print("Scanning only channel #", c)
channel = int(c)
channel, block, cf, dab_snr, dab_block_detected, dab_ppm = \
cali.utils.scan_one_dab_channel(dabchannels, channel, sdr, rs, ns, rg, filename, samplerate,
show_graph, verbose)
result.append([channel, block, cf, dab_snr, dab_block_detected, dab_ppm])
# output
dab_snr_max = 0
for i in range(len(result)):
if i == 0 or dab_snr_max < result[i][3]:
dab_snr_max = result[i][3]
print("")
print("____Results_______________________________________________________________________________________")
print("# , block, freq [Hz], SNR [dB] , Prec. [ppm], offset [Hz], block [x][o][ ] & signal strength")
print("--------------------------------------------------------------------------------------------------")
for i in range(len(result)):
channel = result[i][0]
block = result[i][1]
cf = result[i][2]
dab_snr = result[i][3]
dab_block_detected = "[ ]"
if result[i][4] == 1:
dab_block_detected = "[o]"
elif result[i][4] == 2:
dab_block_detected = "[x]"
dab_ppm = result[i][5]
bar = cali.utils.signal_bar(dab_snr, dab_snr_max)
f_offset = 0.0
if dab_ppm != None:
f_offset = cf / 1000000.0 * dab_ppm
print("# {0:2d}, {1:5s}, {2:9.0f}, {3:+9.5f}, {4:+10.4f}, {5:+11.1f}, {6:4s} {7}".
format(channel, block, cf, dab_snr, dab_ppm, f_offset, dab_block_detected, bar))
else:
print("# {0:2d}, {1:5s}, {2:9.0f}, {3:+9.5f}, None, {5:+11.1f}, {6:4s} {7}".
format(channel, block, cf, dab_snr, dab_ppm, f_offset, dab_block_detected, bar))
sdr.close()
elif mode == "dvbt":
print("starting mode: dvbt")
cali.dvbt.dvbt.main()
elif mode == "gsm":
print("starting mode: gsm")
cali.gsm.gsm.main()
else:
print("ending")
if __name__ == "__main__":
my_parser = argparse.ArgumentParser()
my_parser.add_argument('-f',
action='store',
type=str,
help='select path to input file')
my_parser.add_argument('-m',
action='store',
choices=['dab', 'dvbt', 'gsm'],
help='select mode',
default='dab')
my_parser.add_argument('-s',
action='store',
choices=['rtlsdr'],
help='scan with rtlsdr',
default='rtlsdr')
my_parser.add_argument('-c',
action='store',
type=str,
help='scan by \"all\" channels, by channel number \"0,1,...n\" or by blockname',
default = 'all')
my_parser.add_argument('-rs',
action='store',
type=int,
help='file/scan samplerate',
default=2048000)
my_parser.add_argument('-rg',
action='store',
type=int,
help='scan with gain',
default=20)
my_parser.add_argument('-rd',
action='store',
type=int,
help='scan with device',
default=0)
my_parser.add_argument('-nsec',
action='store',
type=float,
help='scan for n-seconds',
default=10)
my_parser.add_argument('-gr',
'--graph',
action='store_true',
help='activate graphs',
default=False)
my_parser.add_argument('-v',
'--verbose',
action='store_true',
help='an optional argument')
# Execute parse_args()
args = my_parser.parse_args()
print(vars(args))
main(vars(args))