Build a new module with python

Build a new module

Build a simple new module, which output = input * 0.7, and types of them are both float.

  1. In command line, input: gr_modtool newmod tutorial
  2. cd gr_tutorial/
  3. gr_modtool add multiply_py_ff
  4. Enter block type: sync
  5. Language: python
  6. holder: just Enter
  7. Enter valid argument list, including default arguments: multiple
  8. Add Python QA code? [Y/n] y
    Note:
    Line 3. _ff means float in, float out
    Line 4. sync means input items = output items

Modify the module

1. Modify multiply_py_ff.py

Source 1:

def __init__(self, multiple):
        gr.sync_block.__init__(self,
            name="multiply_py_ff",
            in_sig=[<+numpy.float+>],
            out_sig=[<+numpy.float+>])
        self.multiple = multiple

After modify:

def __init__(self, multiple):
        gr.sync_block.__init__(self,
            name="multiply_py_ff",
            in_sig=[numpy.float32],
            out_sig=[numpy.float32])
        self.multiple = multiple

Source 2:

def work(self, input_items, output_items):
        in0 = input_items[0]
        out = output_items[0]
        # <+signal processing here+>
        out[:] = in0
        return len(output_items[0])

After modify:

def work(self, input_items, output_items):
        in0 = input_items[0]
        out = output_items[0]
        # <+signal processing here+>
        out[:] = in0 * self.multiple
        return len(output_items[0])

2. Modify qa_multiply_py_ff.py

Source:

def test_001_t (self):
        # set up fg
        self.tb.run ()
        # check data

After modify

def test_001_t (self):
        # set up fg
        src_data = (0, 1, -1, 3.5, -2.5)
        expected_result = (0, 2, -2, 7, -5)
        src = blocks.vector_source_f(src_data)
        mult = multiply_py_ff(2)
        snk = blocks.vector_sink_f()
        self.tb.connect(src, mult, snk)
        self.tb.run ()
        result_data = snk.data()
		# check data
        self.assertFloatTuplesAlmostEqual(expected_result, result_data, 6)

Note:

  1. In a new line, we should use space bar to avoid “unexpected indent”
  2. src = blocks.vector_source_f(src_data)
    There are several typies:
    b - bytes/ unsigned char/ int 8
    c - complex
    f - float
    i - int
    s - short

3. python qa_multiply_py_ff.py

Ran 1 test in 0.003s
OK

4. Modify tutorial_multiply_py_ff.xml in grc fold

source:

  <param>
    <name>...</name>
    <key>...</key>
    <type>...</type>
  </param>
  <sink>
    <name>in</name>
    <type><!-- e.g. int, float, complex, byte, short, xxx_vector, ...--></type>
  </sink>
  <source>
    <name>out</name>
    <type><!-- e.g. int, float, complex, byte, short, xxx_vector, ...--></type>
  </source>

After modify

  <param>
    <name>Multiple</name>
    <key>multiple</key>
    <type>float</type>
  </param>
  <sink>
    <name>in</name>
    <type>float</type>
  </sink>
  <source>
    <name>out</name>
    <type>float</type>
  </source>
  1. In gr-tutorial folder
mkdir build
cd build/
cmake ../
make
sudo make install
sudo ldconfig

Open GRC (gnuradio companion in command line), and then can find new module

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值