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]}' 等")
相关推荐
山居秋暝LS4 分钟前
【无标题】RTX00安装paddle OCR,win11不能装最新的,也不能用GPU
开发语言·r语言
卢锡荣9 分钟前
单芯通吃,盲插标杆 —— 乐得瑞 LDR6020,Type‑C 全场景互联 “智慧芯”
c语言·开发语言·计算机外设
Xin_ye1008613 分钟前
C# 零基础到精通教程 - 第七章:面向对象编程(入门)——类与对象
开发语言·c#
AI科技星44 分钟前
《数学公理体系·第三部·数术几何》(2026 年版)
c语言·开发语言·线性代数·算法·矩阵·量子计算·agi
审判长烧鸡1 小时前
【Go工具】go-playground是什么组织?官方的?
开发语言·安全·go
kkeeper~1 小时前
0基础C语言积跬步之字符函数与字符串函数(上)
c语言·开发语言
hhb_6182 小时前
Swift核心技术难点与实战案例解析
开发语言·ios·swift
一楼的猫2 小时前
从工具链视角对比:番茄作家助手 vs 第三方写作辅助方案
java·服务器·开发语言·前端·学习·chatgpt·ai写作
程序leo源2 小时前
Qt窗口详解
开发语言·数据库·c++·qt·青少年编程·c#
likerhood3 小时前
Java static 关键字从浅入深
java·开发语言