Python Variables in GRC: Difference between revisions
No edit summary |
m (Use python syntax highlighting) |
||
Line 9: | Line 9: | ||
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 type are numbers. Numbers in Python can be floating point or integers: | ||
< | <syntaxhighlight lang="python"> | ||
integerNumber = 2</ | floatNumber = 3.14 | ||
integerNumber = 2 | |||
</syntaxhighlight> | |||
Integers can be converted to floating point by casting using ''float()'', and floating point numbers can be converted to integers using ''int()'': | Integers can be converted to floating point by casting using ''float()'', and floating point numbers can be converted to integers using ''int()'': | ||
< | <syntaxhighlight lang="python"> | ||
integerNumber = int(3.14)</ | floatNumber = float(2) | ||
integerNumber = int(3.14) | |||
</syntaxhighlight> | |||
Type conversion can be done within the variable blocks: | Type conversion can be done within the variable blocks: | ||
Line 55: | Line 59: | ||
Python uses both single quotes ' and double quotes " to contain strings: | Python uses both single quotes ' and double quotes " to contain strings: | ||
< | <syntaxhighlight lang="python"> | ||
doubleQuoteString = "string2"</ | singleQuoteString = 'string1' | ||
doubleQuoteString = "string2" | |||
</syntaxhighlight> | |||
Strings can be used as variables in GRC: | Strings can be used as variables in GRC: | ||
Line 96: | Line 102: | ||
[https://www.w3schools.com/python/python_lists_comprehension.asp 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: | [https://www.w3schools.com/python/python_lists_comprehension.asp 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: | ||
< | <syntaxhighlight lang="python"> | ||
listComprehensionExample = [(i+1)*2 for i in listVariable]</ | listVariable = [0, 1, 2, 3] | ||
listComprehensionExample = [(i+1)*2 for i in listVariable] | |||
</syntaxhighlight> | |||
This is accomplished in GNU Radio by using two variables, ''listVariable'' and ''listComprehensionExample'', and entering their associated ''values'': | This is accomplished in GNU Radio by using two variables, ''listVariable'' and ''listComprehensionExample'', and entering their associated ''values'': |
Revision as of 10:45, 8 February 2022
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 your first 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 type are 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:
The value is then displayed as an integer:
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.
Double-click the samp_rate variable to edit the properties:
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
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
- Value: 0.25
GRC now displays the value 0.25 as 250m because it has been converted to SI units:
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 then displayed in GRC:
Lists and Tuples in GRC
Variables in GRC can use lists:
The list is then displayed in GRC:
Variables in GRC can use tuples:
The tuple is then displayed in GRC:
List Comprehension
Each Variable acts as 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 is accomplished in GNU Radio by using two variables, listVariable and listComprehensionExample, and entering their associated values:
The lists are displayed in GRC:
The next tutorial, Variables in Flowgraphs, describes how to use and modify variables in a more sophisticated flowgraph.