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]}' 等")
相关推荐
软件算法开发3 分钟前
基于海象优化算法的LSTM网络模型(WOA-LSTM)的一维时间序列预测matlab仿真
算法·matlab·lstm·一维时间序列预测·woa-lstm·海象优化
Thera77715 分钟前
C++ 高性能时间轮定时器:从单例设计到 Linux timerfd 深度优化
linux·开发语言·c++
炘爚1 小时前
C语言(文件操作)
c语言·开发语言
阿蒙Amon1 小时前
C#常用类库-详解SerialPort
开发语言·c#
凸头1 小时前
CompletableFuture 与 Future 对比与实战示例
java·开发语言
wuqingshun3141591 小时前
线程安全需要保证几个基本特征
java·开发语言·jvm
Moksha2622 小时前
5G、VoNR基本概念
开发语言·5g·php
jzlhll1232 小时前
kotlin Flow first() last()总结
开发语言·前端·kotlin
W.D.小糊涂2 小时前
gpu服务器安装windows+ubuntu24.04双系统
c语言·开发语言·数据库
用头发抵命2 小时前
Vue 3 中优雅地集成 Video.js 播放器:从组件封装到功能定制
开发语言·javascript·ecmascript