Friday, January 3, 2020

Python Block to selectively pass signals

"""
Embedded Python Blocks:

Each time this file is saved, GRC will instantiate the first class it finds
to get ports and parameters of your block. The arguments to __init__ will
be the parameters. All of them are required to have default values!
"""

import numpy as np
from gnuradio import gr


class blk(gr.block): # other base classes are basic_block, decim_block, interp_block
"""Embedded Python Block example - a simple multiply const"""

def __init__(self, threshold=0.5): # only default arguments here
"""arguments to this function show up as parameters in GRC"""
gr.block.__init__(
self,
name='Output after Detect 1', # will show up in GRC
in_sig=[np.float32],
out_sig=[np.float32]
)
# if an attribute with the same name as a parameter is found,
# a callback is registered (properties work, too).
self.threshold = threshold
self.state=0

def work(self, input_items, output_items):
"""example: multiply with constant"""
avg_inp = np.mean(input_items[0])
if avg_inp>self.threshold and self.state==0:
print("detected")
self.state = 1

if self.state:
output_items[0][:] = input_items[0]
else:
output_items[0][:] = np.array([])

return len(output_items[0])

Hi,
I am trying to make a python block that drops the frames that don't have the presence of a signal but forwards if there is. I tried using "basic block" but always got the error "Param - Code(_source_code): No python block class found in code". Please check the attachments for the code I wrote.

Please help me find the mistake I am doing.

Thanks!

No comments:

Post a Comment