Use a GNURadio Embedded Python Block to add your own processing blocks to GRC flow graphs. This tutorial and video will get you started.
Recently, I was experimenting with how different AM detectors performed in GNURadio. I wanted to try a plain, old fashioned envelope detector. An envelope detector is simply a diode that rectifies the signal, followed by a low pass filter to get rid of the carrier. Unfortunately, I discovered that GNURadio does not have a block for this purpose. Could I somehow create one?
The answer is yes. And the tool is called the GNURadio Embedded Python Block. Here’s how it works.
GNURadio is basically a bunch of signal processing blocks and a synchronization engine under the hood. The basic blocks are all written in C++ and run fast. The processing blocks are glued together with Python scripts. Python is not a high performance language, but it is fast enough to run the overall GR program. In fact, GNURadio Companion is just a Python script that automates creation of underlying code to run a GNURadio flow graph.
What I discovered is that you can also write processing blocks in Python and embed them in your flow graph. These are not as powerful as C++ but can work if you do not get too complex. GNURadio provides a tool for doing just this: the GNURadio Embedded Python Block.
So, I decided to build my own envelope detector in code. All that I needed was an algorithm and a Python editor. Oh, and I needed to learn some Python, too!
The overall process for writing your own processing block in Python is described in this video. Also, here is the source code.
GNURadio Embedded Python Block – Introduction
You will find the GNURadio Embedded Python Block in the Misc. section of the tree. When you insert it into your flow graph, it comes loaded with an example – a documented template to get your started. As shown in the video, there are four required steps to modify this template for your own purposes.
- Define the data type for the input and output data streams. Normally these are either “complex64” or “float32”. The example program inherits from the GNURadio object set up to manage a 1:1 data flow. The arrays in Python are called lists. The input and output arrays are “input_items” and “output_items”. The reason for the zero index is that GNRadio blocks send and receive lists of lists, and you want the first list for your data flow.
- Define your object’s properties. The example provides “self. example_param” If the same property is passed as an argument to the object’s initialization routine, the property will also show up as a parameter you can edit visually, and a callback is connected so the value can be changed during execution. Also, properties will be remembered between block executions.
- Write your processing code in the “work” routine. This is what gets called each time the block executes.
- Change the name of the block and add whatever documentation. Actually, changing the block name is the first thing you should do.
I find it useful to use a separate folder for each GRC flow graph that contains embedded blocks. This keeps all the code separate. Hopefully, this is enough information to get you started using the GNURadio Embedded Python Block.