当前位置: 首页 > news >正文

【matlab】读取.rec文件格式,脑电数据格式(Trodes)文件格式处理

代码解读

该代码旨在从 Trodes .rec 文件中读取电生理信号数据并对其进行处理。.rec 文件格式用于记录神经数据,而该代码通过指定通道从中提取数据,并按时间片段保存相关信息。

主要步骤
  1. 指定文件和通道:

    • filename1 为输入文件路径。
    • channels 变量定义了多个通道,包含nTrode ID和相应的通道号。
  2. 调用自定义函数 readTrodesFileContinuous

    • 该函数从 .rec 文件中读取指定通道的连续数据。
    • 参数 skipTime=0 表示会读取每个采样的时间戳信息。
  3. 数据处理:

    • out.channelData 保存了通道数据,out.timestamps 保存了时间戳信息。
    • 根据时间片长度 timel(以分钟为单位)分割数据为三个部分:起始部分、中间部分和结束部分。
    • 数据片段存储为 tp_begin_datatp_mid_datatp_end_data
  4. 保存数据:

    • 最终处理完的数据以 .mat 格式保存,方便以后在 MATLAB 中使用。
主要函数:readTrodesFileContinuous

该函数从 .rec 文件中读取多个通道的电生理数据,并返回一个结构体 out,包括通道数据、时间戳和一些配置信息。

  • 输入参数:

    • filename:输入 .rec 文件名。
    • channels:二维数组,每行指定一个通道的 nTrode ID 和通道号。
    • skipTime:若为0,读取时间戳信息,若为1,则忽略时间戳。
  • 内部步骤:

    • 首先读取 .rec 文件的配置信息,获取通道数、采样率、头文件大小等。
    • 根据指定的 channels 列表,找出相应的硬件通道号。
    • 使用 importChannels 函数从文件中读取通道数据和时间戳信息。

文档:MATLAB中读取Trodes .rec 文件数据

1. 简介

Trodes .rec 文件用于记录神经信号,包括电极上的电生理数据。为了处理这些数据,我们可以使用 MATLAB 编写代码来读取指定通道的神经数据。本指南将介绍如何编写 MATLAB 代码读取 .rec 文件中的数据,并进行基本的数据处理和存储。

2. 步骤
2.1 设置文件路径和通道

首先,指定你要读取的 .rec 文件路径:

filename = 'C:/data/20230920_135926.rec';

然后,定义你要提取的通道:

channels = [1, 1; 256, 1; 512, 1; 768, 1; 1024, 1];
2.2 读取 .rec 文件数据

通过自定义的函数 readTrodesFileContinuous 来读取数据:

out = readTrodesFileContinuous(filename, channels, 0);

函数会返回一个包含通道数据和时间戳信息的结构体 out

2.3 数据处理

提取出来的数据可以进一步进行时间片段的处理,如按时间划分为起始、中间和结束部分:

timel = 1; % 设置时间片段长度,单位为分钟
datal = length(out.channelData); % 获取数据长度if out.timelength > 3 * timel% 分割数据tp_begin = 1:timel*60*30000;tp_mid = int32(datal/2)-0.5*timel*60*30000:int32(datal/2)+0.5*timel*60*30000-1;tp_end = int32(datal)-timel*60*30000:int32(datal)-1;% 存储各时间片段的数据out.tp_begin_data = out.channelData(tp_begin, :);out.tp_mid_data = out.channelData(tp_mid, :);out.tp_end_data = out.channelData(tp_end, :);
elseerror("数据时长不足以分割");
end
2.4 保存处理后的数据

处理完的数据可以保存为 .mat 文件:

save('processed_data.mat', 'out', '-v7.3');
3. 函数解释

readTrodesFileContinuous 是一个核心函数,用来从 .rec 文件中提取数据。其参数及工作原理如下:

  • filename: .rec 文件路径。
  • channels: 一个二维数组,每行包含 nTrode ID 和相应的通道号。
  • skipTime: 控制是否读取时间戳信息。

该函数读取配置文件、获取硬件通道号,并通过内部函数 importChannels 从文件中导入数据,返回一个包含通道数据和时间戳的结构体 out

4. 总结

本指南介绍了如何在 MATLAB 中读取和处理 Trodes .rec 文件数据。通过自定义函数,你可以灵活提取所需的通道数据并进行分段处理。

