Difference between revisions of "Flowgraph Python Code"

From GNU Radio
Jump to: navigation, search
Line 1: Line 1:
 
== Dial Tone Flowgraph  ==
 
== Dial Tone Flowgraph  ==
  
The following is a dial-tone example using gnuradio-companion (GRC).
+
The following example flowgraph implements a dial-tone:
  
 
https://raw.githubusercontent.com/gnuradio/gr-tutorial/master/examples/tutorial3/images/tutorial_three_1.png
 
https://raw.githubusercontent.com/gnuradio/gr-tutorial/master/examples/tutorial3/images/tutorial_three_1.png

Revision as of 17:27, 20 January 2020

Dial Tone Flowgraph

The following example flowgraph implements a dial-tone:

tutorial_three_1.png

When we click the Generate button, the terminal tells us that it produced a "tutorial_three_1.py" file, so let's open it to examine the code.

  1 #!/usr/bin/env python3
  2 # -*- coding: utf-8 -*-
  3 
  4 #
  5 # SPDX-License-Identifier: GPL-3.0
  6 #
  7 # GNU Radio Python Flow Graph
  8 # Title: tutorial_three_1
  9 # GNU Radio version: 3.8.0.0
 10 
 11 from distutils.version import StrictVersion
 12 
 13 if __name__ == '__main__':
 14     import ctypes
 15     import sys
 16     if sys.platform.startswith('linux'):
 17         try:
 18             x11 = ctypes.cdll.LoadLibrary('libX11.so')
 19             x11.XInitThreads()
 20         except:
 21             print("Warning: failed to XInitThreads()")
 22 
 23 from gnuradio import analog
 24 from gnuradio import audio
 25 from gnuradio import gr
 26 from gnuradio.filter import firdes
 27 import sys
 28 import signal
 29 from PyQt5 import Qt
 30 from argparse import ArgumentParser
 31 from gnuradio.eng_arg import eng_float, intx
 32 from gnuradio import eng_notation
 33 from gnuradio import qtgui
 34 
 35 class tutorial_three_1(gr.top_block, Qt.QWidget):
 36 
 37     def __init__(self):
 38         gr.top_block.__init__(self, "tutorial_three_1")
 39         Qt.QWidget.__init__(self)
 40         self.setWindowTitle("tutorial_three_1")
 41         qtgui.util.check_set_qss()
 42         try:
 43             self.setWindowIcon(Qt.QIcon.fromTheme('gnuradio-grc'))
 44         except:
 45             pass
 46         self.top_scroll_layout = Qt.QVBoxLayout()
 47         self.setLayout(self.top_scroll_layout)
 48         self.top_scroll = Qt.QScrollArea()
 49         self.top_scroll.setFrameStyle(Qt.QFrame.NoFrame)
 50         self.top_scroll_layout.addWidget(self.top_scroll)
 51         self.top_scroll.setWidgetResizable(True)
 52         self.top_widget = Qt.QWidget()
 53         self.top_scroll.setWidget(self.top_widget)
 54         self.top_layout = Qt.QVBoxLayout(self.top_widget)
 55         self.top_grid_layout = Qt.QGridLayout()
 56         self.top_layout.addLayout(self.top_grid_layout)
 57 
 58         self.settings = Qt.QSettings("GNU Radio", "tutorial_three_1")
 59 
 60         try:
 61             if StrictVersion(Qt.qVersion()) < StrictVersion("5.0.0"):
 62                 self.restoreGeometry(self.settings.value("geometry").toByteArray())
 63             else:
 64                 self.restoreGeometry(self.settings.value("geometry"))
 65         except:
 66             pass
 67 
 68         ##################################################
 69         # Variables
 70         ##################################################
 71         self.samp_rate = samp_rate = 32000
 72 
 73         ##################################################
 74         # Blocks
 75         ##################################################
 76         self.audio_sink_0 = audio.sink(samp_rate, '', True)
 77         self.analog_sig_source_x_1 = analog.sig_source_f(samp_rate, analog.GR_COS_WAVE, 350, 0.1, 0, 0)
 78         self.analog_sig_source_x_0 = analog.sig_source_f(samp_rate, analog.GR_COS_WAVE, 440, 0.1, 0, 0)
 79 
 80 
 81 
 82         ##################################################
 83         # Connections
 84         ##################################################
 85         self.connect((self.analog_sig_source_x_0, 0), (self.audio_sink_0, 0))
 86         self.connect((self.analog_sig_source_x_1, 0), (self.audio_sink_0, 1))
 87 
 88     def closeEvent(self, event):
 89         self.settings = Qt.QSettings("GNU Radio", "tutorial_three_1")
 90         self.settings.setValue("geometry", self.saveGeometry())
 91         event.accept()
 92 
 93     def get_samp_rate(self):
 94         return self.samp_rate
 95 
 96     def set_samp_rate(self, samp_rate):
 97         self.samp_rate = samp_rate
 98         self.analog_sig_source_x_0.set_sampling_freq(self.samp_rate)
 99         self.analog_sig_source_x_1.set_sampling_freq(self.samp_rate)
