Python Variables in GRC: Difference between revisions
Mattcarrick (talk | contribs) (→Floats and Integers in GRC: making easier to read for ESL) |
|||
(13 intermediate revisions by one other user not shown) | |||
Line 50: | Line 50: | ||
GRC now displays the value ''0.25'' as ''250m'' because it has been converted to SI units: | GRC now displays the value ''0.25'' as ''250m'' because it has been converted to SI units (milli-) : | ||
[[File:FloatNumberVariable.png|250px]] | [[File:FloatNumberVariable.png|250px]] | ||
Line 73: | Line 73: | ||
== Lists and Tuples in GRC == | == Lists and Tuples in GRC == | ||
Variables in GRC can use lists: | Variables in GRC can use Python lists: | ||
[[File:ListProperties.png|500px]] | [[File:ListProperties.png|500px]] | ||
Line 83: | Line 83: | ||
Variables in GRC can use tuples: | Variables in GRC can use Python tuples: | ||
[[File:TupleProperties.png|500px]] | [[File:TupleProperties.png|500px]] | ||
Line 91: | Line 91: | ||
[[File:TupleVariable.png|300px]] | [[File:TupleVariable.png|300px]] | ||
== List Comprehension == | == List Comprehension == | ||
Line 99: | Line 98: | ||
''Id = Value'' | ''Id = Value'' | ||
[https://www.w3schools.com/python/python_lists_comprehension.asp List comprehension] can be used | [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> | <syntaxhighlight lang="python" line> | ||
listVariable = [0, 1, 2, 3] | listVariable = [0, 1, 2, 3] | ||
Line 115: | Line 114: | ||
[[File:ListComprehensionVariables.png]] | [[File: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: | |||
[[File:QTGUIFrequencySinkBlock.png]] | |||
[[File:QTGUIFrequencySinkPropertyColors.png|500px]] | |||
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 Flowgraph Fundamentals
Creating and Modifying Python Blocks DSP Blocks
SDR Hardware |
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:
The value is displayed as an integer:
GRC displays numbers differently than Python. For example, the samp_rate block is added to every new flowgraph.
Double-click the samp_rate variable to edit the properties:
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
GRC now displays the value 0.25 as 250m because it has been converted to SI units (milli-) :
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:
The string is displayed in GRC:
Lists and Tuples in GRC
Variables in GRC can use Python lists:
The list is displayed in GRC:
Variables in GRC can use Python tuples:
The tuple is displayed in GRC:
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:
The lists are displayed in GRC:
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:
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.