forked from LetsGoRosco/grc-files
/
nbfm.py
385 lines (352 loc) · 14.5 KB
/
nbfm.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env python
##################################################
# Gnuradio Python Flow Graph
# Title: Nbfm
# Generated: Sat Apr 25 14:56:54 2015
##################################################
from gnuradio import analog
from gnuradio import audio
from gnuradio import blocks
from gnuradio import eng_notation
from gnuradio import filter
from gnuradio import gr
from gnuradio import wxgui
from gnuradio.eng_option import eng_option
from gnuradio.fft import window
from gnuradio.filter import firdes
from gnuradio.wxgui import fftsink2
from gnuradio.wxgui import forms
from gnuradio.wxgui import scopesink2
from gnuradio.wxgui import waterfallsink2
from grc_gnuradio import wxgui as grc_wxgui
from optparse import OptionParser
import math
import osmosdr
import time
import wx
class nbfm(grc_wxgui.top_block_gui):
def __init__(self):
grc_wxgui.top_block_gui.__init__(self, title="Nbfm")
##################################################
# Variables
##################################################
self.decim = decim = 2
self.adc_rate = adc_rate = 2000000
self.xlate_offset_fine = xlate_offset_fine = 4000
self.xlate_offset = xlate_offset = 0
self.xlate_decim = xlate_decim = 4
self.xlate_bandwidth = xlate_bandwidth = 12500
self.volume = volume = 1
self.squelch = squelch = 50
self.samp_rate = samp_rate = adc_rate/decim
self.main_freq = main_freq = 167.99e6
self.audio_rate = audio_rate = 48000
self.audio_interp = audio_interp = 4
##################################################
# Blocks
##################################################
self.main_notebook = self.main_notebook = wx.Notebook(self.GetWin(), style=wx.NB_TOP)
self.main_notebook.AddPage(grc_wxgui.Panel(self.main_notebook), "Baseband")
self.main_notebook.AddPage(grc_wxgui.Panel(self.main_notebook), "Scope")
self.main_notebook.AddPage(grc_wxgui.Panel(self.main_notebook), "Waterfall")
self.main_notebook.AddPage(grc_wxgui.Panel(self.main_notebook), "Quad demod")
self.Add(self.main_notebook)
_xlate_offset_fine_sizer = wx.BoxSizer(wx.VERTICAL)
self._xlate_offset_fine_text_box = forms.text_box(
parent=self.main_notebook.GetPage(0).GetWin(),
sizer=_xlate_offset_fine_sizer,
value=self.xlate_offset_fine,
callback=self.set_xlate_offset_fine,
label="Fine Offset",
converter=forms.float_converter(),
proportion=0,
)
self._xlate_offset_fine_slider = forms.slider(
parent=self.main_notebook.GetPage(0).GetWin(),
sizer=_xlate_offset_fine_sizer,
value=self.xlate_offset_fine,
callback=self.set_xlate_offset_fine,
minimum=-10000,
maximum=10000,
num_steps=100,
style=wx.SL_HORIZONTAL,
cast=float,
proportion=1,
)
self.main_notebook.GetPage(0).Add(_xlate_offset_fine_sizer)
self._xlate_offset_text_box = forms.text_box(
parent=self.main_notebook.GetPage(0).GetWin(),
value=self.xlate_offset,
callback=self.set_xlate_offset,
label="Xlate Offset",
converter=forms.float_converter(),
)
self.main_notebook.GetPage(0).Add(self._xlate_offset_text_box)
_xlate_bandwidth_sizer = wx.BoxSizer(wx.VERTICAL)
self._xlate_bandwidth_text_box = forms.text_box(
parent=self.main_notebook.GetPage(0).GetWin(),
sizer=_xlate_bandwidth_sizer,
value=self.xlate_bandwidth,
callback=self.set_xlate_bandwidth,
label="Xlate Bandwidth",
converter=forms.float_converter(),
proportion=0,
)
self._xlate_bandwidth_slider = forms.slider(
parent=self.main_notebook.GetPage(0).GetWin(),
sizer=_xlate_bandwidth_sizer,
value=self.xlate_bandwidth,
callback=self.set_xlate_bandwidth,
minimum=2500,
maximum=250000,
num_steps=100,
style=wx.SL_HORIZONTAL,
cast=float,
proportion=1,
)
self.main_notebook.GetPage(0).Add(_xlate_bandwidth_sizer)
_volume_sizer = wx.BoxSizer(wx.VERTICAL)
self._volume_text_box = forms.text_box(
parent=self.GetWin(),
sizer=_volume_sizer,
value=self.volume,
callback=self.set_volume,
label='volume',
converter=forms.float_converter(),
proportion=0,
)
self._volume_slider = forms.slider(
parent=self.GetWin(),
sizer=_volume_sizer,
value=self.volume,
callback=self.set_volume,
minimum=0,
maximum=10,
num_steps=100,
style=wx.SL_HORIZONTAL,
cast=float,
proportion=1,
)
self.Add(_volume_sizer)
_squelch_sizer = wx.BoxSizer(wx.VERTICAL)
self._squelch_text_box = forms.text_box(
parent=self.GetWin(),
sizer=_squelch_sizer,
value=self.squelch,
callback=self.set_squelch,
label='squelch',
converter=forms.float_converter(),
proportion=0,
)
self._squelch_slider = forms.slider(
parent=self.GetWin(),
sizer=_squelch_sizer,
value=self.squelch,
callback=self.set_squelch,
minimum=0,
maximum=100,
num_steps=100,
style=wx.SL_HORIZONTAL,
cast=float,
proportion=1,
)
self.Add(_squelch_sizer)
self._main_freq_text_box = forms.text_box(
parent=self.main_notebook.GetPage(0).GetWin(),
value=self.main_freq,
callback=self.set_main_freq,
label="Main Freq",
converter=forms.float_converter(),
)
self.main_notebook.GetPage(0).Add(self._main_freq_text_box)
self.wxgui_waterfallsink2_0 = waterfallsink2.waterfall_sink_c(
self.main_notebook.GetPage(2).GetWin(),
baseband_freq=0,
dynamic_range=100,
ref_level=50,
ref_scale=2.0,
sample_rate=samp_rate/xlate_decim,
fft_size=512,
fft_rate=30,
average=False,
avg_alpha=None,
title="Waterfall Plot",
)
self.main_notebook.GetPage(2).Add(self.wxgui_waterfallsink2_0.win)
self.wxgui_scopesink2_1 = scopesink2.scope_sink_f(
self.main_notebook.GetPage(3).GetWin(),
title="Scope Plot",
sample_rate=samp_rate/xlate_decim,
v_scale=0,
v_offset=0,
t_scale=0,
ac_couple=False,
xy_mode=False,
num_inputs=1,
trig_mode=wxgui.TRIG_MODE_AUTO,
y_axis_label="Counts",
)
self.main_notebook.GetPage(3).Add(self.wxgui_scopesink2_1.win)
self.wxgui_scopesink2_0 = scopesink2.scope_sink_c(
self.main_notebook.GetPage(1).GetWin(),
title="Scope Plot",
sample_rate=samp_rate/xlate_decim,
v_scale=10,
v_offset=0,
t_scale=10000,
ac_couple=False,
xy_mode=False,
num_inputs=1,
trig_mode=wxgui.TRIG_MODE_AUTO,
y_axis_label="Counts",
)
self.main_notebook.GetPage(1).Add(self.wxgui_scopesink2_0.win)
self.wxgui_fftsink2_0 = fftsink2.fft_sink_c(
self.main_notebook.GetPage(0).GetWin(),
baseband_freq=0,
y_per_div=10,
y_divs=10,
ref_level=50,
ref_scale=2.0,
sample_rate=samp_rate/xlate_decim,
fft_size=1024,
fft_rate=30,
average=False,
avg_alpha=None,
title="FFT Plot",
peak_hold=False,
)
self.main_notebook.GetPage(0).Add(self.wxgui_fftsink2_0.win)
self.rtlsdr_source_0 = osmosdr.source( args="numchan=" + str(1) + " " + "" )
self.rtlsdr_source_0.set_sample_rate(samp_rate)
self.rtlsdr_source_0.set_center_freq(main_freq, 0)
self.rtlsdr_source_0.set_freq_corr(0, 0)
self.rtlsdr_source_0.set_dc_offset_mode(0, 0)
self.rtlsdr_source_0.set_iq_balance_mode(2, 0)
self.rtlsdr_source_0.set_gain_mode(True, 0)
self.rtlsdr_source_0.set_gain(50, 0)
self.rtlsdr_source_0.set_if_gain(20, 0)
self.rtlsdr_source_0.set_bb_gain(20, 0)
self.rtlsdr_source_0.set_antenna("", 0)
self.rtlsdr_source_0.set_bandwidth(0, 0)
self.rational_resampler_xxx_0 = filter.rational_resampler_ccc(
interpolation=audio_rate*audio_interp,
decimation=samp_rate/xlate_decim,
taps=None,
fractional_bw=None,
)
self.freq_xlating_fir_filter_xxx_0 = filter.freq_xlating_fir_filter_ccc(xlate_decim, (firdes.low_pass(1, samp_rate, xlate_bandwidth/2, 1000)), xlate_offset + xlate_offset_fine, samp_rate)
self.blocks_multiply_const_vxx_0 = blocks.multiply_const_vff((volume, ))
self.audio_sink_0 = audio.sink(48000, "", True)
self.analog_simple_squelch_cc_0 = analog.simple_squelch_cc(squelch*-1, 1)
self.analog_quadrature_demod_cf_0 = analog.quadrature_demod_cf(1)
self.analog_nbfm_rx_0 = analog.nbfm_rx(
audio_rate=audio_rate,
quad_rate=audio_rate*audio_interp,
tau=75e-6,
max_dev=5e3,
)
##################################################
# Connections
##################################################
self.connect((self.analog_nbfm_rx_0, 0), (self.blocks_multiply_const_vxx_0, 0))
self.connect((self.analog_quadrature_demod_cf_0, 0), (self.wxgui_scopesink2_1, 0))
self.connect((self.analog_simple_squelch_cc_0, 0), (self.analog_nbfm_rx_0, 0))
self.connect((self.blocks_multiply_const_vxx_0, 0), (self.audio_sink_0, 0))
self.connect((self.freq_xlating_fir_filter_xxx_0, 0), (self.analog_quadrature_demod_cf_0, 0))
self.connect((self.freq_xlating_fir_filter_xxx_0, 0), (self.rational_resampler_xxx_0, 0))
self.connect((self.freq_xlating_fir_filter_xxx_0, 0), (self.wxgui_fftsink2_0, 0))
self.connect((self.freq_xlating_fir_filter_xxx_0, 0), (self.wxgui_scopesink2_0, 0))
self.connect((self.freq_xlating_fir_filter_xxx_0, 0), (self.wxgui_waterfallsink2_0, 0))
self.connect((self.rational_resampler_xxx_0, 0), (self.analog_simple_squelch_cc_0, 0))
self.connect((self.rtlsdr_source_0, 0), (self.freq_xlating_fir_filter_xxx_0, 0))
def get_decim(self):
return self.decim
def set_decim(self, decim):
self.decim = decim
self.set_samp_rate(self.adc_rate/self.decim)
def get_adc_rate(self):
return self.adc_rate
def set_adc_rate(self, adc_rate):
self.adc_rate = adc_rate
self.set_samp_rate(self.adc_rate/self.decim)
def get_xlate_offset_fine(self):
return self.xlate_offset_fine
def set_xlate_offset_fine(self, xlate_offset_fine):
self.xlate_offset_fine = xlate_offset_fine
self.freq_xlating_fir_filter_xxx_0.set_center_freq(self.xlate_offset + self.xlate_offset_fine)
self._xlate_offset_fine_slider.set_value(self.xlate_offset_fine)
self._xlate_offset_fine_text_box.set_value(self.xlate_offset_fine)
def get_xlate_offset(self):
return self.xlate_offset
def set_xlate_offset(self, xlate_offset):
self.xlate_offset = xlate_offset
self.freq_xlating_fir_filter_xxx_0.set_center_freq(self.xlate_offset + self.xlate_offset_fine)
self._xlate_offset_text_box.set_value(self.xlate_offset)
def get_xlate_decim(self):
return self.xlate_decim
def set_xlate_decim(self, xlate_decim):
self.xlate_decim = xlate_decim
self.wxgui_fftsink2_0.set_sample_rate(self.samp_rate/self.xlate_decim)
self.wxgui_waterfallsink2_0.set_sample_rate(self.samp_rate/self.xlate_decim)
self.wxgui_scopesink2_1.set_sample_rate(self.samp_rate/self.xlate_decim)
self.wxgui_scopesink2_0.set_sample_rate(self.samp_rate/self.xlate_decim)
def get_xlate_bandwidth(self):
return self.xlate_bandwidth
def set_xlate_bandwidth(self, xlate_bandwidth):
self.xlate_bandwidth = xlate_bandwidth
self.freq_xlating_fir_filter_xxx_0.set_taps((firdes.low_pass(1, self.samp_rate, self.xlate_bandwidth/2, 1000)))
self._xlate_bandwidth_slider.set_value(self.xlate_bandwidth)
self._xlate_bandwidth_text_box.set_value(self.xlate_bandwidth)
def get_volume(self):
return self.volume
def set_volume(self, volume):
self.volume = volume
self.blocks_multiply_const_vxx_0.set_k((self.volume, ))
self._volume_slider.set_value(self.volume)
self._volume_text_box.set_value(self.volume)
def get_squelch(self):
return self.squelch
def set_squelch(self, squelch):
self.squelch = squelch
self._squelch_slider.set_value(self.squelch)
self._squelch_text_box.set_value(self.squelch)
self.analog_simple_squelch_cc_0.set_threshold(self.squelch*-1)
def get_samp_rate(self):
return self.samp_rate
def set_samp_rate(self, samp_rate):
self.samp_rate = samp_rate
self.wxgui_fftsink2_0.set_sample_rate(self.samp_rate/self.xlate_decim)
self.wxgui_waterfallsink2_0.set_sample_rate(self.samp_rate/self.xlate_decim)
self.wxgui_scopesink2_1.set_sample_rate(self.samp_rate/self.xlate_decim)
self.wxgui_scopesink2_0.set_sample_rate(self.samp_rate/self.xlate_decim)
self.freq_xlating_fir_filter_xxx_0.set_taps((firdes.low_pass(1, self.samp_rate, self.xlate_bandwidth/2, 1000)))
self.rtlsdr_source_0.set_sample_rate(self.samp_rate)
def get_main_freq(self):
return self.main_freq
def set_main_freq(self, main_freq):
self.main_freq = main_freq
self._main_freq_text_box.set_value(self.main_freq)
self.rtlsdr_source_0.set_center_freq(self.main_freq, 0)
def get_audio_rate(self):
return self.audio_rate
def set_audio_rate(self, audio_rate):
self.audio_rate = audio_rate
def get_audio_interp(self):
return self.audio_interp
def set_audio_interp(self, audio_interp):
self.audio_interp = audio_interp
if __name__ == '__main__':
import ctypes
import sys
if sys.platform.startswith('linux'):
try:
x11 = ctypes.cdll.LoadLibrary('libX11.so')
x11.XInitThreads()
except:
print "Warning: failed to XInitThreads()"
parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
(options, args) = parser.parse_args()
tb = nbfm()
tb.Start(True)
tb.Wait()