Python Block with Vectors: Difference between revisions
Mattcarrick (talk | contribs) |
|||
(70 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
{{ | <div style="float:right"> | ||
{{Template:BeginnerTutorials}} | |||
</div> | |||
This tutorial describes how the ''Python Embedded Block'' can be modified to accept vector inputs and outputs, and how the ''input_items'' vector indexing is different between vectors and streams. | This tutorial describes how the ''Python Embedded Block'' can be modified to accept vector inputs and outputs, and how the ''input_items'' vector indexing is different between vectors and streams. | ||
Line 7: | Line 8: | ||
== Starting the Flowgraph == | == Starting the Flowgraph == | ||
This tutorial uses vectors | This tutorial uses vectors. Please complete the [[Streams_and_Vectors|Streams and Vectors]] tutorial before moving on. | ||
Add the | Add the blocks to the flowgraph: | ||
* Signal Source | * Signal Source | ||
* Throttle | * Throttle | ||
Line 15: | Line 16: | ||
* Embedded Python Block | * Embedded Python Block | ||
* Vector to Stream | * Vector to Stream | ||
* QT GUI Time Sink | * QT GUI Time Sink | ||
* Virtual Sink | |||
* Virtual Source | |||
* Variable | * Variable | ||
Change the block properties: | |||
* Signal Source | * Signal Source | ||
** Output Type: float | |||
** Frequency: 100 | |||
* Variable | * Variable | ||
** Id: vectorLength | ** Id: vectorLength | ||
Line 25: | Line 30: | ||
* Stream to Vector, Num Items: vectorLength | * Stream to Vector, Num Items: vectorLength | ||
* Vector to Stream, Num Items: vectorLength | * Vector to Stream, Num Items: vectorLength | ||
* QT GUI Time Sink | * Virtual Sink, Stream Id: sinusoid | ||
* Virtual Source, Stream Id: sinusoid | |||
* QT GUI Time Sink | |||
** Autoscale: Yes | |||
** Number of Inputs: 2 | |||
Connect the blocks according to the following flowgraph: | Connect the blocks according to the following flowgraph: | ||
[[File:PythonVectorStartingFlowgraph.png| | [[File:PythonVectorStartingFlowgraph.png|800px]] | ||
== Accepting Vector Inputs and Outputs == | == Accepting Vector Inputs and Outputs == | ||
The ''Embedded Python Block'' needs to be modified to accept vector inputs | The ''Embedded Python Block'' needs to be modified to: | ||
* accept vector inputs | |||
* produce vector outputs | |||
* change the data types to ''float'' | |||
With respect to the [[Streams_and_Vectors|Streams and Vectors]] tutorial, from a higher-level point of view, ''Streams'' are typically just a special case of ''Vectors'', ones that have just one data element in parallel instead of multiple. This is why we can just change the <code>in_sig</code> and <code>out_sig</code> parameters to use vectors rather than streams. | |||
Since the case of streams is so common it has its own simple syntax, whereas the syntax for vectors is just a bit more complex. In particular, when using vectors, we must specify the vector's length along with the data type of the elements in the vectors. We do so using a tuple in Python. | |||
Double-click on the "Embedded Python Block" to edit the source code. | |||
Change ''example_param'' in the function definition to ''vectorSize'': | Change ''example_param'' in the function definition to ''vectorSize'': | ||
Line 41: | Line 59: | ||
<pre>name='Max Hold Block',</pre> | <pre>name='Max Hold Block',</pre> | ||
Define the input and output | Define the input and output vector signals using a tuple: | ||
<pre>in_sig=[(np.float32,vectorSize)], | <pre>in_sig=[(np.float32,vectorSize)], | ||
out_sig=[(np.float32,vectorSize)]</pre> | out_sig=[(np.float32,vectorSize)]</pre> | ||
Line 50: | Line 68: | ||
<pre>output_items[0][:] = input_items[0]</pre> | <pre>output_items[0][:] = input_items[0]</pre> | ||
The code should now look like the following: | |||
[[File:PythonVectorDefineBlock.png|800px]] | |||
Save. Connect the ''Max Hold Block'' to the rest of the flowgraph: | |||
[[File:PythonVectorConnectMaxHold.png|800px]] | |||
== Warning for Vector Length Mismatches == | |||
The ''Embedded Python Block'' has one difference that other [[OutOfTreeModules|Out of Tree Modules]] do not have. Before a flowgraph can be run, GRC checks to ensure all of the connected data types and vector sizes match. During this process the default value of ''vectorSize'' in the ''__init__()'' function, | |||
<pre>def __init__(self, vectorSize=16):</pre> | |||
is used to define the size of the vectors for the input and output, | |||
<pre>in_sig=[(np.float32,vectorSize)], | |||
out_sig=[(np.float32,vectorSize)]</pre> | |||
when determining if the flowgraph is correct. In this case ''vectorSize=16''. GRC assumes the input and output ports are vectors with length 16, <u>even if a different parameter is passed in through the block properties!</u>. The following image shows how a vector size of 128 is passed in as a parameter, which GRC does not catch as an error, but the flowgraph will crash once it's run: | |||
[[File:MismatchVectorSizeOption1.png|800px]] | |||
<pre>Traceback (most recent call last): | |||
File "/home/username/vectorinput.py", line 250, in <module> | |||
main() | |||
File "/home/username/vectorinput.py", line 226, in main | |||
tb = top_block_cls() | |||
File "/home/username/vectorinput.py", line 188, in __init__ | |||
self.connect((self.blocks_stream_to_vector_0, 0), (self.epy_block_0, 0)) | |||
File "/usr/lib/python3/dist-packages/gnuradio/gr/hier_block2.py", line 48, in wrapped | |||
func(self, src, src_port, dst, dst_port) | |||
File "/usr/lib/python3/dist-packages/gnuradio/gr/hier_block2.py", line 111, in connect | |||
self.primitive_connect(*args) | |||
File "/usr/lib/python3/dist-packages/gnuradio/gr/runtime_swig.py", line 4531, in primitive_connect | |||
return _runtime_swig.top_block_sptr_primitive_connect(self, *args) | |||
RuntimeError: itemsize mismatch: stream_to_vector0:0 using 64, Embedded Python Block0:0 using 512</pre> | |||
Alternatively, GRC will show an error if the default parameter does not match the vector size of the other blocks. In this case, the default vector length in the code is 128 but the passed-in parameter is 16: | |||
[[File:MismatchVectorSizeOption2.png|800px]] | |||
== Indexing Streams == | |||
For a stream, inputs and outputs can be indexed using both the port number and the sample index. | |||
Indexing based on the port number returns all of the input samples for a specific port. For example, | |||
<pre>input_items[0]</pre> | |||
returns all input samples on port 0. | |||
The following line returns the 4th input sample on port 0: | |||
<pre>input_items[0][3]</pre> | |||
The indexing for streams is generalized to: | |||
<pre>input_items[portIndex][sampleIndex] | |||
output_items[portIndex][sampleIndex]</pre> | |||
The image shows how to visualize indexing streams: | |||
[[File:IndexingStreams.png|500px]] | |||
== Indexing Vectors == | |||
The input ''input_items'' and output ''output_items'' include an extra dimension when using vectors. | |||
Vectors add an additional dimension, represented as ''vectorIndex'' below. The ''input_items'' and ''output_items'' are now three-dimensional arrays: | |||
<pre>input_items[portIndex][vectorIndex][sampleIndex] | |||
output_items[portIndex][vectorIndex][sampleIndex]</pre> | |||
Indexing based on the ''portIndex'' returns a two-dimensional array of all vectors and samples, for example: | |||
<pre>input_items[portIndex] | |||
output_items[portIndex]</pre> | |||
Indexing based on ''portIndex'' and ''vectorIndex'' returns an single-dimensional array of samples, for example: | |||
<pre>input_items[portIndex][vectorIndex] | |||
output_items[portIndex][vectorIndex]</pre> | |||
Indexing based on ''portIndex'', ''vectorIndex'' and ''sampleIndex'' returns a single sample. | |||
<pre>input_items[portIndex][vectorIndex][sampleIndex] | |||
output_items[portIndex][vectorIndex][sampleIndex]</pre> | |||
A visual example vector indexing is given below: | |||
[[File:VectorIndexing.png|700px]] | |||
== Creating Max Hold Function == | |||
The ''work()'' function is modified to include the max hold function. Add a loop over all of the vectors in ''input_items[0]'': | |||
<pre>for vectorIndex in range(len(input_items[0])):</pre> | |||
Calculate the max value of the vector: | |||
<pre>maxValue = np.max(input_items[0][vectorIndex])</pre> | |||
Loop over each of the input samples: | |||
<pre>for sampleIndex in range(len(input_items[0][vectorIndex])):</pre> | |||
Assign each output sample ''maxValue'': | |||
<pre>output_items[0][vectorIndex][sampleIndex] = maxValue</pre> | |||
The code should look like the following: | |||
[[File:MaxHoldWorkFunction.png|700px]] | |||
Save the code (CTRL + S). Run the flowgraph. The output will shows a sinusoid and a sinusoid with a max-hold applied every 16 samples: | |||
[[File:MaxHoldOutput.png|600px]] | |||
== Multiple Vector Ports == | |||
The ''Max Hold Block'' is modified to add a second vector input and output port. | |||
Add the following blocks to the workspace: | |||
* Noise Source | |||
* Stream to Vector | |||
* Vector to Stream | |||
* Virtual Sink | |||
* Virtual Source | |||
* QT GUI Time Sink | |||
Change the following block properties: | |||
* Noise Source, Output Type: float | |||
* Stream to Vector, Num Items: vectorLength | |||
* Vector to Stream, Num Items: vectorLength | |||
* Virtual Sink, Stream Id: noise | |||
* Virtual Source, Stream Id: noise | |||
* QT GUI Time Sink | |||
** Autoscale: Yes | |||
** Number of Inputs: 2 | |||
Connect the blocks: | |||
[[File:PythonVectorNoiseSource.png|800px]] | |||
Edit the code for the ''Max Hold Block''. Add a second vector input and output: | |||
<pre>in_sig=[(np.float32,vectorSize),(np.float32,vectorSize)], | |||
out_sig=[(np.float32,vectorSize),(np.float32,vectorSize)]</pre> | |||
[[File:PythonBlockSecondVector.png|700px]] | |||
The ''work()'' function is modified to perform the max hold function over both input ports. | |||
Include an outer loop over all input ports: | |||
<pre>for portIndex in range(len(input_items)):</pre> | |||
Change all indexing ''[0]'' to ''[portIndex]'': | |||
<pre>for portIndex in range(len(input_items)): | |||
for vectorIndex in range(len(input_items[portIndex])): | |||
maxValue = np.max(input_items[portIndex][vectorIndex]) | |||
for sampleIndex in range(len(input_items[portIndex][vectorIndex])): | |||
output_items[portIndex][vectorIndex][sampleIndex] = maxValue</pre> | |||
The code should now look like the following: | |||
[[File:PythonVectorFinalCode.png|700px]] | |||
Save the code and connect the blocks: | |||
[[File:PythonVectorFinalFlowgraph.png|800px]] | |||
Run the flowgraph. Two max-hold outputs will now be generated, one for the noise source and one for the sinusoid: | |||
[[File: | [[File:TwoMaxHoldOutputs.png|700px]] | ||
The next tutorial, [[Python_Block_Message_Passing|Python Block Message Passing]] describes how to send and receive messages using the ''Embedded Python Block''. |
Latest revision as of 12:45, 27 July 2024
Beginner Tutorials
Introducing GNU Radio Flowgraph Fundamentals
Creating and Modifying Python Blocks DSP Blocks
SDR Hardware |
This tutorial describes how the Python Embedded Block can be modified to accept vector inputs and outputs, and how the input_items vector indexing is different between vectors and streams.
The previous tutorial, Creating Your First Block, demonstrates how to create a Python block using the Embedded Python Block. The next tutorial, Python Block Message Passing describes how to send and receive messages using the Embedded Python Block.
Starting the Flowgraph
This tutorial uses vectors. Please complete the Streams and Vectors tutorial before moving on.
Add the blocks to the flowgraph:
- Signal Source
- Throttle
- Stream to Vector
- Embedded Python Block
- Vector to Stream
- QT GUI Time Sink
- Virtual Sink
- Virtual Source
- Variable
Change the block properties:
- Signal Source
- Output Type: float
- Frequency: 100
- Variable
- Id: vectorLength
- Value: 16
- Stream to Vector, Num Items: vectorLength
- Vector to Stream, Num Items: vectorLength
- Virtual Sink, Stream Id: sinusoid
- Virtual Source, Stream Id: sinusoid
- QT GUI Time Sink
- Autoscale: Yes
- Number of Inputs: 2
Connect the blocks according to the following flowgraph:
Accepting Vector Inputs and Outputs
The Embedded Python Block needs to be modified to:
- accept vector inputs
- produce vector outputs
- change the data types to float
With respect to the Streams and Vectors tutorial, from a higher-level point of view, Streams are typically just a special case of Vectors, ones that have just one data element in parallel instead of multiple. This is why we can just change the in_sig
and out_sig
parameters to use vectors rather than streams.
Since the case of streams is so common it has its own simple syntax, whereas the syntax for vectors is just a bit more complex. In particular, when using vectors, we must specify the vector's length along with the data type of the elements in the vectors. We do so using a tuple in Python.
Double-click on the "Embedded Python Block" to edit the source code.
Change example_param in the function definition to vectorSize:
def __init__(self, vectorSize=16):
Change the name:
name='Max Hold Block',
Define the input and output vector signals using a tuple:
in_sig=[(np.float32,vectorSize)], out_sig=[(np.float32,vectorSize)]
Remove the self.example_param = example_param line.
Remove the multiplication by self.example_param:
output_items[0][:] = input_items[0]
The code should now look like the following:
Save. Connect the Max Hold Block to the rest of the flowgraph:
Warning for Vector Length Mismatches
The Embedded Python Block has one difference that other Out of Tree Modules do not have. Before a flowgraph can be run, GRC checks to ensure all of the connected data types and vector sizes match. During this process the default value of vectorSize in the __init__() function,
def __init__(self, vectorSize=16):
is used to define the size of the vectors for the input and output,
in_sig=[(np.float32,vectorSize)], out_sig=[(np.float32,vectorSize)]
when determining if the flowgraph is correct. In this case vectorSize=16. GRC assumes the input and output ports are vectors with length 16, even if a different parameter is passed in through the block properties!. The following image shows how a vector size of 128 is passed in as a parameter, which GRC does not catch as an error, but the flowgraph will crash once it's run:
Traceback (most recent call last): File "/home/username/vectorinput.py", line 250, in <module> main() File "/home/username/vectorinput.py", line 226, in main tb = top_block_cls() File "/home/username/vectorinput.py", line 188, in __init__ self.connect((self.blocks_stream_to_vector_0, 0), (self.epy_block_0, 0)) File "/usr/lib/python3/dist-packages/gnuradio/gr/hier_block2.py", line 48, in wrapped func(self, src, src_port, dst, dst_port) File "/usr/lib/python3/dist-packages/gnuradio/gr/hier_block2.py", line 111, in connect self.primitive_connect(*args) File "/usr/lib/python3/dist-packages/gnuradio/gr/runtime_swig.py", line 4531, in primitive_connect return _runtime_swig.top_block_sptr_primitive_connect(self, *args) RuntimeError: itemsize mismatch: stream_to_vector0:0 using 64, Embedded Python Block0:0 using 512
Alternatively, GRC will show an error if the default parameter does not match the vector size of the other blocks. In this case, the default vector length in the code is 128 but the passed-in parameter is 16:
Indexing Streams
For a stream, inputs and outputs can be indexed using both the port number and the sample index.
Indexing based on the port number returns all of the input samples for a specific port. For example,
input_items[0]
returns all input samples on port 0.
The following line returns the 4th input sample on port 0:
input_items[0][3]
The indexing for streams is generalized to:
input_items[portIndex][sampleIndex] output_items[portIndex][sampleIndex]
The image shows how to visualize indexing streams:
Indexing Vectors
The input input_items and output output_items include an extra dimension when using vectors.
Vectors add an additional dimension, represented as vectorIndex below. The input_items and output_items are now three-dimensional arrays:
input_items[portIndex][vectorIndex][sampleIndex] output_items[portIndex][vectorIndex][sampleIndex]
Indexing based on the portIndex returns a two-dimensional array of all vectors and samples, for example:
input_items[portIndex] output_items[portIndex]
Indexing based on portIndex and vectorIndex returns an single-dimensional array of samples, for example:
input_items[portIndex][vectorIndex] output_items[portIndex][vectorIndex]
Indexing based on portIndex, vectorIndex and sampleIndex returns a single sample.
input_items[portIndex][vectorIndex][sampleIndex] output_items[portIndex][vectorIndex][sampleIndex]
A visual example vector indexing is given below:
Creating Max Hold Function
The work() function is modified to include the max hold function. Add a loop over all of the vectors in input_items[0]:
for vectorIndex in range(len(input_items[0])):
Calculate the max value of the vector:
maxValue = np.max(input_items[0][vectorIndex])
Loop over each of the input samples:
for sampleIndex in range(len(input_items[0][vectorIndex])):
Assign each output sample maxValue:
output_items[0][vectorIndex][sampleIndex] = maxValue
The code should look like the following:
Save the code (CTRL + S). Run the flowgraph. The output will shows a sinusoid and a sinusoid with a max-hold applied every 16 samples:
Multiple Vector Ports
The Max Hold Block is modified to add a second vector input and output port.
Add the following blocks to the workspace:
- Noise Source
- Stream to Vector
- Vector to Stream
- Virtual Sink
- Virtual Source
- QT GUI Time Sink
Change the following block properties:
- Noise Source, Output Type: float
- Stream to Vector, Num Items: vectorLength
- Vector to Stream, Num Items: vectorLength
- Virtual Sink, Stream Id: noise
- Virtual Source, Stream Id: noise
- QT GUI Time Sink
- Autoscale: Yes
- Number of Inputs: 2
Connect the blocks:
Edit the code for the Max Hold Block. Add a second vector input and output:
in_sig=[(np.float32,vectorSize),(np.float32,vectorSize)], out_sig=[(np.float32,vectorSize),(np.float32,vectorSize)]
The work() function is modified to perform the max hold function over both input ports.
Include an outer loop over all input ports:
for portIndex in range(len(input_items)):
Change all indexing [0] to [portIndex]:
for portIndex in range(len(input_items)): for vectorIndex in range(len(input_items[portIndex])): maxValue = np.max(input_items[portIndex][vectorIndex]) for sampleIndex in range(len(input_items[portIndex][vectorIndex])): output_items[portIndex][vectorIndex][sampleIndex] = maxValue
The code should now look like the following:
Save the code and connect the blocks:
Run the flowgraph. Two max-hold outputs will now be generated, one for the noise source and one for the sinusoid:
The next tutorial, Python Block Message Passing describes how to send and receive messages using the Embedded Python Block.