在GNURadio中写自己的模块(OOT)

1、Linux下安装GNURadio

参考:http://blog.csdn.net/windstrip/article/details/52847007

2、在GNURadio下写自己的Python模块

(1)构建OTT文件夹

$ gr_modtool newmod tutorial
Creating out-of-tree module in ./gr-tutorial... Done.
Use 'gr_modtool add' to add a new block to this currently empty module.

其中:gr-tutorial为模块所在文件夹。

(2)向文件夹中添加模块

gr-tutorial$ gr_modtool add -t sync -l python
GNU Radio module name identified: tutorial
Language: Python
Enter name of block/code (without module name prefix): multiply_py_ff
Block/code identifier: multiply_py_ff
Enter valid argument list, including default arguments: multiple
Add Python QA code? [Y/n] y
Adding file 'Python/multiply_py_ff.py'...
Adding file 'Python/qa_multiply_py_ff.py'...
Editing Python/CMakeLists.txt...
Adding file 'grc/tutorial_multiply_py_ff.xml'...
Editing grc/CMakeLists.txt...

可以通过以下代码查询add用法。

gr-tutorial$ gr_modtool help add

(3)需要修改的文件

1> multiply_py_ff.py

import numpy
from gnuradio import gr

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

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

标红的地方需要修改为自己的代码,init中定义输入输出信号的类型,例如修改为float32类型:

in_sig=[numpy.float32]
out_sig=[numpy.float32]

work中为信号处理代码。

2> qa_multiply_py_ff.py

from gnuradio import gr, gr_unittest
from gnuradio import blocks
from multiply_py_ff import multiply_py_ff

class qa_multiply_py_ff (gr_unittest.TestCase):

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

    def tearDown (self):
        self.tb = None

    def test_001_t (self):               
        src_data = (0, 1, -2, 5.5, -0.5)
        expected_result = (0, 2, -4, 11, -1)
        src = blocks.vector_source_f (src_data)
        mult = multiply_py_ff (2)
        snk = blocks.vector_sink_f ()
        self.tb.connect (src, mult)
        self.tb.connect (mult, snk)
        self.tb.run ()
        result_data = snk.data ()
        self.assertFloatTuplesAlmostEqual (expected_result, result_data, 6)

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

3> tutorial_multiply_py_ff.xml



  multiply_py_ff
  tutorial_multiply_py_ff
  tutorial
  import tutorial
  tutorial.multiply_py_ff($multiple)
  
  
    ...
    ...
    ...
  

  
  
    in
    
  

  
  
    out
    
  

3、在GNURadio下写自己的C++模块
(1)构建OTT文件夹

同python。

(2)向文件夹中添加模块

xyz@comp:mydir/gr-tutorial$ gr_modtool add my_qpsk_demod_cb
GNU Radio module name identified: tutorial
Enter code type: general
Language: C++
Block/code identifier: my_qpsk_demod_cb
Enter valid argument list, including default arguments:  bool gray_code
Add Python QA code? [Y/n] 
Add C++ QA code? [y/N] Y
Adding file 'my_qpsk_demod_cb_impl.h'...
Adding file 'my_qpsk_demod_cb_impl.cc'...
Adding file 'my_qpsk_demod_cb.h'...
Editing swig/qpsk_demod_swig.i...
Adding file 'qa_my_qpsk_demod_cb.py'...
Editing python/CMakeLists.txt...
Adding file 'qpsk_demod_my_qpsk_demod_cb.xml'...
Editing grc/CMakeLists.txt...

(3)需要修改的文件

1> my_qpsk_demod_cb_impl.cc、my_qpsk_demod_cb_impl.h

根据需要修改,general_work()即为模块运行时调用的函数。

2> demod_my_qpsk_demod_cb.xml

此文件定义模块GUI。



  My QPSK Demodulator
  tutorial_my_qpsk_demod_cb
  tutorial
  import tutorial
  tutorial.my_qpsk_demod_cb($gray_code)
  
    Gray Code
    gray_code
    True
    bool
    
    
  
  
    in
    complex
  
  
    out
    byte
  

3> qa_qpsk_demod.py

修改测试代码,根据功能测试模块。

4、安装模块

gr-tutorial$ mkdir build
gr-tutorial$ cd build
gr-tutorial/build$ cmake ..
...
gr-tutorial/build$ make
...
gr-tutorial/build$ sudo make install
...
gr-tutorial/build$ sudo ldconfig

5、使用模块
在GNURadio中写自己的模块(OOT)_第1张图片

参考:http://gnuradio.org/redmine/projects/gnuradio/wiki/Guided_Tutorial_GNU_Radio_in_Python

你可能感兴趣的:(在GNURadio中写自己的模块(OOT))