Gnuradio创建OOT自定义模块的三种方式

本文介绍了在Gnuradio中创建OOT(OutOfTree)模块的三种方法,包括通过grc添加Python Block、使用gr_modtool创建Python和C++定义的OOT block。实验结果显示,所有方法都能实现将输入信号幅度扩大2倍的效果。

本文内容、所用开发板、配件等仅限与研发和学习使用,如有侵权,请联系我们进行删除!

目录

一、概要

二、实验环境

三、实验内容

1、方式一:grc 中直接添加“Python Block”

2、方式二:使用gr_modtool添加python定义的OOT block

3、方式三:使用gr_modtool添加C++定义的OOT block

4、实验结果

四、参考链接


一、概要

OOT模块是指Gnuradio中并不包含的模块资源,用户根据需求自己定义来制作。编写OOT模块的方法多种,下面用简单的例子分别进行介绍。

二、实验环境

ubuntu 20.04 ; gnuradio 3.8 ; uhd 4.1.0

三、实验内容

下面以自定义一个2倍乘法器的grc oot block,即将输入的每个数据值都乘以2为例,简单介绍一下如何生成OOT block。

1、方式一:grc 中直接添加“Python Block”

打开gnruadio,在右边的模块搜索栏中直接搜索“Python Block”,然后添加到流图中。然后双击该模块,并对代码进行自定义的修改。

import numpy as np
from gnuradio import gr

class blk(gr.sync_block):  # other base classes are basic_block, decim_block, interp_block

    def __init__(self):  # only default arguments here
        gr.sync_block.__init__(
            self,
            name='mult_2_grc',   
            in_sig=[np.complex64],
            out_sig=[np.complex64]
        )

    def work(self, input_items, output_items):
        """example: multiply with 2"""
        output_items[0][:] = input_items[0] * 2
        return len(output_items[0])

2、方式二:使用gr_modtool添加python定义的OOT block

打开终端,收入一下命令,使用gr_modtool创建一个molude,即许多blocks的集合目录,然后进入该目录中,添加一个python block。

1. gr_modtool newmod ootBlocks  //添加新的modlue
2. cd gr-ootBlocks/             //进入产生的目录
3. gr_modtool add mult_2_py     //添加oot block
    GNU Radio module name identified: ootBlocks
    ('sink', 'source', 'sync', 'decimator', 'interpolator', 'general',     'tagged_stream', 'hier', 'noblock')
    Enter block type: sync    
    Language (python/cpp): python  //这里使用python
    Language: Python
    Block/code identifier: mult_2_py
    Please specify the copyright holder: 
    Enter valid argument list, including default arguments: 
 
    Add Python QA code? [Y/n] n
    Adding file 'python/mult_2_py.py'...
    Adding file 'grc/ootBlocks_mult_2_py.block.yml'...
    Editing grc/CMakeLists.txt...
4. cd python/                    
    CMakeLists.txt  __init__.py  mult_2_py.py
5. gedit mult_2_py.py          //编辑后代码如下图所示
6. cd ../grc
7. gedit  ootBlocks_mult_2_py.block.yml  //编辑后的代码如下图所示
8. mkdir build/ && cd build/ && cmake ../ && make 
9. sudo make install && sudo ldconfig     //编译安装

编辑后的python代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy
from gnuradio import gr

class mult_2_py(gr.sync_block):

    def __init__(self):
        gr.sync_block.__init__(self,
            name="mult_2_py",
            in_sig=[np.complex64],
            out_sig=[np.complex64])


    def work(self, input_items, output_items):
        in0 = input_items[0]
        out = output_items[0]
        out[:] = in0 * 2
        return len(output_items[0])

编辑后的yml文件

id: ootBlocks_mult_2_py
label: mult_2_py
category: '[ootBlocks]'

templates:
  imports: import ootBlocks
  make: ootBlocks.mult_2_py()

inputs:
- label: in
  domain: stream
  dtype: complex

outputs:
- label: out
  domain: stream
  dtype: complex

file_format: 1

3、方式三:使用gr_modtool添加C++定义的OOT block

在gr-ootBlocks中再添加一个用C++实现的OOT block.

