summaryrefslogtreecommitdiff
path: root/gr-pager/apps/usrp_rx_flex.py
blob: 47bf5a3df6e9f5d94c189289f2e4797f255f4a4d (plain)
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
#!/usr/bin/env python
##################################################
# Gnuradio Python Flow Graph
# Title: USRP FLEX Pager Receiver (Single Channel)
# Generated: Thu Oct 29 11:03:16 2009
##################################################

from gnuradio import blks2
from gnuradio import eng_notation
from gnuradio import gr
from gnuradio import pager
from gnuradio import window
from gnuradio.eng_option import eng_option
from gnuradio.gr import firdes
from gnuradio.wxgui import fftsink2
from gnuradio.wxgui import forms
from gnuradio.wxgui import scopesink2
from grc_gnuradio import usrp as grc_usrp
from grc_gnuradio import wxgui as grc_wxgui
from optparse import OptionParser
import ConfigParser
import os, math
import wx

class usrp_rx_flex(grc_wxgui.top_block_gui):

	def __init__(self):
		grc_wxgui.top_block_gui.__init__(self, title="USRP FLEX Pager Receiver (Single Channel)")

		##################################################
		# Variables
		##################################################
		self.config_filename = config_filename = os.environ["HOME"]+"/.gnuradio/config.conf"
		self.symbol_rate = symbol_rate = 3200
		self._saved_channel_config = ConfigParser.ConfigParser()
		self._saved_channel_config.read(config_filename)
		try: saved_channel = self._saved_channel_config.getint("gr-pager", "channel")
		except: saved_channel = 25
		self.saved_channel = saved_channel
		self._saved_band_freq_config = ConfigParser.ConfigParser()
		self._saved_band_freq_config.read(config_filename)
		try: saved_band_freq = self._saved_band_freq_config.getfloat("gr-pager", "band_center")
		except: saved_band_freq = 930.5125e6
		self.saved_band_freq = saved_band_freq
		self.deviation = deviation = 4800
		self.decim = decim = 20
		self.adc_rate = adc_rate = 64e6
		self.sample_rate = sample_rate = adc_rate/decim
		self.passband = passband = 2*(deviation+symbol_rate)
		self.channel_rate = channel_rate = 8*3200
		self.channel = channel = saved_channel
		self.band_freq = band_freq = saved_band_freq
		self._saved_rx_gain_config = ConfigParser.ConfigParser()
		self._saved_rx_gain_config.read(config_filename)
		try: saved_rx_gain = self._saved_rx_gain_config.getint("gr-pager", "rx_gain")
		except: saved_rx_gain = 40
		self.saved_rx_gain = saved_rx_gain
		self._saved_offset_config = ConfigParser.ConfigParser()
		self._saved_offset_config.read(config_filename)
		try: saved_offset = self._saved_offset_config.getfloat("gr-pager", "freq_offset")
		except: saved_offset = 0
		self.saved_offset = saved_offset
		self.freq = freq = band_freq+(channel-61)*25e3
		self.channel_taps = channel_taps = firdes.low_pass(10, sample_rate, passband/2.0, (channel_rate-passband)/2.0)
		self.rx_gain = rx_gain = saved_rx_gain
		self.offset = offset = saved_offset
		self.nchan_taps = nchan_taps = len(channel_taps)
		self.ma_ntaps = ma_ntaps = int(channel_rate/symbol_rate)
		self.freq_text = freq_text = freq
		self.demod_k = demod_k = 3*channel_rate/(2*math.pi*deviation)
		self.channel_decim = channel_decim = int(sample_rate/channel_rate)
		self.bb_interp = bb_interp = 5
		self.bb_decim = bb_decim = 8
		self.baseband_rate = baseband_rate = 16000

		##################################################
		# Notebooks
		##################################################
		self.displays = wx.Notebook(self.GetWin(), style=wx.NB_TOP)
		self.displays.AddPage(grc_wxgui.Panel(self.displays), "RX Spectrum")
		self.displays.AddPage(grc_wxgui.Panel(self.displays), "Baseband")
		self.GridAdd(self.displays, 1, 0, 1, 5)

		##################################################
		# Controls
		##################################################
		_channel_sizer = wx.BoxSizer(wx.VERTICAL)
		self._channel_text_box = forms.text_box(
			parent=self.GetWin(),
			sizer=_channel_sizer,
			value=self.channel,
			callback=self.set_channel,
			label="Channel",
			converter=forms.int_converter(),
			proportion=0,
		)
		self._channel_slider = forms.slider(
			parent=self.GetWin(),
			sizer=_channel_sizer,
			value=self.channel,
			callback=self.set_channel,
			minimum=1,
			maximum=120,
			num_steps=119,
			style=wx.SL_HORIZONTAL,
			cast=int,
			proportion=1,
		)
		self.GridAdd(_channel_sizer, 0, 1, 1, 1)
		self._band_freq_text_box = forms.text_box(
			parent=self.GetWin(),
			value=self.band_freq,
			callback=self.set_band_freq,
			label="Band Freq.",
			converter=forms.float_converter(),
		)
		self.GridAdd(self._band_freq_text_box, 0, 0, 1, 1)
		_rx_gain_sizer = wx.BoxSizer(wx.VERTICAL)
		self._rx_gain_text_box = forms.text_box(
			parent=self.GetWin(),
			sizer=_rx_gain_sizer,
			value=self.rx_gain,
			callback=self.set_rx_gain,
			label="Analog Gain",
			converter=forms.int_converter(),
			proportion=0,
		)
		self._rx_gain_slider = forms.slider(
			parent=self.GetWin(),
			sizer=_rx_gain_sizer,
			value=self.rx_gain,
			callback=self.set_rx_gain,
			minimum=0,
			maximum=100,
			num_steps=100,
			style=wx.SL_HORIZONTAL,
			cast=int,
			proportion=1,
		)
		self.GridAdd(_rx_gain_sizer, 0, 4, 1, 1)
		_offset_sizer = wx.BoxSizer(wx.VERTICAL)
		self._offset_text_box = forms.text_box(
			parent=self.GetWin(),
			sizer=_offset_sizer,
			value=self.offset,
			callback=self.set_offset,
			label="Freq. Offset",
			converter=forms.float_converter(),
			proportion=0,
		)
		self._offset_slider = forms.slider(
			parent=self.GetWin(),
			sizer=_offset_sizer,
			value=self.offset,
			callback=self.set_offset,
			minimum=-12.5e3,
			maximum=12.5e3,
			num_steps=100,
			style=wx.SL_HORIZONTAL,
			cast=float,
			proportion=1,
		)
		self.GridAdd(_offset_sizer, 0, 3, 1, 1)
		self._freq_text_static_text = forms.static_text(
			parent=self.GetWin(),
			value=self.freq_text,
			callback=self.set_freq_text,
			label="Ch. Freq",
			converter=forms.float_converter(),
		)
		self.GridAdd(self._freq_text_static_text, 0, 2, 1, 1)

		##################################################
		# Blocks
		##################################################
		self.fm_demod = gr.quadrature_demod_cf(demod_k)
		self.gr_freq_xlating_fir_filter_xxx_0 = gr.freq_xlating_fir_filter_ccc(channel_decim, (channel_taps), band_freq-freq+offset, sample_rate)
		self.gr_null_sink_0 = gr.null_sink(gr.sizeof_int*1)
		self.gr_null_sink_0_0 = gr.null_sink(gr.sizeof_int*1)
		self.gr_null_sink_0_1 = gr.null_sink(gr.sizeof_int*1)
		self.gr_null_sink_0_2 = gr.null_sink(gr.sizeof_int*1)
		self.pager_flex_deinterleave_0 = pager.flex_deinterleave()
		self.pager_flex_deinterleave_0_0 = pager.flex_deinterleave()
		self.pager_flex_deinterleave_0_1 = pager.flex_deinterleave()
		self.pager_flex_deinterleave_0_1_0 = pager.flex_deinterleave()
		self.pager_flex_sync_0 = pager.flex_sync()
		self.pager_slicer_fb_0 = pager.slicer_fb(1e-6)
		self.resampler = blks2.rational_resampler_fff(
			interpolation=bb_interp,
			decimation=bb_decim,
			taps=([1.0/ma_ntaps,]*ma_ntaps*bb_interp),
			fractional_bw=None,
		)
		self.usrp_source = grc_usrp.simple_source_c(which=0, side="A", rx_ant="RXA")
		self.usrp_source.set_decim_rate(decim)
		self.usrp_source.set_frequency(band_freq, verbose=True)
		self.usrp_source.set_gain(rx_gain)
		self.wxgui_fftsink2_0 = fftsink2.fft_sink_c(
			self.displays.GetPage(0).GetWin(),
			baseband_freq=band_freq,
			y_per_div=10,
			y_divs=10,
			ref_level=0,
			ref_scale=65536,
			sample_rate=sample_rate,
			fft_size=1024,
			fft_rate=30,
			average=False,
			avg_alpha=None,
			title="FLEX Spectrum",
			peak_hold=False,
		)
		self.displays.GetPage(0).GridAdd(self.wxgui_fftsink2_0.win, 0, 0, 1, 1)
		self.wxgui_fftsink2_1 = fftsink2.fft_sink_c(
			self.displays.GetPage(0).GetWin(),
			baseband_freq=freq,
			y_per_div=10,
			y_divs=10,
			ref_level=0,
			ref_scale=65536,
			sample_rate=channel_rate,
			fft_size=1024,
			fft_rate=30,
			average=False,
			avg_alpha=None,
			title="Channel Spectrum",
			peak_hold=False,
		)
		self.displays.GetPage(0).GridAdd(self.wxgui_fftsink2_1.win, 1, 0, 1, 1)
		self.wxgui_scopesink2_0_0 = scopesink2.scope_sink_f(
			self.displays.GetPage(1).GetWin(),
			title="Baseband",
			sample_rate=16e3,
			v_scale=1,
			v_offset=0,
			t_scale=40.0/16e3,
			ac_couple=False,
			xy_mode=False,
			num_inputs=1,
		)
		self.displays.GetPage(1).GridAdd(self.wxgui_scopesink2_0_0.win, 0, 0, 1, 1)

		##################################################
		# Connections
		##################################################
		self.connect((self.gr_freq_xlating_fir_filter_xxx_0, 0), (self.wxgui_fftsink2_1, 0))
		self.connect((self.usrp_source, 0), (self.gr_freq_xlating_fir_filter_xxx_0, 0))
		self.connect((self.usrp_source, 0), (self.wxgui_fftsink2_0, 0))
		self.connect((self.gr_freq_xlating_fir_filter_xxx_0, 0), (self.fm_demod, 0))
		self.connect((self.resampler, 0), (self.wxgui_scopesink2_0_0, 0))
		self.connect((self.fm_demod, 0), (self.resampler, 0))
		self.connect((self.pager_slicer_fb_0, 0), (self.pager_flex_sync_0, 0))
		self.connect((self.resampler, 0), (self.pager_slicer_fb_0, 0))
		self.connect((self.pager_flex_sync_0, 1), (self.pager_flex_deinterleave_0_1_0, 0))
		self.connect((self.pager_flex_sync_0, 2), (self.pager_flex_deinterleave_0_1, 0))
		self.connect((self.pager_flex_sync_0, 0), (self.pager_flex_deinterleave_0, 0))
		self.connect((self.pager_flex_sync_0, 3), (self.pager_flex_deinterleave_0_0, 0))
		self.connect((self.pager_flex_deinterleave_0, 0), (self.gr_null_sink_0, 0))
		self.connect((self.pager_flex_deinterleave_0_1_0, 0), (self.gr_null_sink_0_0, 0))
		self.connect((self.pager_flex_deinterleave_0_1, 0), (self.gr_null_sink_0_1, 0))
		self.connect((self.pager_flex_deinterleave_0_0, 0), (self.gr_null_sink_0_2, 0))

	def set_config_filename(self, config_filename):
		self.config_filename = config_filename
		self._saved_band_freq_config = ConfigParser.ConfigParser()
		self._saved_band_freq_config.read(self.config_filename)
		if not self._saved_band_freq_config.has_section("gr-pager"):
			self._saved_band_freq_config.add_section("gr-pager")
		self._saved_band_freq_config.set("gr-pager", "band_center", str(self.band_freq))
		self._saved_band_freq_config.write(open(self.config_filename, 'w'))
		self._saved_channel_config = ConfigParser.ConfigParser()
		self._saved_channel_config.read(self.config_filename)
		if not self._saved_channel_config.has_section("gr-pager"):
			self._saved_channel_config.add_section("gr-pager")
		self._saved_channel_config.set("gr-pager", "channel", str(self.channel))
		self._saved_channel_config.write(open(self.config_filename, 'w'))
		self._saved_offset_config = ConfigParser.ConfigParser()
		self._saved_offset_config.read(self.config_filename)
		if not self._saved_offset_config.has_section("gr-pager"):
			self._saved_offset_config.add_section("gr-pager")
		self._saved_offset_config.set("gr-pager", "freq_offset", str(self.offset))
		self._saved_offset_config.write(open(self.config_filename, 'w'))
		self._saved_rx_gain_config = ConfigParser.ConfigParser()
		self._saved_rx_gain_config.read(self.config_filename)
		if not self._saved_rx_gain_config.has_section("gr-pager"):
			self._saved_rx_gain_config.add_section("gr-pager")
		self._saved_rx_gain_config.set("gr-pager", "rx_gain", str(self.rx_gain))
		self._saved_rx_gain_config.write(open(self.config_filename, 'w'))

	def set_symbol_rate(self, symbol_rate):
		self.symbol_rate = symbol_rate
		self.set_passband(2*(self.deviation+self.symbol_rate))
		self.set_ma_ntaps(int(self.channel_rate/self.symbol_rate))

	def set_saved_channel(self, saved_channel):
		self.saved_channel = saved_channel
		self.set_channel(self.saved_channel)

	def set_saved_band_freq(self, saved_band_freq):
		self.saved_band_freq = saved_band_freq
		self.set_band_freq(self.saved_band_freq)

	def set_deviation(self, deviation):
		self.deviation = deviation
		self.set_demod_k(3*self.channel_rate/(2*math.pi*self.deviation))
		self.set_passband(2*(self.deviation+self.symbol_rate))

	def set_decim(self, decim):
		self.decim = decim
		self.set_sample_rate(self.adc_rate/self.decim)
		self.usrp_source.set_decim_rate(self.decim)

	def set_adc_rate(self, adc_rate):
		self.adc_rate = adc_rate
		self.set_sample_rate(self.adc_rate/self.decim)

	def set_sample_rate(self, sample_rate):
		self.sample_rate = sample_rate
		self.wxgui_fftsink2_0.set_sample_rate(self.sample_rate)
		self.set_channel_decim(int(self.sample_rate/self.channel_rate))
		self.set_channel_taps(firdes.low_pass(10, self.sample_rate, self.passband/2.0, (self.channel_rate-self.passband)/2.0))

	def set_passband(self, passband):
		self.passband = passband
		self.set_channel_taps(firdes.low_pass(10, self.sample_rate, self.passband/2.0, (self.channel_rate-self.passband)/2.0))

	def set_channel_rate(self, channel_rate):
		self.channel_rate = channel_rate
		self.wxgui_fftsink2_1.set_sample_rate(self.channel_rate)
		self.set_channel_decim(int(self.sample_rate/self.channel_rate))
		self.set_demod_k(3*self.channel_rate/(2*math.pi*self.deviation))
		self.set_channel_taps(firdes.low_pass(10, self.sample_rate, self.passband/2.0, (self.channel_rate-self.passband)/2.0))
		self.set_ma_ntaps(int(self.channel_rate/self.symbol_rate))

	def set_channel(self, channel):
		self.channel = channel
		self.set_freq(self.band_freq+(self.channel-61)*25e3)
		self._saved_channel_config = ConfigParser.ConfigParser()
		self._saved_channel_config.read(self.config_filename)
		if not self._saved_channel_config.has_section("gr-pager"):
			self._saved_channel_config.add_section("gr-pager")
		self._saved_channel_config.set("gr-pager", "channel", str(self.channel))
		self._saved_channel_config.write(open(self.config_filename, 'w'))
		self._channel_slider.set_value(self.channel)
		self._channel_text_box.set_value(self.channel)

	def set_band_freq(self, band_freq):
		self.band_freq = band_freq
		self.set_freq(self.band_freq+(self.channel-61)*25e3)
		self.wxgui_fftsink2_0.set_baseband_freq(self.band_freq)
		self.usrp_source.set_frequency(self.band_freq)
		self.gr_freq_xlating_fir_filter_xxx_0.set_center_freq(self.band_freq-self.freq+self.offset)
		self._saved_band_freq_config = ConfigParser.ConfigParser()
		self._saved_band_freq_config.read(self.config_filename)
		if not self._saved_band_freq_config.has_section("gr-pager"):
			self._saved_band_freq_config.add_section("gr-pager")
		self._saved_band_freq_config.set("gr-pager", "band_center", str(self.band_freq))
		self._saved_band_freq_config.write(open(self.config_filename, 'w'))
		self._band_freq_text_box.set_value(self.band_freq)

	def set_saved_rx_gain(self, saved_rx_gain):
		self.saved_rx_gain = saved_rx_gain
		self.set_rx_gain(self.saved_rx_gain)

	def set_saved_offset(self, saved_offset):
		self.saved_offset = saved_offset
		self.set_offset(self.saved_offset)

	def set_freq(self, freq):
		self.freq = freq
		self.gr_freq_xlating_fir_filter_xxx_0.set_center_freq(self.band_freq-self.freq+self.offset)
		self.wxgui_fftsink2_1.set_baseband_freq(self.freq)
		self.set_freq_text(self.freq)

	def set_channel_taps(self, channel_taps):
		self.channel_taps = channel_taps
		self.gr_freq_xlating_fir_filter_xxx_0.set_taps((self.channel_taps))
		self.set_nchan_taps(len(self.channel_taps))

	def set_rx_gain(self, rx_gain):
		self.rx_gain = rx_gain
		self.usrp_source.set_gain(self.rx_gain)
		self._saved_rx_gain_config = ConfigParser.ConfigParser()
		self._saved_rx_gain_config.read(self.config_filename)
		if not self._saved_rx_gain_config.has_section("gr-pager"):
			self._saved_rx_gain_config.add_section("gr-pager")
		self._saved_rx_gain_config.set("gr-pager", "rx_gain", str(self.rx_gain))
		self._saved_rx_gain_config.write(open(self.config_filename, 'w'))
		self._rx_gain_slider.set_value(self.rx_gain)
		self._rx_gain_text_box.set_value(self.rx_gain)

	def set_offset(self, offset):
		self.offset = offset
		self.gr_freq_xlating_fir_filter_xxx_0.set_center_freq(self.band_freq-self.freq+self.offset)
		self._saved_offset_config = ConfigParser.ConfigParser()
		self._saved_offset_config.read(self.config_filename)
		if not self._saved_offset_config.has_section("gr-pager"):
			self._saved_offset_config.add_section("gr-pager")
		self._saved_offset_config.set("gr-pager", "freq_offset", str(self.offset))
		self._saved_offset_config.write(open(self.config_filename, 'w'))
		self._offset_slider.set_value(self.offset)
		self._offset_text_box.set_value(self.offset)

	def set_nchan_taps(self, nchan_taps):
		self.nchan_taps = nchan_taps

	def set_ma_ntaps(self, ma_ntaps):
		self.ma_ntaps = ma_ntaps

	def set_freq_text(self, freq_text):
		self.freq_text = freq_text
		self._freq_text_static_text.set_value(self.freq_text)

	def set_demod_k(self, demod_k):
		self.demod_k = demod_k

	def set_channel_decim(self, channel_decim):
		self.channel_decim = channel_decim

	def set_bb_interp(self, bb_interp):
		self.bb_interp = bb_interp

	def set_bb_decim(self, bb_decim):
		self.bb_decim = bb_decim

	def set_baseband_rate(self, baseband_rate):
		self.baseband_rate = baseband_rate

if __name__ == '__main__':
	parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
	(options, args) = parser.parse_args()
	tb = usrp_rx_flex()
	tb.Run(True)