Python Variables in GRC: Difference between revisions

From GNU Radio
Jump to navigation Jump to search
No edit summary
 
(57 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{TutorialNavigation}}
<div style="float:right">
 
{{Template:BeginnerTutorials}}
</div>
This tutorial describes how Python data types are used in GRC and how the variables are displayed.
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 your first simple flowgraph. The next tutorial, [[Variables in Flowgraphs]], describes how to use and modify variables in a more sophisticated flowgraph.
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 ==
== Floats and Integers in GRC ==


GNU Radio Companion (GRC) uses Python data types to represent variables. The simplest data type are numbers. Numbers in Python can be floating point or integers:
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:
<pre>floatNumber = 3.14
<syntaxhighlight lang="python">
integerNumber = 2</pre>
floatNumber = 3.14
integerNumber = 2
</syntaxhighlight>
 
Integers can be converted to floating point using ''float()'', and floating point numbers can be converted to integers using ''int()'':
 
<syntaxhighlight lang="python">
floatNumber = float(2)
integerNumber = int(3.14)
</syntaxhighlight>
 
Type conversion can be done within the variable blocks:
 
[[File:FloatToIntProperties.png|500px]]
 


Integers can be converted to floating point by casting using ''float()'', and floating point numbers can be converted to integers using ''int()'':
The value is displayed as an integer:


<pre>floatNumber = float(2)
[[File:FloatToIntVariable.png|200px]]
integerNumber = int(3.14)</pre>


However, GRC displays numbers in a different fashion than Python. For example, when you create a new flowgraph you will be given the ''samp_rate'' block.


[[File:SampRateVariable.png|350px]]
GRC displays numbers differently than Python. For example, the ''samp_rate'' block is added to every new flowgraph.


[[File:SampRateVariable.png|300px]]


Double-click the ''samp_rate'' variable to edit the properties:
Double-click the ''samp_rate'' variable to edit the properties:
Line 25: Line 39:
[[File:SampRateProperties.png|500px]]
[[File:SampRateProperties.png|500px]]


The value of ''samp_rate'' is ''32000'' yet GRC displays the value ''32k''. GRC converts all numbers into
[https://en.wikipedia.org/wiki/International_System_of_Units SI Units]. Note that GRC ''may'' display a number in a different format than it is represented in Python.


You'll notice that the value is ''32000'' yet GRC displays the value in the flowgraph as ''32k''. This is because GRC converts all numbers into
For another example, drag and drop a new variable into the workspace. Double-click to edit the properties:
[https://en.wikipedia.org/wiki/International_System_of_Units SI Units]. Note that how the number is entered into GRC ''may'' be different than how GRC displays the number.
 
For another example, drag and drop a variable into the workspace. Double-click to edit the properties:


* Id: ''floatNumber''
* Id: ''floatNumber''
* Value: ''0.25''
* Value: ''0.25''
[[File:FloatNumberProperties.png|250px]]


GRC now displays the value ''0.25'' as ''250m'' because it has been converted to a SI units:
[[File:FloatNumberProperties.png|500px]]
 


[[File:FloatNumberVariable.png|500px]]
GRC now displays the value ''0.25'' as ''250m'' because it has been converted to SI units (milli-) :


[[File:FloatNumberVariable.png|250px]]


== Strings in GRC ==
== Strings in GRC ==


Python uses both single quotes ' and double quotes " to contain strings:
Python uses both single quotes ' and double quotes " to contain strings:
<pre>singleQuoteString = 'string1'
<syntaxhighlight lang="python">
doubleQuoteString = "string2"</pre>
singleQuoteString = 'string1'
doubleQuoteString = "string2"
</syntaxhighlight>
 
Strings can be used as variables in GRC:
 
[[File:StringProperties.png|500px]]
 
 
The string is displayed in GRC:
 
[[File:StringVariable.png|250px]]
 
== Lists and Tuples in GRC ==
 
Variables in GRC can use Python lists:
 
[[File:ListProperties.png|500px]]
 
 
The list is displayed in GRC:
 
[[File:ListVariable.png|300px]]
 
 
Variables in GRC can use Python tuples:
 
[[File:TupleProperties.png|500px]]
 
 
The tuple is displayed in GRC:
 
[[File:TupleVariable.png|300px]]
 
== List Comprehension ==
 
Each ''Variable'' is a single line in Python:
 
''Id = Value''
 
[https://www.w3schools.com/python/python_lists_comprehension.asp List comprehension] can be used to write functions in a ''Variable''. For example, list comprehension is used to loop through a list, add +1 to all entries, and then multiply each entry by 2:
<syntaxhighlight lang="python" line>
listVariable = [0, 1, 2, 3]
listComprehensionExample = [(i + 1) * 2 for i in listVariable]
</syntaxhighlight>
 
This list comprehension example is used in GNU Radio by using two variables, ''listVariable'' and ''listComprehensionExample'', and entering their associated ''values'':
 
[[File:ListVariableProperties.png|500px]]


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


== Lists, Tuples and Arrays in GRC ==
The lists are displayed in GRC:


Variables in GRC can also use lists and tuples:
[[File:ListComprehensionVariables.png]]


<pre>integerList = [1, 2, 3, 4, 5]
== Property Colors in GNU Radio Companion ==
floatList = [


* reference filter section that uses tuples to hold filter weights
GRC uses a color scheme to represent data types when editing block properties. The properties for the ''QT GUI Frequency Sink'' block are as follows:
* lists
* arrays


== Using Python Types in GRC ==
[[File:QTGUIFrequencySinkBlock.png]]
* python data types accepted in variables


== How GRC Displays Variable Values ==
[[File:QTGUIFrequencySinkPropertyColors.png|500px]]
* 32,000 -> 32k
* 0.25 -> 250m


There are a variety of colors for the ''QT GUI Frequency Sink'' properties: '''<span style="color:orange">orange</span>''', '''<span style="color:MediumSeaGreen">green</span>''' and '''<span style="color:purple">purple</span>'''. Each color corresponds to a different data type:


* Floating Point: '''<span style="color:orange">orange</span>'''
* Integer: '''<span style="color:MediumSeaGreen">green</span>'''
* String: '''<span style="color:purple">purple</span>'''


For example, the ''bandwidth'' is '''<span style="color:orange">orange</span>''' because the bandwidth can be any floating point number. The ''FFT Size'' must be an integer so it is colored in '''<span style="color:MediumSeaGreen">green</span>'''. The ''Y Label'' is a string because it contains words used to describe the vertical axis of the plot so it is colored in '''<span style="color:purple">purple</span>'''.


The ''Variable'' blocks do not have a color because they can be used to represent any data type or object.


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

Latest revision as of 21:02, 23 October 2023

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


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 new 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 (milli-) :

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

ListProperties.png


The list is displayed in GRC:

ListVariable.png


Variables in GRC can use Python 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 to write functions in a Variable. For example, list comprehension is used to loop through a list, add +1 to all entries, and then multiply each entry 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

Property Colors in GNU Radio Companion

GRC uses a color scheme to represent data types when editing block properties. The properties for the QT GUI Frequency Sink block are as follows:

QTGUIFrequencySinkBlock.png

QTGUIFrequencySinkPropertyColors.png

There are a variety of colors for the QT GUI Frequency Sink properties: orange, green and purple. Each color corresponds to a different data type:

  • Floating Point: orange
  • Integer: green
  • String: purple

For example, the bandwidth is orange because the bandwidth can be any floating point number. The FFT Size must be an integer so it is colored in green. The Y Label is a string because it contains words used to describe the vertical axis of the plot so it is colored in purple.

The Variable blocks do not have a color because they can be used to represent any data type or object.

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