Variables in Flowgraphs: Difference between revisions

From GNU Radio
Jump to navigation Jump to search
No edit summary
(Try to clarify the role of GRC in creating Python code since most people use GRC instead of writing Python flowgraphs from scratch)
 
(15 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Template:TutorialNavigation}}
<div style="float:right">
{{Template:BeginnerTutorials}}
</div>
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.


This tutorial describes how to use variables in a flowgraph. The flowgraph from the previous guide ([[Your_First_Flowgraph|Your First Flowgraph]]) will be used as a starting point for this tutorial, so please complete it 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==
==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.
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:
Every new flowgraph starts with the ''samp_rate'' variable:
Line 12: Line 19:




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:
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:


[[File:NewVariableBlock.png|800px]]
[[File:NewVariableBlock.png|800px]]




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


[[File:VariableProperties.png|500px]]
[[File:VariableProperties.png|500px]]




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''.
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''.


[[File:FrequencyVariable.png|500px]]
[[File:FrequencyVariable.png|500px]]
Line 28: Line 35:
Click ''OK'' to save.
Click ''OK'' to save.


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


[[File:SignalSourceProperties.png|500px]]
[[File:SignalSourceProperties.png|500px]]




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:
The ''Frequency'' is set to ''1000''. Enter ''frequency'' into the Frequency field to use the variable:


[[File:SignalSourceFrequency.png|500px]]
[[File:SignalSourceFrequency.png|500px]]




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:
Click ''OK'' to save the properties. The ''frequency'' variable and the value within the ''Signal Source'' block are updated:


[[File:FlowgraphWithFrequencyVariable.png|600px]]
[[File:FlowgraphWithFrequencyVariable.png|600px]]




Re-run the flowgraph:
Run the flowgraph:


[[File:FlowgraphNewFrequencyOutput.png|800px]]
[[File:FlowgraphNewFrequencyOutput.png|800px]]




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


==Dependent Variables==
==Dependent Variables==


Variables can be dependent on one another in the same way variables in Python can be derived from other variables.
Variables can be dependent on one another. The ''Id'' and ''Value'' fields are converted into a line of Python in the following manner:
 
The ''Id'' and ''Value'' fields are converted into a line of Python in the following manner:


<pre>Id = Value</pre>
<pre>Id = Value</pre>


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


<pre>frequency = 4000</pre>
<pre>frequency = 4000</pre>


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''.
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''.


[[File:ExampleDependentVariable.png|500px]]
[[File:ExampleDependentVariable.png|500px]]




The change will now ripple through the flowgraph:
The change is displayed in the flowgraph:


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




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


[[File:FrequencySinkUpdatedFrequency.png|800px]]
[[File:FrequencySinkUpdatedFrequency.png|800px]]




The next section will describe how to [[Runtime_Updating_Variables|update variables in real time]] while the flowgraph is running.
The next tutorial, [[Runtime_Updating_Variables|Runtime Updating Variables]], demonstrates how variables are updated while a flowgraph is running.

Latest revision as of 15:21, 27 July 2022

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
  4. Frequency Shifting
  5. Reading and Writing Binary Files

SDR Hardware

  1. RTL-SDR FM Receiver
  2. B200-B205mini FM Receiver

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

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:

VariableSampRate.png


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:

NewVariableBlock.png


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

VariableProperties.png


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.

FrequencyVariable.png

Click OK to save.

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

SignalSourceProperties.png


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

SignalSourceFrequency.png


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

FlowgraphWithFrequencyVariable.png


Run the flowgraph:

FlowgraphNewFrequencyOutput.png


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

Dependent Variables

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.

ExampleDependentVariable.png


The change is displayed in the flowgraph:

UpdatedFrequencyFlowgraph.png


Running the flowgraph shows the frequency is updated:

FrequencySinkUpdatedFrequency.png


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