GNU Radio使用Python Block实现模块运行时间间隔获取

文章目录

  • 前言
  • [一、timestamp_sender 模块](#一、timestamp_sender 模块)
  • [二、timestamp_receiver 模块](#二、timestamp_receiver 模块)
  • 三、测试

前言

GNU Radio 中没有实现测量两个模块之间的时间测量模块,本文记录一下通过 python block 制作一个很简单的测时 block。


一、timestamp_sender 模块

使用 python block 做一个发送端时间戳记录模块,并添加下面的代码:

python 复制代码
"""
Embedded Python Blocks:

Each time this file is saved, GRC will instantiate the first class it finds
to get ports and parameters of your block. The arguments to __init__  will
be the parameters. All of them are required to have default values!
"""

import numpy as np
from gnuradio import gr
import time
import numpy as np

class timestamp_sender(gr.sync_block):  # other base classes are basic_block, decim_block, interp_block
    """Embedded Python Block example - a simple multiply const"""

    def __init__(self):  # only default arguments here
        """arguments to this function show up as parameters in GRC"""
        gr.sync_block.__init__(
            self,
            name="timestamp_sender",   # will show up in GRC
            in_sig=None,
            out_sig=[np.float32]
        )
        self.kk = 1;
        
        # if an attribute with the same name as a parameter is found,
        # a callback is registered (properties work, too).

    def work(self, input_items, output_items):
        # Record the current time
        start = np.float32(time.perf_counter())
        
        # Output data and the current timestamp (here using a simple value for demonstration)
        output_items[0][:] = [start for _ in output_items[0]]
        
        if self.kk == 1:
            self.kk = 2;
            print(f"output_items[0][:] = {output_items[0][:]}");
        
        return len(output_items[0])

二、timestamp_receiver 模块

使用 python block 做一个接收端时间戳记录模块,并添加下面的代码:

python 复制代码
"""
Embedded Python Blocks:

Each time this file is saved, GRC will instantiate the first class it finds
to get ports and parameters of your block. The arguments to __init__  will
be the parameters. All of them are required to have default values!
"""

import numpy as np
from gnuradio import gr
import numpy as np  # 导入NumPy库
import time

class timestamp_receiver(gr.sync_block):  # other base classes are basic_block, decim_block, interp_block
    """Embedded Python Block example - a simple multiply const"""

    def __init__(self):  # only default arguments here
        """arguments to this function show up as parameters in GRC"""
        gr.sync_block.__init__(
            self,
            name="timestamp_receiver",   # will show up in GRC
            in_sig=[np.float32],
            out_sig=None
        )
        # if an attribute with the same name as a parameter is found,
        # a callback is registered (properties work, too).
        self.kk = 1

    def work(self, input_items, output_items):
        for item in input_items[0]:
            if item == 0:
                continue
            if self.kk == 1:
                self.kk = 2
                end = np.float32(time.perf_counter())
                print(f"input_items[0] = {input_items[0]}")
                print(f"Received at {end}, interval since sent: {(end - item) * 1000000} Microsecond")

        return len(input_items[0])

三、测试

按照下图将 block 进行连接:

采样率 32KHz,延时 320 * 5 = 160000 个采样点,大约 5s 的时间

打印信息:

bash 复制代码
Generating: '/home/gnep/ofdm_usrp/test.py'

Executing: /usr/bin/python3 -u /home/gnep/ofdm_usrp/test.py

Press Enter to quit: output_items[0][:] = [7809.57 7809.57 7809.57 ... 7809.57 7809.57 7809.57]
item = 7809.56982421875
input_items[0] = [   0.      0.      0.   ... 7809.57 7809.57 7809.57]
Received at 7814.44189453125, interval since sent: 4872070.3125 Microsecond

可以看到打印信息为 4872070.3125 ,大约为 5s 时间


我的qq:2442391036,欢迎交流!


相关推荐
懒大王爱吃狼19 分钟前
Python教程:python枚举类定义和使用
开发语言·前端·javascript·python·python基础·python编程·python书籍
秃头佛爷1 小时前
Python学习大纲总结及注意事项
开发语言·python·学习
深度学习lover2 小时前
<项目代码>YOLOv8 苹果腐烂识别<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·苹果腐烂识别
API快乐传递者3 小时前
淘宝反爬虫机制的主要手段有哪些?
爬虫·python
阡之尘埃5 小时前
Python数据分析案例61——信贷风控评分卡模型(A卡)(scorecardpy 全面解析)
人工智能·python·机器学习·数据分析·智能风控·信贷风控
丕羽8 小时前
【Pytorch】基本语法
人工智能·pytorch·python
bryant_meng9 小时前
【python】Distribution
开发语言·python·分布函数·常用分布
m0_5945263010 小时前
Python批量合并多个PDF
java·python·pdf
工业互联网专业10 小时前
Python毕业设计选题:基于Hadoop的租房数据分析系统的设计与实现
vue.js·hadoop·python·flask·毕业设计·源码·课程设计
钱钱钱端10 小时前
【压力测试】如何确定系统最大并发用户数?
自动化测试·软件测试·python·职场和发展·压力测试·postman