GnuRadio python插值块

时间:2016-10-21 17:57:49

标签: python gnuradio

我一直在Gnuradio tutorials工作,以了解如何制作块。

我正在尝试制作一个插值块(1个输入:2个输出),它返回输出值乘以两个输出流中的常量。我已经将它实现为一个类,multiply_out_fff()继承自interp_block

import numpy
from gnuradio import gr

class multiply_out_fff(gr.interp_block):
    """ 
    docstring for block multiply_out_fff
    """
    def __init__(self, multiple):
        gr.interp_block.__init__(self,
            name="multiply_out_fff",
            in_sig=[numpy.float32],
            out_sig=[numpy.float32], interp = 2)
        self.multiple = multiple


    def work(self, input_items, output_items):
        in0 = input_items[0]
        out = output_items[0]

        print("the data coming in: ", in0)
        print("in shape ",in0.shape)

        for i in range(0,len(out)/2):
          out[i] = in0[i] * self.multiple
        for i in range(len(out)/2,len(out)):
          out[i] = in0[i-len(out)] * self.multiple

        print("the data going out: ", out)
        print("out shape ", out.shape)

        return len(output_items[0])

我已为这个区块编写了一个测试,并设法让它通过,但不是我原先认为可行的方式

from gnuradio import gr, gr_unittest
from gnuradio import blocks
from multiply_out_fff import multiply_out_fff

class qa_multiply_out_fff (gr_unittest.TestCase):

    def setUp (self):
        self.tb = gr.top_block ()

    def tearDown (self):
        self.tb = None

    def test_001_t (self):
        # set up fg
        self.data = (0,1,-2,5.5)
        self.expected_result = (0,2,-4,11,0,2,-4,11)

        print("---------testing----------")
        print("test data: ", self.data)

        #make blocks
        self.src = blocks.vector_source_f(self.data)
        self.mult = multiply_out_fff(2)
        self.snk1 = blocks.vector_sink_f()
        self.snk2 = blocks.vector_sink_f()

        #make connections
        self.tb.connect((self.src,0),(self.mult,0))
        self.tb.connect((self.mult,0),(self.snk1,0))
        #self.tb.connect((self.mult,1),(self.snk2,0))
        self.tb.run ()

        self.result_data1 = self.snk1.data()
        #self.result_data2 = self.snk2.data()

        print("The output data: ", self.result_data1)
        print("--------test complete-------------\n")

        # check data
        self.assertFloatTuplesAlmostEqual(self.expected_result, self.result_data1)
        #self.assertFloatTuplesAlmostEqual(self.expected_result, self.result_data2)


if __name__ == '__main__':
    gr_unittest.run(qa_multiply_out_fff, "qa_multiply_out_fff.xml")

运行测试脚本给出:

 ~/gnuradio/gr-tutorial/python$ python qa_multiply_out_fff.py 
---------testing----------
('test data: ', (0, 1, -2, 5.5))
('the data coming in: ', array([ 0. ,  1. , -2. ,  5.5], dtype=float32))
('in shape ', (4,))
('the data going out: ', array([  0.,   2.,  -4.,  11.,   0.,   2.,  -4.,  11.], dtype=float32))
('out shape ', (8,))
('The output data: ', (0.0, 2.0, -4.0, 11.0, 0.0, 2.0, -4.0, 11.0))
--------test complete-------------

.
----------------------------------------------------------------------
Ran 1 test in 0.002s

OK

我努力理解的概念是我的块如何返回数据。现在,它在单个数组中返回两个插值输出流,其长度是输入的两倍。我认为它应该有两个独立的输出数组,并允许我为每个输出连接一个单独的接收器,如下所示(在测试中注释掉):

    self.tb.connect((self.mult,0),(self.snk1,0))
    self.tb.connect((self.mult,1),(self.snk2,0))

相反,我的所有数据都流入snk1。如果我取消注释第二个连接,我收到一个错误,通知我self.mult块不能有更多的输出连接。

ValueError: port number 1 exceeds max of 0

如何制作插值块我可以用?

进行多个输出连接

1 个答案:

答案 0 :(得分:1)

看起来我误解了'interp'参数,它指定了输入矢量长度和输出矢量长度之间的比率。为此块创建多个输出的正确方法是将'out_sig'参数修改为输出类型列表,如下面修改后的multiply_out_fff类所示。

import numpy
from gnuradio import gr

class multiply_out_fff(gr.interp_block):
    """ 
    docstring for block multiply_out_fff
    """
    def __init__(self, multiple):
        gr.interp_block.__init__(self,
            name="multiply_out_fff",
            in_sig=[numpy.float32],
            out_sig=[numpy.float32,numpy.float32], interp = 1)
        self.multiple = multiple


    def work(self, input_items, output_items):
        in0 = input_items[0]
        out1 = output_items[0]
        out2 = output_items[1]    
#        print("the data coming in: ", in0)
#        print("in shape ",in0.shape)

        for i in range(len(in0)):
          out1[i] = in0[i] * self.multiple

        for i in range(len(in0)):
          out2[i] = in0[i] * self.multiple * 2 

#        print("the data going out: ", out)
#        print("out shape ", out.shape)

        return len(output_items[0])

Gnuradio Reference

Gnuradio - “如果我们想要向量,我们可以将它们定义为in_sig = [(numpy.float32,4),numpy.float32]。这意味着有两个输入端口,一个用于4个浮点数的向量,另一个用于标量“。