简体   繁体   中英

Variable number of inputs and outputs in Python Block?

I am building a GNURadio block in Python that has a variable number of input, So I can change the number of input in each simulation. I use self.k=k to get the number of input as:

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)],

However, this works only when I change the value of k inside the python code

def __init__(self, k=3):  

or

def __init__(self, k=2 ) ....

How do I make it change automatically from the block embedded itself without change the codes?

""" 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.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])

In your code you gave the parameter ka default value in case it wasn't set. If you want the variable to be something else than the default value you will have to set it when calling the method.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM