gnuradio学习之qa_tag_file_sink.py

简介:
#!/usr/bin/env python
#A file sink that uses tags to save files.

#        The sink uses a tag with the key 'burst' to trigger the saving of the burst data to a new file.
# If the value of this tag is True, it will open a new file and start writing all incoming data to it.
# If the tag is False, it will close the file (if already opened). The file names are based on the
# time when the burst tag was seen. If there is an 'rx_time' tag (standard with UHD sources),
# that is used as the time. If no 'rx_time' tag is found, the new time is calculated based off the 
#sample rate of the block.
from gnuradio import gr, gr_unittest, blocks
import os, struct
#####os 模块提供了一个统一的操作系统接口函数, 这些接口函数通常是平台指定的,os 模块能在不
#####同操作系统平台如 nt 或 posix中的特定函数间自动切换,从而能实现跨平台操作 ,
#####关于python的os模块http://www.cnblogs.com/wayneye/archive/2010/05/03/1726865.html

class test_tag_file_sink(gr_unittest.TestCase):

    def setUp(self):
        self.tb = gr.top_block()

    def tearDown(self):
        self.tb = None

    def test_001(self):##建立流图
        src_data = ( 1,  2,  3,  4,  5,  6,  7,  8,  9,  10)
        trg_data = (-1, -1,  1,  1, -1, -1,  1,  1, -1,  -1)
        src = blocks.vector_source_i(src_data)
        trg = blocks.vector_source_s(trg_data)
        op  = blocks.burst_tagger(gr.sizeof_int)
        snk = blocks.tagged_file_sink(gr.sizeof_int, 1)
        self.tb.connect(src, (op,0))
        self.tb.connect(trg, (op,1))
        self.tb.connect(op, snk)
        self.tb.run()

        # Tagged file sink gets 2 burst tags at index 2 and index 5.
        # Creates two new files, each with two integers in them from
        # src_data at these indexes (3,4) and (7,8).
        file0 = "file{0}_0_2.00000000.dat".format(snk.unique_id())
        file1 = "file{0}_1_6.00000000.dat".format(snk.unique_id())

        # Open the files and read in the data, then remove the files
        # to clean up the directory.
        outfile0 = file(file0, 'rb')#读取二进制文件,python中的文件读写:http://www.cnblogs.com/allenblogs/archive/2010/09/13/1824842.html
    outfile1 = file(file1, 'rb')
    data0 = outfile0.read(8)
    data1 = outfile1.read(8)
        outfile0.close()
        outfile1.close()
    os.remove(file0)
    os.remove(file1)

        # Convert the 8 bytes from the files into a tuple of 2 ints.

####python用struct处理二进制数据:http://www.cnblogs.com/gala/archive/2011/09/22/2184801.html
        idata0 = struct.unpack('ii', data0)
        idata1 = struct.unpack('ii', data1)

        self.assertEqual(idata0, (3, 4))##python中的测试模块
        self.assertEqual(idata1, (7, 8))

if __name__ == '__main__':
    gr_unittest.run(test_tag_file_sink, "test_tag_file_sink.xml")
#gr_unittest 是一个扩展的标准 Python 模块单元测试。gr_unittest 增加了对检查浮点
#和复数的元组的近似相等的支持。unittest 使用 Python 的反射机制来发现所有运行的方法和运行
#它们。unittest 包装每次调用 test_*来匹配建立和拆除的调用。当我们运行测试,在这种秩序中
#gr_unittest.main 要调用 SETUP,test_001和 tearDown。



目录
相关文章
|
SQL XML 安全
mybatis批量更新数据三种方法效率对比【Mysql】
mybatis批量更新数据三种方法效率对比【Mysql】
4577 0
mybatis批量更新数据三种方法效率对比【Mysql】
|
Python
【信号处理】python按原理实现BPSK、QPSK、QAM信号调制
本文提供了两种不同的方法来实现16-QAM(正交幅度调制)的调制和解调过程,一种是使用commpy库,另一种是通过手动定义映射字典来实现。
934 8
|
2天前
|
人工智能 运维 安全
|
5天前
|
SpringCloudAlibaba 负载均衡 Dubbo
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
本文对比分析了SpringCloudAlibaba框架下Feign与Dubbo的服务调用性能及差异。Feign基于HTTP协议,使用简单,适合轻量级微服务架构;Dubbo采用RPC通信,性能更优,支持丰富的服务治理功能。通过实际测试,Dubbo在调用性能、负载均衡和服务发现方面表现更出色。两者各有适用场景,可根据项目需求灵活选择。
386 124
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
|
7天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
702 107
|
2天前
|
算法 Python
【轴承故障诊断】一种用于轴承故障诊断的稀疏贝叶斯学习(SBL),两种群稀疏学习算法来提取故障脉冲,第一种仅利用故障脉冲的群稀疏性,第二种则利用故障脉冲的额外周期性行为(Matlab代码实现)
【轴承故障诊断】一种用于轴承故障诊断的稀疏贝叶斯学习(SBL),两种群稀疏学习算法来提取故障脉冲,第一种仅利用故障脉冲的群稀疏性,第二种则利用故障脉冲的额外周期性行为(Matlab代码实现)
223 152
|
4天前
|
Java 数据库 数据安全/隐私保护
Spring 微服务和多租户:处理多个客户端
本文介绍了如何在 Spring Boot 微服务架构中实现多租户。多租户允许单个应用实例为多个客户提供独立服务,尤其适用于 SaaS 应用。文章探讨了多租户的类型、优势与挑战,并详细说明了如何通过 Spring Boot 的灵活配置实现租户隔离、动态租户管理及数据源路由,同时确保数据安全与系统可扩展性。结合微服务的优势,开发者可以构建高效、可维护的多租户系统。
203 127
|
4天前
|
Web App开发 前端开发 API
在折叠屏应用中,如何处理不同屏幕尺寸和设备类型的样式兼容性?
在折叠屏应用中,如何处理不同屏幕尺寸和设备类型的样式兼容性?
230 124