Variables in Flowgraphs: Difference between revisions

From GNU Radio
Jump to navigation Jump to search
No edit summary
No edit summary
Line 69: Line 69:
The change will now ripple through the flowgraph:
The change will now ripple through the flowgraph:


[[File:UpdatedFrequencyFlowgraph.png|500px]]
[[File:UpdatedFrequencyFlowgraph.png|600px]]




Re-running the flowgraph shows that the frequency has been updated:
Re-running the flowgraph shows that the frequency has been updated:


[[File:FrequencySinkUpdatedFrequency.png|500px]]
[[File:FrequencySinkUpdatedFrequency.png|600px]]

Revision as of 19:09, 3 January 2022

This page describes how to use variables in a flowgraph. The flowgraph from the previous guide (Your First Flowgraph) will be used as a reference.

Basic Variables

A GNURadio flowgraph is a .py Python file. Just as Python code can have variables so too can a GNURadio flowgraph by using the Variable block.

Every new flowgraph starts with the samp_rate variable:

VariableSampRate.png


GNURadio blocks are implemented as functions and can take parameters which modify their 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:

NewVariableBlock.png


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

VariableProperties.png


The Id field is the name of the variable. The variable is going to represent the frequency of the Signal Source block, so edit the name to frequency. Now edit the value to 4000.

FrequencyVariable.png

Click OK to save.

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

SignalSourceProperties.png


You'll notice that samp_rate is the variable being used for the Sample Rate but Frequency is hard coded to 1000. Enter frequency into the Frequency field:

SignalSourceFrequency.png


Click OK to save the properties. You'll notice that the frequency variable name has been updated as well as the parameter within the Signal Source block:

FlowgraphWithFrequencyVariable.png


Re-run the flowgraph:

FlowgraphNewFrequencyOutput.png


You'll notice that the peak of the frequency response has moved to 4,000 due to the variable that was added.


Dependent Variables

Variables can be dependent on one another in the same way variables in Python can be derived from other variables.

The Id and Value fields are converted into a line of Python in the following manner:

Id = Value

Previously 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 such that it is now samp_rate/3, which for samp_rate = 32000 will be a frequency of 10,667.

ExampleDependentVariable.png


The change will now ripple through the flowgraph:

UpdatedFrequencyFlowgraph.png


Re-running the flowgraph shows that the frequency has been updated:

FrequencySinkUpdatedFrequency.png