Python Blocks
Every block so far came off the shelf.
This chapter writes one: a small class
in plain Python that takes complex
samples in and puts their power —
I² + Q², chapter 2's strength squared —
out, one float per sample. It runs it
in a real flowgraph and checks its
output to the last decimal. The code is
gnu-radio-lab/fg4_python_block.py;
the transcript is
gnu-radio-lab/transcripts/t04-python-block.txt.
Why write a block at all
Because the shelf, enormous as it is, ends. The signal you are chasing does a thing nobody packaged; the measurement you need is three lines of arithmetic wearing a trench coat; the prototype must exist by Friday. GNU Radio's answer is that a block is not a sacred object: it is a class with typed inputs, typed outputs, and one method that turns the former into the latter. Companion users meet the same thing as the embedded Python block; the lab writes it as a class in the flowgraph file, which is all an embedded block ever is.
The block, whole
class power_block(gr.sync_block):
def __init__(self):
gr.sync_block.__init__(
self,
name="power_block",
in_sig=[np.complex64],
out_sig=[np.float32],
)
def work(self, input_items, output_items):
samples = input_items[0]
out = output_items[0]
out[:] = samples.real ** 2 + samples.imag ** 2
return len(out)
Three ideas carry it:
- The signature is the contract.
in_sigandout_sigdeclare the stream types — one complex64 in, one float32 out — and the runtime holds both sides to it, exactly as the canvas's port colours do (chapter 5). - A sync block keeps the count.
gr.sync_blockis the family whose outputs march one-for-one with their inputs; decimating and interpolating blocks declare other ratios, and the general block declares none and keeps its own books — power and responsibility rising together down that list. workis handed arrays, not samples.input_items[0]is a NumPy array of however many samples the scheduler brought; the block fills the output array and returns how many it made. The vectorised line —samples.real ** 2 + samples.imag ** 2— is the whole computation, and writing it as array arithmetic rather than a Python loop is the difference between a block that keeps up and a block that becomes chapter 15's computer bill.
The proof
Fed a unit-amplitude tone — whose arrow has length exactly 1 at every instant, so I² + Q² must be exactly 1 — for 48,000 samples, the output file measured:
samples: 48000
value: mean 1.0000, max 1.0000, min 1.0000
Not 0.9998, not a peak near one: every one of 48,000 outputs is 1.0000 to the four decimals the measurement prints. A block is verified the same way a flowgraph is — by an expectation stated in advance that the output either keeps or breaks.
The honest limits of Python here
Python blocks are the fastest way to a working block and among the slowest ways to run one: the interpreter's overhead rides on every call, and a naive Python block in a wide, fast chain is an overrun looking for somewhere to happen (chapters 15 and 16). The working pattern of the whole ecosystem follows: prototype in Python, measure, and only rebuild in C++ (an out-of-tree module) when the measurement says the prototype is the bottleneck — which, at the rates most mere-mortal work runs at, it honestly may never be. The lab's receiver chain of chapter 11 runs at rates a Python block could join without disgrace; a 30-million-sample wideband chain is a different country, and chapter 15 has its prices posted.
Check: modify the block to output
the arrow's length rather than its
square (np.abs(samples) does it in
one call), and predict the measurement
before you run: mean, max and min all
still 1.0000 — the same claim, reached
by the other road. Then feed it
chapter 9's two-tone sum (amplitudes
0.5 and 0.5) and predict honestly:
no longer constant, and working out
why — the arrows adding and
cancelling as they turn at different
speeds — is chapter 2 paying its
final dividend.