1. gr_modtool add mult_2_cpp   //再添加一个oot block
    ('sink', 'source', 'sync', 'decimator', 'interpolator', 'general',  'tagged_stream', 'hier', 'noblock')
    Enter block type: sync
    Language (python/cpp): cpp  //这里选择cpp
    Language: C++
    Block/code identifier: mult_2_cpp
    Please specify the copyright holder: 
    Enter valid argument list, including default arguments: 
 
    Add Python QA code? [Y/n] n
    Add C++ QA code? [Y/n] n
    Adding file 'lib/mult_2_cpp_impl.h'...
    Adding file 'lib/mult_2_cpp_impl.cc'...
    Adding file 'include/ootBlocks/mult_2_cpp.h'...
    Editing swig/ootBlocks_swig.i...
    Adding file 'grc/ootBlocks_mult_2_cpp.block.yml'...
    Editing grc/CMakeLists.txt...
2. gedit lib/mult_2_cpp_impl.cc  //修改实现自定义的功能,代码如下
3. gedit grc/ootBlocks_mult_2_cpp.block.yml //代码如下
4. 然后再次编译安装

修改后的cpp文件

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gnuradio/io_signature.h>
#include "mult_2_cpp_impl.h"

namespace gr {
  namespace ootBlocks {

    mult_2_cpp::sptr
    mult_2_cpp::make()
    {
      return gnuradio::get_initial_sptr
        (new mult_2_cpp_impl());
    }

    mult_2_cpp_impl::mult_2_cpp_impl()
      : gr::sync_block("mult_2_cpp",
              gr::io_signature::make(1, 1, sizeof(gr_complex)),
              gr::io_signature::make(1, 1, sizeof(gr_complex)))
    {}

    mult_2_cpp_impl::~mult_2_cpp_impl()
    {
    }

    int
    mult_2_cpp_impl::work(int noutput_items,
        gr_vector_const_void_star &input_items,
        gr_vector_void_star &output_items)
    {
      const gr_complex *in = (const gr_complex *) input_items[0];
      gr_complex *out = (gr_complex *) output_items[0];
      
      for(int index = 0; index < noutput_items; index++) {
        out[index] = in[index] * (gr_complex)2;
      }
      return noutput_items;
    }

  } /* namespace ootBlocks */
} /* namespace gr */

修改后的yml文件

id: ootBlocks_mult_2_cpp
label: mult_2_cpp
category: '[ootBlocks]'

templates:
  imports: import ootBlocks
  make: ootBlocks.mult_2_cpp()

inputs:
- label: in
  domain: stream
  dtype: complex

outputs:
- label: out    
  domain: stream
  dtype: complex

file_format: 1

4、实验结果

可见,三组图都是一模一样的幅度为2的正弦波重合在一起,相较与收入的幅度扩大了2倍。实验结果符合预期!

四、参考链接

1、http://www.highmesh.cn/

2、 OutOfTreeModules - GNU Radio

3、https://blog.youkuaiyun.com/qq_41300075/article/details/121411905

Gnuradio自定义C++模块有多种方法和相关教程。 在自定义开发前,需在Linux上安装GNU Radio软件,安装时会默认安装gr_modtool脚本工具,其可自动生成模块的样板代码、makefile文件等,开发者能专注于模块的核心代码编写,可基于matlab的sin函数在GNU Radio软件上通过自定义编写信号源模块实现函数功能,生成正弦波形[^3]。 创建OOT自定义模块的一种方式涉及修改cpp文件,示例代码如下: ```cpp #ifdef HAVE_CONFIG_H #include "config.h" #endif #include <gnuradio/io_signature.h> #include "mult_2_cpp_impl.h" namespace gr { namespace ootBlocks { mult_2_cpp::sptr mult_2_cpp::make() { return gnuradio::get_initial_sptr (new mult_2_cpp_impl()); } mult_2_cpp_impl::mult_2_cpp_impl() : gr::sync_block("mult_2_cpp", gr::io_signature::make(1, 1, sizeof(gr_complex)), gr::io_signature::make(1, 1, sizeof(gr_complex))) { } mult_2_cpp_impl::~mult_2_cpp_impl() { } int mult_2_cpp_impl::work(int noutput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items) { const gr_complex *in = (const gr_complex *) input_items[0]; gr_complex *out = (gr_complex *) output_items[0]; for(int index = 0; index < noutput_items; index++) { out[index] = in[index] * (gr_complex)2; } return noutput_items; } } /* namespace ootBlocks */ } /* namespace gr */ ``` 另外,还可通过“Creating C++ OOT with gr - modtool”的方式实现自定义C++模块。关于Python的自定义模块法可参考:GNURadio:Embedded Python Block以及知乎上的“GNURadio开发入门(作者楚友马)” [^1][^2]。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Highmesh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值