Importing Libraries
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.
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.
The Import block then displays the imported library as np.
Running the flowgraph generates a Python flowgraph of the same name, ending in .py. Opening the .py file displays the import statement:
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:
The sequentialArray variable is then manipulated by the reversedArray variable, reversing the order of the array:
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:
Add a Signal Source to the flowgraph and use the randomAmplitude variable as the Amplitude:
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.