pythonでgnuradio その26

# 概要
pythonでgnuradioやってみた。
練習問題、やってみた。

# 練習問題
USBトングルで、周波数をサーチせよ。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/1287077d-ba14-a920-902b-08d0e3a534a9.png)

# サンプルコード

```

from PyQt5 import Qt
from gnuradio import qtgui
from PyQt5 import QtCore
from gnuradio import gr
from gnuradio.filter import firdes
from gnuradio.fft import window
import sys
import signal
from argparse import ArgumentParser
from gnuradio.eng_arg import eng_float, intx
from gnuradio import eng_notation
from gnuradio import soapy
import sip

class rtl1(gr.top_block, Qt.QWidget):
	def __init__(self):
		gr.top_block.__init__(self, "Not titled yet", catch_exceptions = True)
		Qt.QWidget.__init__(self)
		self.setWindowTitle("Not titled yet")
		qtgui.util.check_set_qss()
		try:
			self.setWindowIcon(Qt.QIcon.fromTheme('gnuradio-grc'))
		except BaseException as exc:
			print(f"Qt GUI: Could not set Icon: {str(exc)}", file = sys.stderr)
		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", "rtl1")
		try:
			geometry = self.settings.value("geometry")
			if geometry:
				self.restoreGeometry(geometry)
		except BaseException as exc:
			print(f"Qt GUI: Could not restore geometry: {str(exc)}", file = sys.stderr)
		self.samp_rate = samp_rate = 1024000
		self.freq = freq = 77300000
		self._freq_range = qtgui.Range(24000000, 1000000000, 100000, 77300000, 200)
		self._freq_win = qtgui.RangeWidget(self._freq_range, self.set_freq, "'freq'", "counter_slider", float, QtCore.Qt.Horizontal)
		self.top_layout.addWidget(self._freq_win)
		self.soapy_rtlsdr_source_0 = None
		dev = 'driver=rtlsdr'
		stream_args = 'bufflen=16384'
		tune_args = ['']
		settings = ['']
		def _set_soapy_rtlsdr_source_0_gain_mode(channel, agc):
			self.soapy_rtlsdr_source_0.set_gain_mode(channel, agc)
			if not agc:
				  self.soapy_rtlsdr_source_0.set_gain(channel, self._soapy_rtlsdr_source_0_gain_value)
		self.set_soapy_rtlsdr_source_0_gain_mode = _set_soapy_rtlsdr_source_0_gain_mode
		def _set_soapy_rtlsdr_source_0_gain(channel, name, gain):
			self._soapy_rtlsdr_source_0_gain_value = gain
			if not self.soapy_rtlsdr_source_0.get_gain_mode(channel):
				self.soapy_rtlsdr_source_0.set_gain(channel, gain)
		self.set_soapy_rtlsdr_source_0_gain = _set_soapy_rtlsdr_source_0_gain
		def _set_soapy_rtlsdr_source_0_bias(bias):
			if 'biastee' in self._soapy_rtlsdr_source_0_setting_keys:
				self.soapy_rtlsdr_source_0.write_setting('biastee', bias)
		self.set_soapy_rtlsdr_source_0_bias = _set_soapy_rtlsdr_source_0_bias
		self.soapy_rtlsdr_source_0 = soapy.source(dev, "fc32", 1, '', stream_args, tune_args, settings)
		self._soapy_rtlsdr_source_0_setting_keys = [a.key for a in self.soapy_rtlsdr_source_0.get_setting_info()]
		self.soapy_rtlsdr_source_0.set_sample_rate(0, samp_rate)
		self.soapy_rtlsdr_source_0.set_frequency(0, freq)
		self.soapy_rtlsdr_source_0.set_frequency_correction(0, 0)
		self.set_soapy_rtlsdr_source_0_bias(bool(False))
		self._soapy_rtlsdr_source_0_gain_value = 20
		self.set_soapy_rtlsdr_source_0_gain_mode(0, bool(False))
		self.set_soapy_rtlsdr_source_0_gain(0, 'TUNER', 20)
		self.qtgui_sink_x_0 = qtgui.sink_c(1024, window.WIN_BLACKMAN_hARRIS, 0, samp_rate, "", True, True, True, True, None)
		self.qtgui_sink_x_0.set_update_time(1.0 / 10)
		self._qtgui_sink_x_0_win = sip.wrapinstance(self.qtgui_sink_x_0.qwidget(), Qt.QWidget)
		self.qtgui_sink_x_0.enable_rf_freq(False)
		self.top_layout.addWidget(self._qtgui_sink_x_0_win)
		self.connect((self.soapy_rtlsdr_source_0, 0), (self.qtgui_sink_x_0, 0))
	def closeEvent(self, event):
		self.settings = Qt.QSettings("GNU Radio", "rtl1")
		self.settings.setValue("geometry", self.saveGeometry())
		self.stop()
		self.wait()
		event.accept()
	def get_samp_rate(self):
		return self.samp_rate
	def set_samp_rate(self, samp_rate):
		self.samp_rate = samp_rate
		self.qtgui_sink_x_0.set_frequency_range(0, self.samp_rate)
		self.soapy_rtlsdr_source_0.set_sample_rate(0, self.samp_rate)
	def get_freq(self):
		return self.freq
	def set_freq(self, freq):
		self.freq = freq
		self.soapy_rtlsdr_source_0.set_frequency(0, self.freq)

def main(top_block_cls = rtl1, options = None):
	qapp = Qt.QApplication(sys.argv)
	tb = top_block_cls()
	tb.start()
	tb.show()
	def sig_handler(sig = None, frame = None):
		tb.stop()
		tb.wait()
		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)
	qapp.exec_()

if __name__ == '__main__':
	main()





```


以上。