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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

目录

一、概要

二、实验环境

三、实验内容

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

### 创建和使用GNU Radio自定义模块 #### 定义OOT模块 OOT(Out-of-Tree)模块指的是GNU Radio框架本身不包含的额外功能模块,这些模块由用户依据特定需求自行设计实现。对于扩展GNU Radio的功能来说,创建这样的模块是非常重要的途径之一[^1]。 #### 工具支持 为了简化OOT模块的构建过程,官方提供了`gr_modtool`这一实用程序,在安装GNU Radio的过程中通常会一并安装此工具。通过该命令行工具可以方便快捷地初始化项目结构、添加源文件以及配置编译选项等操作[^4]。 #### 实际步骤展示 假设要建立一个新的名为`customModule`的OOT模块,则可以在终端执行如下指令: ```bash $ source /path/to/gnuradio/setup_env.sh # 加载环境变量(如果必要的话) $ mkdir build && cd build # 新建工作目录 $ cmake .. # 配置CMakeLists.txt $ make # 编译整个工程 $ sudo make install # 将库文件复制到系统路径下 ``` 接着利用`gr_modtool`快速搭建基础架构: ```bash $ gr_modtool newmod customModule # 初始化模块模板 Creating out-of-tree module in ./gr-customModule... The directory gr-customModule exists and does not seem to be empty. Are you sure you want to continue? [N/y] y Modifying CMakeLists... Adding language-specific files for 'cpp'... Done. ``` 此时会在当前文件夹内生成一系列必要的子目录及文档,其中包含了用于描述包信息的`CMakeLists.txt`、存放各类组件代码的`lib/`, `include/` 和针对图形界面集成的支持材料`grc/`. #### 使用OOT模块 一旦成功部署了上述流程所提及的内容之后,便能够在GNU Radio Companion (GRC) 中加载新加入的block,并像调用内置单元那样自由组合运用它们来完成复杂信号处理任务。只需确保启动GRC之前已经正确设置了PYTHONPATH等相关环境参数指向含有自定义blocks的位置即可[^2].
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Highmesh

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

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

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

打赏作者

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

抵扣说明:

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

余额充值