[英]Variable number of inputs and outputs in Python Block?
我正在 Python 中构建一个 GNURadio 块,它具有可变数量的输入,因此我可以在每次模拟中更改输入数量。 我使用 self.k=k 来获取输入的数量:
def __init__(self, k=4): # only default arguments here
"""arguments to this function show up as parameters in GRC"""
self.k=k ....
in_sig=[np.float32 for k in range(k)],
但是,这仅在我更改 python 代码中的 k 值时有效
def __init__(self, k=3):
或者
def __init__(self, k=2 ) ....
如何在不更改代码的情况下使其从嵌入的块中自动更改?
""" 嵌入 Python 块:
每次保存此文件时,GRC 都会实例化它找到的第一个 class 以获取您的块的端口和参数。 初始化的 arguments 将是参数。 所有这些都需要有默认值!
import numpy as np
from gnuradio import gr
class blk(gr.sync_block): # other base classes are basic_block, decim_block, interp_block
"""Embedded Python Block example - a simple multiply const"""
def __init__(self, example_param=1, Nr=4, Nt=4): # only default arguments here
"""arguments to this function show up as parameters in GRC"""
self.Nr = Nr
self.Nt = Nt
gr.sync_block.__init__(
self,
name='Embedded Python Block', # will show up in GRC
in_sig=[np.float32 for k in range(Nr)],
out_sig=[np.float32 for k in range(Nt)],
)
# if an attribute with the same name as a parameter is found,
# a callback is registered (properties work, too).
self.example_param = example_param
def work(self, input_items, output_items):
"""example: multiply with constant"""
output_items[0][:] = input_items[0] * self.example_param
output_items[1][:] = input_items[1] * self.example_param
return len(output_items[0])
在您的代码中,您为参数 ka 提供了默认值,以防未设置。 如果您希望变量不是默认值,则必须在调用方法时设置它。
class Radio():
def __init__(self, k=4): # only default arguments here
self.k=k
print("k equals: " + self.k)
Radio(k=6) #prints "k equals: 6"
Radio() #prints "k equals: 4"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.