前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >GNU Radio使用Python Block实现模块运行时间间隔获取

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

作者头像
Gnep@97
发布2024-04-19 08:19:10
530
发布2024-04-19 08:19:10
举报
文章目录
  • 前言
  • 一、timestamp_sender 模块
  • 二、timestamp_receiver 模块
  • 三、测试

前言

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


一、timestamp_sender 模块

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

代码语言:javascript
复制
"""
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 做一个接收端时间戳记录模块,并添加下面的代码:

代码语言:javascript
复制
"""
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 的时间

打印信息:

代码语言:javascript
复制
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 时间

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-04-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 前言
  • 一、timestamp_sender 模块
  • 二、timestamp_receiver 模块
  • 三、测试
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档