Variables in Flowgraphs

From GNU Radio
Jump to navigation Jump to search
Beginner Tutorials

Introducing GNU Radio

  1. What is GNU Radio?
  2. Installing GNU Radio
  3. Your First Flowgraph

Flowgraph Fundamentals

  1. Python Variables in GRC
  2. Variables in Flowgraphs
  3. Runtime Updating Variables
  4. Signal Data Types
  5. Converting Data Types
  6. Packing Bits
  7. Streams and Vectors
  8. Hier Blocks and Parameters

Creating and Modifying Python Blocks

  1. Creating Your First Block
  2. Python Block With Vectors
  3. Python Block Message Passing
  4. Python Block Tags

DSP Blocks

  1. Low Pass Filter Example
  2. Designing Filter Taps
  3. Sample Rate Change

This tutorial describes how to use variables in a flowgraph. The flowgraph from a previous tutorial (Your First Flowgraph) is used as a starting point for this tutorial. Please complete the Your First Flowgraph tutorial beforehand.

The previous tutorial, Python Variables in GRC, describes how GRC uses Python data types and how values are displayed in Variable blocks. The next tutorial, Runtime Updating Variables, demonstrates how variables are updated while a flowgraph is running.

Basic Variables[edit]

GRC allows a user to interact with GNURadio flowgraphs, either ones they create from scratch interactively, or ones that are read from a .grc file.

When the GRC user uses the play button to execute a flowgraph, GRC creates a .py Python file that contains the flowgraph's code.

Python code can have variables, and a GNURadio flowgraph can have variables created by the Variable block.

Every new flowgraph starts with the samp_rate variable:


GNURadio blocks are implemented as functions. GNU Radio blocks take parameters which modify the behavior. All of the blocks in the flowgraph above use samp_rate as a parameter. Create a new variable block by dragging and dropping it from the block library on the right:


Double-click the variable_0 block to view and modify the parameters.


The Id field is the name of the variable. The variable will be the frequency of the Signal Source block. Edit the name to frequency. Now edit the value to 4000.


Click OK to save.

Double-click the Signal Source block to modify the parameters:


The Frequency is set to 1000. Enter frequency into the Frequency field to use the variable:


Click OK to save the properties. The frequency variable and the value within the Signal Source block are updated:


Run the flowgraph:


The peak of the frequency response has moved to 4,000 due to the variable change.

Dependent Variables[edit]

Variables can be dependent on one another. The Id and Value fields are converted into a line of Python in the following manner:

Id = Value

The frequency variable was modified to accept the value 4000, which is the same as a line of Python code:

frequency = 4000

The frequency variable can also be dependent on another variable. Edit frequency to enter the value samp_rate/3, which for samp_rate = 32000 will be a frequency of 10,667.


The change is displayed in the flowgraph:


Running the flowgraph shows the frequency is updated:


The next tutorial, Runtime Updating Variables, demonstrates how variables are updated while a flowgraph is running.