%所有代码,脚本文件如下:
filename1="C:/data/20230920_135926.rec"filename=filename1
%读取采集信息
% out1 = readTrodesFileConfig(filename)%从.rec文件中提取并保存时间信息,选择通道(1,1)(2,1)
%如果是通道选择通道256,(256,1)
channels=[1,1;256,1;512,1;768,1;1024,1]
skipTime=0
timel=1;out = readTrodesFileContinuous(filename,channels,skipTime)
out.channelid=channels(1);
out.timelength=length(out.channelData)/30000/60;
datal=length(out.channelData);if out.timelength>3*timeltp_begin=1:timel*60*30000;tp_mid=int32(datal/2)-0.5*timel*60*30000:int32(datal/2)+0.5*timel*60*30000-1;tp_end=int32(datal)-timel*60*30000:int32(datal)-1;out.tp_begin_data=out.channelData(tp_begin,:);out.tp_mid_data=out.channelData(tp_mid,:);out.tp_end_data=out.channelData(tp_end,:);elseerror("err")
end% out=rmfield(out,["timestamps" "channelData"]);save("20231124.mat","out","-v7.3")
%% 开源函数读rec
function out = readTrodesFileContinuous(filename,channels,skipTime,varargin)
%out = readTrodesFileChannels(filename,channels)
%out = readTrodesFileChannels(filename,channels,skipTime)
%out = readTrodesFileChannels(filename,channels,skipTime,OPTIONS)
%Reads in the designated channels from a Trodes .rec file
%filename -- the name of the .rec file, i.e., 'myrecording.rec'
%channels -- a list of channels to extract from the file, where each entry
%row lists the nTrode ID, follwed by the channel number (1-based)
%skipTime (default 0) -- skips reading in the time of each sample
%out -- a structure containing the broadband traces of the channels,timestamps, and some configuration info
%OPTIONS
%configFileName -- if the configuration settings at not at the top of the
%file, you can designate another file which has the config settings.out = [];if (nargin < 3)skipTime = 0;
endif (size(channels,2) ~= 2)error('The "channels" input must have two columns for nTrode and channel');
endconfigFileName = [];
configExists = 1;for option = 1:2:length(varargin)-1   if isstr(varargin{option})       switch(varargin{option})case 'configFileName'configFileName = varargin{option+1};otherwiseerror(['Option ',varargin{option},' unknown.']);end        elseerror('Options must be strings, followed by the variable');end
endif (isempty(configFileName))configInfo = readTrodesFileConfig(filename);
elseconfigInfo = readTrodesFileConfig(configFileName);configExists = 0;
end
numChannels = str2num(configInfo.numChannels);
samplingRate = str2num(configInfo.samplingRate);
headerSize = str2num(configInfo.headerSize);
numCards = numChannels/32;%Compile a list of the actual packet offsets for the desired channels
hwChannels = [];
displist = [];
for chInd = 1:size(channels,1) foundCh = 0;for trodeInd = 1:length(configInfo.nTrodes)if (str2num(configInfo.nTrodes(trodeInd).id) == channels(chInd,1))%This is the correct nTrode, based on the id fieldfoundCh = 1;if (length(configInfo.nTrodes(trodeInd).channelInfo) >= channels(chInd,2))hwChannels = [hwChannels configInfo.nTrodes(trodeInd).channelInfo(channels(chInd,2)).packetLocation+1];elseerror(['nTrode ', num2str(channels(chInd,1)), ' does not have enough channels.']);end                     break;endendif (~foundCh)error(['nTrode ID ',num2str(channels(chInd,1)), ' not found.']);end
end%Read in the data
if (skipTime)[out.channelData] = importChannels(filename,numChannels,hwChannels,samplingRate,headerSize, configExists);out.timestamps = [];
else    [out.channelData, out.timestamps] = importChannels(filename,numChannels,hwChannels,samplingRate,headerSize, configExists);%timeDir = diff(out.timestamps);%out.timestamps(find(timeDir < 0)) = out.timestamps(find(timeDir < 0))*-1;
end
out.headerSize = headerSize;
out.samplingRate = samplingRate;
out.numChannels = numChannels;end

其他的trode函数,私信。

相关文章:

  • 京东快递员的创新服务
  • 【网站打包app】Prime Web 1.0.10 – 将网站转换为 Flutter 应用程序 |Web View 应用程序 |Web 到 App
  • Tair简介
  • MySQL—触发器详解
  • rabbitMQ 简单使用
  • 9.29总结
  • Sqlserver 死锁指南
  • mysql事务详解
  • hive-拉链表
  • 【源码+文档+调试讲解】无人超市系统python
  • 安卓 shape 的使用
  • 【记录】Excel|不允许的操作:合并或隐藏单元格出现的问题列表及解决方案
  • MySQL InnoDB MVCC数据结构分析
  • smb文件夹共享设置
  • Linux学习之路 -- 线程 -- 条件变量与生产消费模型
  • [译]前端离线指南(上)
  • 《网管员必读——网络组建》(第2版)电子课件下载
  • Angular4 模板式表单用法以及验证
  • codis proxy处理流程
  • eclipse的离线汉化
  • ECS应用管理最佳实践
  • HomeBrew常规使用教程
  • iOS | NSProxy
  • Javascript Math对象和Date对象常用方法详解
  • JSONP原理
  • leetcode46 Permutation 排列组合
  • NLPIR语义挖掘平台推动行业大数据应用服务
  • React中的“虫洞”——Context
  • scala基础语法(二)
  • thinkphp5.1 easywechat4 微信第三方开放平台
  • vagrant 添加本地 box 安装 laravel homestead
  • Xmanager 远程桌面 CentOS 7
  • 基于Mobx的多页面小程序的全局共享状态管理实践
  • 前端存储 - localStorage
  • 使用权重正则化较少模型过拟合
  • 数据结构java版之冒泡排序及优化
  • 想写好前端,先练好内功
  • [地铁译]使用SSD缓存应用数据——Moneta项目: 低成本优化的下一代EVCache ...
  • 《TCP IP 详解卷1:协议》阅读笔记 - 第六章
  • Spark2.4.0源码分析之WorldCount 默认shuffling并行度为200(九) ...
  • # 数仓建模:如何构建主题宽表模型?
  • # 职场生活之道:善于团结
  • #Spring-boot高级
  • (4) PIVOT 和 UPIVOT 的使用
  • (6) 深入探索Python-Pandas库的核心数据结构:DataFrame全面解析
  • (Qt) 默认QtWidget应用包含什么?
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (笔记)Kotlin——Android封装ViewBinding之二 优化
  • (二刷)代码随想录第16天|104.二叉树的最大深度 559.n叉树的最大深度● 111.二叉树的最小深度● 222.完全二叉树的节点个数
  • (三)uboot源码分析
  • (十一)JAVA springboot ssm b2b2c多用户商城系统源码:服务网关Zuul高级篇
  • (一)基于IDEA的JAVA基础10
  • .java 9 找不到符号_java找不到符号
  • .NET 除了用 Task 之外,如何自己写一个可以 await 的对象?
  • .NET 依赖注入和配置系统