100 
101 
102 
103 def main(top_block_cls=tutorial_three_1, options=None):
104 
105     if StrictVersion("4.5.0") <= StrictVersion(Qt.qVersion()) < StrictVersion("5.0.0"):
106         style = gr.prefs().get_string('qtgui', 'style', 'raster')
107         Qt.QApplication.setGraphicsSystem(style)
108     qapp = Qt.QApplication(sys.argv)
109 
110     tb = top_block_cls()
111     tb.start()
112     tb.show()
113 
114     def sig_handler(sig=None, frame=None):
115         Qt.QApplication.quit()
116 
117     signal.signal(signal.SIGINT, sig_handler)
118     signal.signal(signal.SIGTERM, sig_handler)
119 
120     timer = Qt.QTimer()
121     timer.start(500)
122     timer.timeout.connect(lambda: None)
123 
124     def quitting():
125         tb.stop()
126         tb.wait()
127     qapp.aboutToQuit.connect(quitting)
128     qapp.exec_()
129 
130 
131 if __name__ == '__main__':
132     main()

Once GRC has created a Python file, the user is free to modify it in any desired manner, such as changing parameters, sample rate, and even connections among the blocks.

To execute this file from a terminal, enter:
python3 tutorial_three_1.py

Warning: After the Python file has been modified, running GRC again with that flowgraph will wipe out your changes!

Dial Tone Python Code Dissected

Let's examine pertinent lines of the code:

#!/usr/bin/env python3

This tells the shell to use the Python3 interpreter to run this file.

from gnuradio import analog
from gnuradio import audio
from gnuradio import gr

These tell Python what modules to include. We must always have gr to run GNU Radio applications. The audio sink is included in the audio module and the signal source is included in the analog module.

class tutorial_three_1(gr.top_block, Qt.QWidget):

Define a class called "tutorial_three_1" which is derived from another class, gr.top_block. This class is basically a container for the flow graph. By deriving from gr.top_block, we get all the hooks and functions we need to add blocks and interconnect them.

def __init__(self):

Only one member function is defined for this class: the function "init()", which is the constructor of this class.

gr.top_block.__init__(self, "tutorial_three_1")

The parent constructor is called.

self.samp_rate = samp_rate = 32000

Variable declaration for sample rate.

self.connect((self.analog_sig_source_x_0, 0), (self.audio_sink_0, 0))
self.connect((self.analog_sig_source_x_1, 0), (self.audio_sink_0, 1))

There are 2 inputs to the Audio Sink block. The first line connects the only output of analog_sig_source_x_0 (350 Hz waveform) to the first input of audio_sink_0. The second line connects the only output of analog_sig_source_x_1 (440 Hz waveform) to the second input of audio_sink_0.