用numpy读取二进制数据文件中的字节偏移

如何解决用numpy读取二进制数据文件中的字节偏移

我正在使用一个十几岁的微控制器来收集一些数据,并将其存储为具有以下数据记录格式的二进制数据文件:

struct data_t {
  uint32_t time;
  uint32_t test1;
  uint32_t test2;  
};

然后我正尝试使用以下python代码读取数据

dt = np.dtype([('a','<u4'),('b',('c','<u4')])
data = np.fromfile(file,dtype=dt)

前两行数据应如下所示:

(925,1,2)
(1025,2)

但是前两行看起来像这样:

(   42,925,1)
(    2,1025,1)

42是从哪里来的?数据是使用循环缓冲区写入的,每个缓冲区的开始似乎都出现了42。 42是某种标题吗?如何正确读取这些数据?

-编辑-

这是numpy的完整输出:

array([(  42,1),(   2,1125,1225,1325,1425,1525,1625,1725,1825,1925,2025,2125,2225,2325,2425,2525,2625,2725,2825,2925,3025,3125,3225,3325,3425,3525,3625,3725,3825,3925,4025,4125,4225,4325,4425,4525,4625,4725,4825,4925,5025,42),(5125,2),(5225,(5325,(5425,(5525,(5625,(5725,(5825,(5925,(6025,(6125,(6225,(6325,(6425,(6525,(6625,(6725,(6825,(6925,(7025,(7125,(7225,(7325,(7425,(7525,(7625,(7725,(7825,(7925,(8025,(8125,(8225,(8325,(8425,(8525,(8625,(8725,(8825,(8925,(9025,(9125,(9225,2)],dtype=[('a','<u4')])

这是对应的完整十六进制转储,请注意,在numpy输出中,带有“ 2a00”的两行是“ 42”出现的位置。

00000000: 2a00 0000 9d03 0000 0100 0000 0200 0000  *...............      <-- note the 2a00
00000010: 0104 0000 0100 0000 0200 0000 6504 0000  ............e...
00000020: 0100 0000 0200 0000 c904 0000 0100 0000  ................
00000030: 0200 0000 2d05 0000 0100 0000 0200 0000  ....-...........
00000040: 9105 0000 0100 0000 0200 0000 f505 0000  ................
00000050: 0100 0000 0200 0000 5906 0000 0100 0000  ........Y.......
00000060: 0200 0000 bd06 0000 0100 0000 0200 0000  ................
00000070: 2107 0000 0100 0000 0200 0000 8507 0000  !...............
00000080: 0100 0000 0200 0000 e907 0000 0100 0000  ................
00000090: 0200 0000 4d08 0000 0100 0000 0200 0000  ....M...........
000000a0: b108 0000 0100 0000 0200 0000 1509 0000  ................
000000b0: 0100 0000 0200 0000 7909 0000 0100 0000  ........y.......
000000c0: 0200 0000 dd09 0000 0100 0000 0200 0000  ................
000000d0: 410a 0000 0100 0000 0200 0000 a50a 0000  A...............
000000e0: 0100 0000 0200 0000 090b 0000 0100 0000  ................
000000f0: 0200 0000 6d0b 0000 0100 0000 0200 0000  ....m...........
00000100: d10b 0000 0100 0000 0200 0000 350c 0000  ............5...
00000110: 0100 0000 0200 0000 990c 0000 0100 0000  ................
00000120: 0200 0000 fd0c 0000 0100 0000 0200 0000  ................
00000130: 610d 0000 0100 0000 0200 0000 c50d 0000  a...............
00000140: 0100 0000 0200 0000 290e 0000 0100 0000  ........).......
00000150: 0200 0000 8d0e 0000 0100 0000 0200 0000  ................
00000160: f10e 0000 0100 0000 0200 0000 550f 0000  ............U...
00000170: 0100 0000 0200 0000 b90f 0000 0100 0000  ................
00000180: 0200 0000 1d10 0000 0100 0000 0200 0000  ................
00000190: 8110 0000 0100 0000 0200 0000 e510 0000  ................
000001a0: 0100 0000 0200 0000 4911 0000 0100 0000  ........I.......
000001b0: 0200 0000 ad11 0000 0100 0000 0200 0000  ................
000001c0: 1112 0000 0100 0000 0200 0000 7512 0000  ............u...
000001d0: 0100 0000 0200 0000 d912 0000 0100 0000  ................
000001e0: 0200 0000 3d13 0000 0100 0000 0200 0000  ....=...........
000001f0: a113 0000 0100 0000 0200 0000 0000 0000  ................
00000200: 2a00 0000 0514 0000 0100 0000 0200 0000  *...............      <-- note the 2a00
00000210: 6914 0000 0100 0000 0200 0000 cd14 0000  i...............
00000220: 0100 0000 0200 0000 3115 0000 0100 0000  ........1.......
00000230: 0200 0000 9515 0000 0100 0000 0200 0000  ................
00000240: f915 0000 0100 0000 0200 0000 5d16 0000  ............]...
00000250: 0100 0000 0200 0000 c116 0000 0100 0000  ................
00000260: 0200 0000 2517 0000 0100 0000 0200 0000  ....%...........
00000270: 8917 0000 0100 0000 0200 0000 ed17 0000  ................
00000280: 0100 0000 0200 0000 5118 0000 0100 0000  ........Q.......
00000290: 0200 0000 b518 0000 0100 0000 0200 0000  ................
000002a0: 1919 0000 0100 0000 0200 0000 7d19 0000  ............}...
000002b0: 0100 0000 0200 0000 e119 0000 0100 0000  ................
000002c0: 0200 0000 451a 0000 0100 0000 0200 0000  ....E...........
000002d0: a91a 0000 0100 0000 0200 0000 0d1b 0000  ................
000002e0: 0100 0000 0200 0000 711b 0000 0100 0000  ........q.......
000002f0: 0200 0000 d51b 0000 0100 0000 0200 0000  ................
00000300: 391c 0000 0100 0000 0200 0000 9d1c 0000  9...............
00000310: 0100 0000 0200 0000 011d 0000 0100 0000  ................
00000320: 0200 0000 651d 0000 0100 0000 0200 0000  ....e...........
00000330: c91d 0000 0100 0000 0200 0000 2d1e 0000  ............-...
00000340: 0100 0000 0200 0000 911e 0000 0100 0000  ................
00000350: 0200 0000 f51e 0000 0100 0000 0200 0000  ................
00000360: 591f 0000 0100 0000 0200 0000 bd1f 0000  Y...............
00000370: 0100 0000 0200 0000 2120 0000 0100 0000  ........! ......
00000380: 0200 0000 8520 0000 0100 0000 0200 0000  ..... ..........
00000390: e920 0000 0100 0000 0200 0000 4d21 0000  . ..........M!..
000003a0: 0100 0000 0200 0000 b121 0000 0100 0000  .........!......
000003b0: 0200 0000 1522 0000 0100 0000 0200 0000  ....."..........
000003c0: 7922 0000 0100 0000 0200 0000 dd22 0000  y"..........."..
000003d0: 0100 0000 0200 0000 4123 0000 0100 0000  ........A#......
000003e0: 0200 0000 a523 0000 0100 0000 0200 0000  .....#..........
000003f0: 0924 0000 0100 0000 0200 0000 0000 0000  .$..............

解决方法

也许有更好的方法,但是使用以下嵌套数据类型有效:

dt = np.dtype([('head','<u4'),('buff',[('a',('b',('c','<u4')],(number_of_data_records,)),('tail','i1',number_fill_bytes)])

这个想法是,我正在读取每个块,其中包括一个4字节的标头,具有(42)个实际数据记录的缓冲区以及一个包含空填充字节(由teensy代码定义)的尾部。

数据记录的数量由看起来是4字节的标头给出。可以自动确定填充字节数吗?

我将小代码,数据和python分析放在github上,以防对任何人有帮助:https://github.com/florisvb/data_tutorials/tree/master/TeensyDataLogger

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-