-
Notifications
You must be signed in to change notification settings - Fork 18
/
FM_Recever.py
executable file
·365 lines (313 loc) · 14.1 KB
/
FM_Recever.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# SPDX-License-Identifier: GPL-3.0
#
# GNU Radio Python Flow Graph
# Title: This is an Fm Recever
# Author: anton
# GNU Radio version: 3.8.1.0
from distutils.version import StrictVersion
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()")
from PyQt5 import Qt
from PyQt5.QtCore import QObject, pyqtSlot
from gnuradio import qtgui
from gnuradio.filter import firdes
import sip
from gnuradio import analog
from gnuradio import audio
from gnuradio import blocks
from gnuradio import filter
from gnuradio import gr
import sys
import signal
from argparse import ArgumentParser
from gnuradio.eng_arg import eng_float, intx
from gnuradio import eng_notation
from gnuradio.qtgui import Range, RangeWidget
import osmosdr
import time
from gnuradio import qtgui
class FM_Recever(gr.top_block, Qt.QWidget):
def __init__(self):
gr.top_block.__init__(self, "This is an Fm Recever")
Qt.QWidget.__init__(self)
self.setWindowTitle("This is an Fm Recever")
qtgui.util.check_set_qss()
try:
self.setWindowIcon(Qt.QIcon.fromTheme('gnuradio-grc'))
except:
pass
self.top_scroll_layout = Qt.QVBoxLayout()
self.setLayout(self.top_scroll_layout)
self.top_scroll = Qt.QScrollArea()
self.top_scroll.setFrameStyle(Qt.QFrame.NoFrame)
self.top_scroll_layout.addWidget(self.top_scroll)
self.top_scroll.setWidgetResizable(True)
self.top_widget = Qt.QWidget()
self.top_scroll.setWidget(self.top_widget)
self.top_layout = Qt.QVBoxLayout(self.top_widget)
self.top_grid_layout = Qt.QGridLayout()
self.top_layout.addLayout(self.top_grid_layout)
self.settings = Qt.QSettings("GNU Radio", "FM_Recever")
try:
if StrictVersion(Qt.qVersion()) < StrictVersion("5.0.0"):
self.restoreGeometry(self.settings.value("geometry").toByteArray())
else:
self.restoreGeometry(self.settings.value("geometry"))
except:
pass
##################################################
# Variables
##################################################
self.samp_rate = samp_rate = 1800000
self.decimation = decimation = 72
self.channel_width = channel_width = int(samp_rate/decimation)
self.xlate_lpf_taps = xlate_lpf_taps = firdes.low_pass(1.0, samp_rate, channel_width/2,channel_width/10, firdes.WIN_HAMMING, 6.76)
self.volume = volume = 25
self.tune_freq = tune_freq = 390
self.squelch = squelch = -55
self.rf_gain = rf_gain = 10
self.mode = mode = 0
self.center_freq = center_freq = 144.7
##################################################
# Blocks
##################################################
self._volume_range = Range(0, 100, 1, 25, 10)
self._volume_win = RangeWidget(self._volume_range, self.set_volume, 'Volume', "counter", float)
self.top_grid_layout.addWidget(self._volume_win, 0, 6, 1, 1)
for r in range(0, 1):
self.top_grid_layout.setRowStretch(r, 1)
for c in range(6, 7):
self.top_grid_layout.setColumnStretch(c, 1)
self._tune_freq_range = Range(-600, 600, 5, 390, 10)
self._tune_freq_win = RangeWidget(self._tune_freq_range, self.set_tune_freq, 'Tune Freq (kHz)', "counter", int)
self.top_grid_layout.addWidget(self._tune_freq_win, 0, 1, 1, 1)
for r in range(0, 1):
self.top_grid_layout.setRowStretch(r, 1)
for c in range(1, 2):
self.top_grid_layout.setColumnStretch(c, 1)
self._squelch_range = Range(-100, 0, 1, -55, 10)
self._squelch_win = RangeWidget(self._squelch_range, self.set_squelch, 'Squelch', "counter", float)
self.top_grid_layout.addWidget(self._squelch_win, 0, 5, 1, 1)
for r in range(0, 1):
self.top_grid_layout.setRowStretch(r, 1)
for c in range(5, 6):
self.top_grid_layout.setColumnStretch(c, 1)
self._rf_gain_range = Range(0, 50, 1, 10, 10)
self._rf_gain_win = RangeWidget(self._rf_gain_range, self.set_rf_gain, 'RF Gain', "counter", float)
self.top_grid_layout.addWidget(self._rf_gain_win, 0, 2, 1, 1)
for r in range(0, 1):
self.top_grid_layout.setRowStretch(r, 1)
for c in range(2, 3):
self.top_grid_layout.setColumnStretch(c, 1)
self._center_freq_range = Range(1, 2000, 1, 144.7, 10)
self._center_freq_win = RangeWidget(self._center_freq_range, self.set_center_freq, 'Center Freq (MHz)', "counter", float)
self.top_grid_layout.addWidget(self._center_freq_win, 0, 0, 1, 1)
for r in range(0, 1):
self.top_grid_layout.setRowStretch(r, 1)
for c in range(0, 1):
self.top_grid_layout.setColumnStretch(c, 1)
self.rtlsdr_source_0 = osmosdr.source(
args="numchan=" + str(1) + " " + ''
)
self.rtlsdr_source_0.set_time_unknown_pps(osmosdr.time_spec_t())
self.rtlsdr_source_0.set_sample_rate(samp_rate)
self.rtlsdr_source_0.set_center_freq(center_freq * 1e6, 0)
self.rtlsdr_source_0.set_freq_corr(0, 0)
self.rtlsdr_source_0.set_gain(rf_gain, 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.qtgui_sink_x_0 = qtgui.sink_c(
1024, #fftsize
firdes.WIN_BLACKMAN, #wintype
(center_freq *1e6) + (tune_freq *1e3), #fc
channel_width, #bw
"Channel Spectrum", #name
True, #plotfreq
True, #plotwaterfall
False, #plottime
False #plotconst
)
self.qtgui_sink_x_0.set_update_time(1.0/10)
self._qtgui_sink_x_0_win = sip.wrapinstance(self.qtgui_sink_x_0.pyqwidget(), Qt.QWidget)
self.qtgui_sink_x_0.enable_rf_freq(True)
self.top_grid_layout.addWidget(self._qtgui_sink_x_0_win, 4, 0, 5, 4)
for r in range(4, 9):
self.top_grid_layout.setRowStretch(r, 1)
for c in range(0, 4):
self.top_grid_layout.setColumnStretch(c, 1)
self.qtgui_freq_sink_x_0 = qtgui.freq_sink_c(
1024, #size
firdes.WIN_BLACKMAN_hARRIS, #wintype
center_freq * 1e6, #fc
samp_rate, #bw
'RF Spectrum', #name
1
)
self.qtgui_freq_sink_x_0.set_update_time(0.10)
self.qtgui_freq_sink_x_0.set_y_axis(-90, -20)
self.qtgui_freq_sink_x_0.set_y_label('Relative Gain', 'dB')
self.qtgui_freq_sink_x_0.set_trigger_mode(qtgui.TRIG_MODE_FREE, 0.0, 0, "")
self.qtgui_freq_sink_x_0.enable_autoscale(False)
self.qtgui_freq_sink_x_0.enable_grid(True)
self.qtgui_freq_sink_x_0.set_fft_average(0.05)
self.qtgui_freq_sink_x_0.enable_axis_labels(True)
self.qtgui_freq_sink_x_0.enable_control_panel(False)
self.qtgui_freq_sink_x_0.disable_legend()
labels = ['', '', '', '', '',
'', '', '', '', '']
widths = [1, 1, 1, 1, 1,
1, 1, 1, 1, 1]
colors = ["blue", "red", "green", "black", "cyan",
"magenta", "yellow", "dark red", "dark green", "dark blue"]
alphas = [1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0]
for i in range(1):
if len(labels[i]) == 0:
self.qtgui_freq_sink_x_0.set_line_label(i, "Data {0}".format(i))
else:
self.qtgui_freq_sink_x_0.set_line_label(i, labels[i])
self.qtgui_freq_sink_x_0.set_line_width(i, widths[i])
self.qtgui_freq_sink_x_0.set_line_color(i, colors[i])
self.qtgui_freq_sink_x_0.set_line_alpha(i, alphas[i])
self._qtgui_freq_sink_x_0_win = sip.wrapinstance(self.qtgui_freq_sink_x_0.pyqwidget(), Qt.QWidget)
self.top_grid_layout.addWidget(self._qtgui_freq_sink_x_0_win, 1, 0, 2, 8)
for r in range(1, 3):
self.top_grid_layout.setRowStretch(r, 1)
for c in range(0, 8):
self.top_grid_layout.setColumnStretch(c, 1)
# Create the options list
self._mode_options = (0, 1, )
# Create the labels list
self._mode_labels = ('FM', 'AM', )
# Create the combo box
self._mode_tool_bar = Qt.QToolBar(self)
self._mode_tool_bar.addWidget(Qt.QLabel('Mode' + ": "))
self._mode_combo_box = Qt.QComboBox()
self._mode_tool_bar.addWidget(self._mode_combo_box)
for _label in self._mode_labels: self._mode_combo_box.addItem(_label)
self._mode_callback = lambda i: Qt.QMetaObject.invokeMethod(self._mode_combo_box, "setCurrentIndex", Qt.Q_ARG("int", self._mode_options.index(i)))
self._mode_callback(self.mode)
self._mode_combo_box.currentIndexChanged.connect(
lambda i: self.set_mode(self._mode_options[i]))
# Create the radio buttons
self.top_grid_layout.addWidget(self._mode_tool_bar, 0, 7, 1, 1)
for r in range(0, 1):
self.top_grid_layout.setRowStretch(r, 1)
for c in range(7, 8):
self.top_grid_layout.setColumnStretch(c, 1)
self.freq_xlating_fir_filter_xxx_0 = filter.freq_xlating_fir_filter_ccc(decimation, xlate_lpf_taps, tune_freq * 1e3, samp_rate)
self.blocks_multiply_const_vxx_0 = blocks.multiply_const_ff(volume/50.0)
self.audio_sink_0 = audio.sink(channel_width, '', True)
self.analog_pwr_squelch_xx_0 = analog.pwr_squelch_cc(squelch, 1e-4, 0, True)
self.analog_fm_demod_cf_0 = analog.fm_demod_cf(
channel_rate=channel_width,
audio_decim=1,
deviation=5000,
audio_pass=3000,
audio_stop=4000,
gain=1.0,
tau=75e-6,
)
##################################################
# Connections
##################################################
self.connect((self.analog_fm_demod_cf_0, 0), (self.blocks_multiply_const_vxx_0, 0))
self.connect((self.analog_pwr_squelch_xx_0, 0), (self.analog_fm_demod_cf_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_pwr_squelch_xx_0, 0))
self.connect((self.freq_xlating_fir_filter_xxx_0, 0), (self.qtgui_sink_x_0, 0))
self.connect((self.rtlsdr_source_0, 0), (self.freq_xlating_fir_filter_xxx_0, 0))
self.connect((self.rtlsdr_source_0, 0), (self.qtgui_freq_sink_x_0, 0))
def closeEvent(self, event):
self.settings = Qt.QSettings("GNU Radio", "FM_Recever")
self.settings.setValue("geometry", self.saveGeometry())
event.accept()
def get_samp_rate(self):
return self.samp_rate
def set_samp_rate(self, samp_rate):
self.samp_rate = samp_rate
self.set_channel_width(int(self.samp_rate/self.decimation))
self.qtgui_freq_sink_x_0.set_frequency_range(self.center_freq * 1e6, self.samp_rate)
self.rtlsdr_source_0.set_sample_rate(self.samp_rate)
def get_decimation(self):
return self.decimation
def set_decimation(self, decimation):
self.decimation = decimation
self.set_channel_width(int(self.samp_rate/self.decimation))
def get_channel_width(self):
return self.channel_width
def set_channel_width(self, channel_width):
self.channel_width = channel_width
self.qtgui_sink_x_0.set_frequency_range((self.center_freq *1e6) + (self.tune_freq *1e3), self.channel_width)
def get_xlate_lpf_taps(self):
return self.xlate_lpf_taps
def set_xlate_lpf_taps(self, xlate_lpf_taps):
self.xlate_lpf_taps = xlate_lpf_taps
self.freq_xlating_fir_filter_xxx_0.set_taps(self.xlate_lpf_taps)
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/50.0)
def get_tune_freq(self):
return self.tune_freq
def set_tune_freq(self, tune_freq):
self.tune_freq = tune_freq
self.freq_xlating_fir_filter_xxx_0.set_center_freq(self.tune_freq * 1e3)
self.qtgui_sink_x_0.set_frequency_range((self.center_freq *1e6) + (self.tune_freq *1e3), self.channel_width)
def get_squelch(self):
return self.squelch
def set_squelch(self, squelch):
self.squelch = squelch
self.analog_pwr_squelch_xx_0.set_threshold(self.squelch)
def get_rf_gain(self):
return self.rf_gain
def set_rf_gain(self, rf_gain):
self.rf_gain = rf_gain
self.rtlsdr_source_0.set_gain(self.rf_gain, 0)
def get_mode(self):
return self.mode
def set_mode(self, mode):
self.mode = mode
self._mode_callback(self.mode)
def get_center_freq(self):
return self.center_freq
def set_center_freq(self, center_freq):
self.center_freq = center_freq
self.qtgui_freq_sink_x_0.set_frequency_range(self.center_freq * 1e6, self.samp_rate)
self.qtgui_sink_x_0.set_frequency_range((self.center_freq *1e6) + (self.tune_freq *1e3), self.channel_width)
self.rtlsdr_source_0.set_center_freq(self.center_freq * 1e6, 0)
def main(top_block_cls=FM_Recever, options=None):
if StrictVersion("4.5.0") <= StrictVersion(Qt.qVersion()) < StrictVersion("5.0.0"):
style = gr.prefs().get_string('qtgui', 'style', 'raster')
Qt.QApplication.setGraphicsSystem(style)
qapp = Qt.QApplication(sys.argv)
tb = top_block_cls()
tb.start()
tb.show()
def sig_handler(sig=None, frame=None):
Qt.QApplication.quit()
signal.signal(signal.SIGINT, sig_handler)
signal.signal(signal.SIGTERM, sig_handler)
timer = Qt.QTimer()
timer.start(500)
timer.timeout.connect(lambda: None)
def quitting():
tb.stop()
tb.wait()
qapp.aboutToQuit.connect(quitting)
qapp.exec_()
if __name__ == '__main__':
main()