Importing Libraries: Difference between revisions

From GNU Radio
Jump to navigation Jump to search
(Created page with "The import block enables calls to Python-based libraries such as NumPy within GRC, allowing for more sophistication in the use and creation of variables and parameters for blocks. ==Import Block== Drag the Import block into the flowgraph. File:Importing_libraries_import_block.png")
 
 
(6 intermediate revisions by the same user not shown)
Line 3: Line 3:


==Import Block==
==Import Block==
Drag the Import block into the flowgraph.
Drag the '''Import''' block into the flowgraph.


[[File:Importing_libraries_import_block.png]]
Double-click the '''Import''' block to bring up the properties. The ''Import'' field receives any legal Python import statement. For this example, the Numpy library is imported as ''np''.
[[File:Importing_libraries_import_statement.png]]
The '''Import''' block then displays the imported library as ''np''.
[[File:Importing_libraries_after_numpy_import.png]]
Running the flowgraph generates a Python flowgraph of the same name, ending in .py. Opening the .py file displays the import statement:
[[File:Importing_libraries_numpy_import_in_python.png]]
==Variables Using Libraries==
Variables and parameters in GRC can now use the NumPy library directly, both for the creation and manipulation of values. For example, the ''sequentialArray'' variable is created using NumPy’s arange() function:
[[File:Importing_libraries_sequential_array_variable.png]]
The ''sequentialArray'' variable is then manipulated by the ''reversedArray'' variable, reversing the order of the array:
[[File:Importing_libraries_reversed_array_variable.png]]


[[File:Importing_libraries_import_block.png]]
 
==Randomized Variables==
NumPy’s random number generators can be used to randomize values within a flowgraph. Create a variable named ''randomAmplitude'' and assign the value using a uniform random variable from 0.1 to 1:
 
[[File:Importing_libraries_random_amplitude_variable.png]]
 
 
Add a Signal Source to the flowgraph and use the randomAmplitude variable as the Amplitude:
 
[[File:Importing_libraries_signal_source_random_amplitude.png]]
 
 
Connect the '''Signal Source''' to a '''QT GUI Time Sink'''. Note that the ''randomAmplitude'' value is different between the '''Variable''' and '''Signal Source''' blocks. This difference in values is because GRC evaluates the variable once the block property window is closed, and since the two windows were closed at different times it evaluated to two different values.
 
[[File:Importing_libraries_random_amplitude_flowgraph.png]]
 
 
However, this is not reflected in the underlying Python code. Instead, the variable text is unevaluated until run-time, allowing for proper randomization.
 
[[File:Importing_libraries_random_amplitude_in_python.png]]
 
 
Running the flowgraph demonstrates that the amplitude is randomized. The following two '''QT GUI Time Sink''' plots shows two different amplitude values for the '''Signal Source''' output.
 
[[File:Importing_libraries_random_amplitude_example_1.png]]
 
[[File:Importing_libraries_random_amplitude_example_2.png]]

Latest revision as of 21:43, 10 April 2024

The import block enables calls to Python-based libraries such as NumPy within GRC, allowing for more sophistication in the use and creation of variables and parameters for blocks.


Import Block

Drag the Import block into the flowgraph.

Importing libraries import block.png


Double-click the Import block to bring up the properties. The Import field receives any legal Python import statement. For this example, the Numpy library is imported as np.

Importing libraries import statement.png


The Import block then displays the imported library as np.

Importing libraries after numpy import.png


Running the flowgraph generates a Python flowgraph of the same name, ending in .py. Opening the .py file displays the import statement:

Importing libraries numpy import in python.png

Variables Using Libraries

Variables and parameters in GRC can now use the NumPy library directly, both for the creation and manipulation of values. For example, the sequentialArray variable is created using NumPy’s arange() function:

Importing libraries sequential array variable.png


The sequentialArray variable is then manipulated by the reversedArray variable, reversing the order of the array:

Importing libraries reversed array variable.png


Randomized Variables

NumPy’s random number generators can be used to randomize values within a flowgraph. Create a variable named randomAmplitude and assign the value using a uniform random variable from 0.1 to 1:

Importing libraries random amplitude variable.png


Add a Signal Source to the flowgraph and use the randomAmplitude variable as the Amplitude:

Importing libraries signal source random amplitude.png


Connect the Signal Source to a QT GUI Time Sink. Note that the randomAmplitude value is different between the Variable and Signal Source blocks. This difference in values is because GRC evaluates the variable once the block property window is closed, and since the two windows were closed at different times it evaluated to two different values.

Importing libraries random amplitude flowgraph.png


However, this is not reflected in the underlying Python code. Instead, the variable text is unevaluated until run-time, allowing for proper randomization.

Importing libraries random amplitude in python.png


Running the flowgraph demonstrates that the amplitude is randomized. The following two QT GUI Time Sink plots shows two different amplitude values for the Signal Source output.

Importing libraries random amplitude example 1.png

Importing libraries random amplitude example 2.png