Python Block with Vectors: Difference between revisions

From GNU Radio
Jump to navigation Jump to search
Line 59: Line 59:
[[File:PythonVectorConnectMaxHold.png|800px]]
[[File:PythonVectorConnectMaxHold.png|800px]]


== Warning for Vector Length Mismatches ==


<u>Warning!</u> The flowgraph will show an error if the length of the vector in your block definition does not match the vector sizes of the other blocks! Since there is not a syntax error the ''Embedded Python Block'' will not report an error in the properties window. Instead the only indication of an error will be that the blocks will not connect:
<u>Warning!</u> The flowgraph will show an error if the length of the vector in your block definition does not match the vector sizes of the other blocks! Since there is not a syntax error the ''Embedded Python Block'' will not report an error in the properties window. Instead the only indication of an error will be that the blocks will not connect:


[[File:VectorSizeError.png|800px]]
[[File:VectorSizeError.png|800px]]

Revision as of 14:44, 31 January 2022

Template:TutorialNavigation

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 following blocks to the flowgraph:

  • Signal Source
  • Throttle
  • Stream to Vector
  • Embedded Python Block
  • Vector to Stream
  • QT GUI Time Sink (two copies)
  • Variable

Modify the following block properties:

  • Signal Source, Frequency: 100
  • Variable
    • Id: vectorLength
    • Value: 16
  • Stream to Vector, Num Items: vectorLength
  • Vector to Stream, Num Items: vectorLength
  • QT GUI Time Sink (both copies), Autoscale: Yes

Connect the blocks according to the following flowgraph:

PythonVectorStartingFlowgraph.png

Accepting Vector Inputs and Outputs

The Embedded Python Block needs to be modified to accept vector inputs, produce vector outputs and change the data type to float. Double-click on the 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 to be a tuple enveloped by a list:

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:

PythonVectorDefineBlock.png


Save the code (CTRL + S). Connect the Max Hold Block to the rest of the flowgraph:

PythonVectorConnectMaxHold.png

Warning for Vector Length Mismatches

Warning! The flowgraph will show an error if the length of the vector in your block definition does not match the vector sizes of the other blocks! Since there is not a syntax error the Embedded Python Block will not report an error in the properties window. Instead the only indication of an error will be that the blocks will not connect:

VectorSizeError.png