imc DEVICES raw数组转Matlab mat数据

  • 本来想将imc采集的数据进行拟合,拟合出一个大概的传函,用于仿真,但操作过程发现影响变量太多,拟合效果不好,查表无法覆盖,实践困难。测试功能还可以,闭环调试有点困难。
  • 新叶儿做的转换软件,可以使用,将所有数据都打包进了mat文件
  • 安装imctermite库,需要安装"适用于 C++ 的 Visual Studio 生成工具",链接地址
  • raw2mat,raw不能是中文,data第一列为时间,第二列为数据。后续可以根据文件名增加键,将值赋值该键,删除data键。
python 复制代码
# -*- coding: utf-8 -*-
import imctermite
import scipy.io as sio
import numpy as np
import os

# ==================== 设置部分 ====================
input_raw = "input2.raw"          # 输入的 .raw 文件名(假设为 input.raw,根据实际情况修改)
output_mat = "output2.mat"        # 输出的 .mat 文件名

if not os.path.exists(input_raw):
    print(f"错误:找不到文件 {input_raw}")
    exit(1)

print(f"正在读取: {input_raw}")

# ==================== 读取 .raw 文件 ====================
try:
    imc = imctermite.imctermite(input_raw.encode('utf-8'))
except RuntimeError as e:
    print("解析 .raw 文件失败:", e)
    exit(1)

# 获取所有通道的数据(包含 ydata)
channels_with_data = imc.get_channels(True)

if not channels_with_data:
    print("文件中没有找到任何通道数据")
    exit(1)

print(f"找到 {len(channels_with_data)} 个通道")

# ==================== 准备数据 ====================
# 取出时间向量(通常取第一个通道的 xdata)
time_vector = None
for ch in channels_with_data:
    x = ch.get('xdata')
    if x is not None and len(x) > 0:
        time_vector = np.asarray(x, dtype=np.float64)
        break

if time_vector is None:
    # 如果没有 xdata,使用索引作为时间(或根据 dt 生成)
    if len(channels_with_data) > 0:
        n = len(channels_with_data[0].get('ydata', []))
        dt = channels_with_data[0].get('dt', 1.0)  # 假设 dt 存在,否则用 1.0
        time_vector = np.arange(n) * dt
    else:
        print("错误:没有数据")
        exit(1)

# 建立 2D numpy 数组:第一列 time,其余列为每个通道的 ydata
data_cols = [time_vector]
channel_names = []  # 用于记录通道名称

for i, ch in enumerate(channels_with_data, 1):
    y = ch.get('ydata')
    if y is None or len(y) == 0:
        continue
    
    y = np.asarray(y, dtype=np.float64)
    
    # 检查长度一致性
    if len(y) != len(time_vector):
        print(f"警告:通道 {ch.get('name', '?')} 长度不符 ({len(y)} ≠ {len(time_vector)}),跳过")
        continue
    
    data_cols.append(y)
    
    # 生成安全的通道名称(MATLAB 变量名兼容)
    name = ch.get('name', f"channel_{i}")
    name = name.replace(" ", "_").replace("-", "_").replace(".", "_").replace("/", "_").replace("\\", "_")
    name = "".join(c for c in name if c.isalnum() or c == "_")
    if not name or name[0].isdigit():
        name = f"channel_{i}"
    channel_names.append(name)

# 如果没有通道数据,退出
if len(data_cols) <= 1:
    print("错误:没有有效的通道数据")
    exit(1)

# 合成 (样本数 × 列数) 的矩阵
data_matrix = np.column_stack(data_cols)

# ==================== 保存为 .mat 文件 ====================
mat_dict = {
    'data': data_matrix,                  # 主数据矩阵,第一列 time
    'channel_names': channel_names,       # 通道名称列表(从第二列开始)
    'source_file': input_raw              # 源文件信息
}

print(f"正在保存到: {output_mat} (形状: {data_matrix.shape})")
sio.savemat(output_mat, mat_dict, oned_as='column')

print("转换完成!")
print("MATLAB 中加载方式:")
print(f"   load('{output_mat}');")
print("   time = data(:,1);")
print("   % 通道数据:data(:,2) 是第一个通道,依此类推")
print(f"   % 通道名称:channel_names{{1}} 是 '{channel_names[0]}' 等")
相关推荐
Ryan-Zhong几秒前
数字人口播视频 API 实战:Python 跑通模板合成、异步轮询与批量出片
开发语言·python·音视频
努力努力再努力wz4 分钟前
【Lua 入门系列】模块与包(一):从多文件组织到require 运行时加载机制、模块缓存、预加载搜索与 Lua 文件搜索机制
开发语言·junit·lua
fengci.9 分钟前
CTF2困难部分
开发语言·网络·python·安全·php
佳得动10 分钟前
编译和链接的认识(C语言)
c语言·开发语言
大黄说说20 分钟前
PHP 异步怎么实现?Swoole、Workerman 使用场景对比
开发语言·php·swoole
十二同学啊21 分钟前
Python 工程化详解:项目结构、依赖管理、代码质量、测试与部署
java·开发语言·python
乐橙开放平台27 分钟前
跨地域监控难管理?通过 GB28181 把各地海康大华乐橙摄像头注册到云端平台
开发语言·笔记·物联网·自动化·php·音视频·智能家居
广东融科数据服务有限公司28 分钟前
网络监控经常卡顿、掉线、画面模糊?一线排查问答实录
开发语言·网络·php
Sarvartha1 小时前
Iterator 迭代器、ListIterator & foreach 循环
开发语言
樱花落木兰1 小时前
Redis 消息队列的可靠性演进:从 List、Pub/Sub 到 Stream 消费者组,以及 8.x 版本改变了什么
java·开发语言·经验分享·redis·分布式·python·软件工程