GNU Radio Companion - GNU Radio Embedded Python Example 1

GNUR Radio Embedded Python Example 1

Prepared by Dr. Aaron Scher
aaron.scher@oit.edu
Oregon Institute of Technology

Back to My collection of GNU radio companion flow graphs
Back to Aaron's home page.

(Last updated: Aug 2016)

Download file: GNC_Embedded_Example1_Simple_First_example.grc.

Flow Graph:

Mod/Demod

Double click on "Embedded Python Block" and you get this menu screen:

Mod/Demod

Click the bottom that says "Open in Editor" and you can enter Python code. Here is the Python code for this example:

 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
"""
Embedded Python Blocks:

Each time this file is saved, GRC will instantiate the first class it finds
to get ports and parameters of your block. The arguments to __init__  will
be the parameters. All of them are required to have default values!
"""

import numpy as np
from gnuradio import gr


class blk(gr.sync_block):  # other base classes are basic_block, decim_block, interp_block
    """Embedded Python Block example - a simple multiply const"""

    def __init__(self, example_param=1):  # only default arguments here
        """arguments to this function show up as parameters in GRC"""
        gr.sync_block.__init__(
            self,
            name='Embedded Python Block',   # will show up in GRC
            in_sig=[np.float32],
            out_sig=[np.float32]
        )
        # if an attribute with the same name as a parameter is found,
        # a callback is registered (properties work, too).
        self.example_param = example_param

    def work(self, input_items, output_items):
        """example: multiply with constant"""
        print "length of input vector =",  len(input_items[0])
        print "length of output vector =",  len(output_items[0])
        output_items[0][:] = input_items[0] * self.example_param
        return len(output_items[0])

Description

This flow graph doesn't do much. I made it to test out the Embedded Python Block. The source for this flow graph is a square wave. The Embedded Python Block simply multiplies the square wave by a constant that is set by the variable "variable_amplitude". The Embedded Python Block also displays (i.e. prints) the lengths of the input and output vectors for each instance the block receives new data from the proceeding block. Notice that the input and output vectors are equal (as they should be).