Python Variables in GRC

From GNU Radio
Revision as of 17:24, 14 March 2022 by Mattcarrick (talk | contribs) (→‎Floats and Integers in GRC: making easier to read for ESL)
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 Python data types are used in GRC and how the variables are displayed.

The previous tutorial, Your First Flowgraph, shows how to build a simple flowgraph. The next tutorial, Variables in Flowgraphs, describes how to use and modify variables in a more sophisticated flowgraph.

Floats and Integers in GRC

GNU Radio Companion (GRC) uses Python data types to represent variables. The simplest data types describe numbers. Numbers in Python can be floating point or integers:

floatNumber = 3.14
integerNumber = 2

Integers can be converted to floating point by casting using float(), and floating point numbers can be converted to integers using int():

floatNumber = float(2)
integerNumber = int(3.14)

Type conversion can be done within the variable blocks:

FloatToIntProperties.png


The value is displayed as an integer:

FloatToIntVariable.png


However, GRC displays numbers differently than Python. For example, the samp_rate block is added to every new flowgraph.

SampRateVariable.png

Double-click the samp_rate variable to edit the properties:

SampRateProperties.png

The value of samp_rate is 32000 yet GRC displays the value 32k. GRC converts all numbers into SI Units. Note that GRC may display a number in a different format than it is represented in Python.

For another example, drag and drop a variable into the workspace. Double-click to edit the properties:

  • Id: floatNumber
  • Value: 0.25

FloatNumberProperties.png


GRC now displays the value 0.25 as 250m because it has been converted to SI units:

FloatNumberVariable.png

Strings in GRC

Python uses both single quotes ' and double quotes " to contain strings:

singleQuoteString = 'string1'
doubleQuoteString = "string2"

Strings can be used as variables in GRC:

StringProperties.png


The string is displayed in GRC:

StringVariable.png

Lists and Tuples in GRC

Variables in GRC can use lists:

ListProperties.png


The list is displayed in GRC:

ListVariable.png


Variables in GRC can use tuples:

TupleProperties.png


The tuple is displayed in GRC:

TupleVariable.png


List Comprehension

Each Variable is a single line in Python:

Id = Value

List comprehension can be used as a way to implement a function in a Variable. For example, list comprehension is used to loop through a list, add +1 to all entries, and multiply the result by 2:

listVariable = [0, 1, 2, 3]
listComprehensionExample = [(i + 1) * 2 for i in listVariable]

This list comprehension example is used in GNU Radio by using two variables, listVariable and listComprehensionExample, and entering their associated values:

ListVariableProperties.png

ListComprehensionVariableProperties.png

The lists are displayed in GRC:

ListComprehensionVariables.png


The next tutorial, Variables in Flowgraphs, describes how to use and modify variables in a more sophisticated flowgraph.