本文内容、所用开发板、配件等仅限与研发和学习使用,如有侵权,请联系我们进行删除!
目录
一、概要
二、实验环境
三、实验内容
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。
打开gnruadio,在右边的模块搜索栏中直接搜索“Python Block”,然后添加到流图中。然后双击该模块,并对代码进行自定义的修改。
import numpy as np
from gnuradio import grclass blk(gr.sync_block): # other base classes are basic_block, decim_block, interp_blockdef __init__(self): # only default arguments heregr.sync_block.__init__(self,name='mult_2_grc', in_sig=[npplex64],out_sig=[npplex64])def work(self, input_items, output_items):"""example: multiply with 2"""output_items[0][:] = input_items[0] * 2return len(output_items[0])
打开终端,收入一下命令,使用gr_modtool创建一个molude,即许多blocks的集合目录,然后进入该目录中,添加一个python block。
1. gr_modtool newmod ootBlocks //添加新的modlue
2. cd gr-ootBlocks/ //进入产生的目录
3. gr_modtool add mult_2_py //添加oot blockGNU Radio module name identified: ootBlocks('sink', 'source', 'sync', 'decimator', 'interpolator', 'general', 'tagged_stream', 'hier', 'noblock')Enter block type: sync Language (python/cpp): python //这里使用pythonLanguage: PythonBlock/code identifier: mult_2_pyPlease specify the copyright holder: Enter valid argument list, including default arguments: Add Python QA code? [Y/n] nAdding file 'python/mult_2_py.py'...Adding file 'grc/ootBlocks_mult_2_l'...Editing
4. cd python/ __init__.py mult_2_py.py
5. gedit mult_2_py.py //编辑后代码如下图所示
6. cd ../grc
7. gedit ootBlocks_mult_2_l //编辑后的代码如下图所示
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 grclass mult_2_py(gr.sync_block):def __init__(self):gr.sync_block.__init__(self,name="mult_2_py",in_sig=[npplex64],out_sig=[npplex64])def work(self, input_items, output_items):in0 = input_items[0]out = output_items[0]out[:] = in0 * 2return len(output_items[0])
编辑后的yml文件
id: ootBlocks_mult_2_py
label: mult_2_py
category: '[ootBlocks]'templates:imports: import ootBlocksmake: ootBlocks.mult_2_py()inputs:
- label: indomain: streamdtype: complexoutputs:
- label: outdomain: streamdtype: complexfile_format: 1
在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: syncLanguage (python/cpp): cpp //这里选择cppLanguage: C++Block/code identifier: mult_2_cppPlease specify the copyright holder: Enter valid argument list, including default arguments: Add Python QA code? [Y/n] nAdd C++ QA code? [Y/n] nAdding file 'lib/mult_2_cpp_impl.h'...Adding file 'lib/mult_2_cpp_impl'...Adding file 'include/ootBlocks/mult_2_cpp.h'...Editing swig/ootBlocks_Adding file 'grc/ootBlocks_mult_2_l'...Editing
2. gedit lib/mult_2_cpp_impl //修改实现自定义的功能,代码如下
3. gedit grc/ootBlocks_mult_2_l //代码如下
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::sptrmult_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(){}intmult_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 ootBlocksmake: ootBlocks.mult_2_cpp()inputs:
- label: indomain: streamdtype: complexoutputs:
- label: out domain: streamdtype: complexfile_format: 1
可见,三组图都是一模一样的幅度为2的正弦波重合在一起,相较与收入的幅度扩大了2倍。实验结果符合预期!
1、/
2、 OutOfTreeModules - GNU Radio
3、
本文发布于:2024-01-28 08:20:28,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17064012346070.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |