Realtime feedback from NIRS recording

2 min read

Hitachi provides a Matlab script (RealtimeOT.m) which can deliver real-time feedback from ETG-4000. This script is installed in the computer (Windows 2000) in the NIRS room. One can modify the script to deliver the real-time signal in the desired format (such as line trace, bar, or color).

To enable realtime feedback, you need to check “LAN OUT Realtime” in the parameter set on ETG-4000 before recording. After you hit the “Ready” button, you need to start RealtimeOT in Matlab command window in the PC. Then click “Start” button in ETG-4000 (or your program send ‘start’ command to ETG-4000).

Here is the script RealtimeOT.m:

% Script RealtimeOT
% Realtime LAN function for OTsystem using MATLAB
%
% Server ETG-4000   :Ex 172.17.101.1
% Client MATLAB     :Ex 172.17.101.2
%
% Reference
% TCP/UDP/IP Toolbox2.0.5
% MATLAB Central>File Exchange>Utilities>Data Import/Export>TCP/UDP/IP Toolbox 2.0.5
% http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=345&objectType=file
%
% Written by S.Kawasaki
% First completed 2006/09/12
%

clear oxy;clear deo;
close all;

%/////////////////////////// Open Port /////////////////////////////
%Connect LAN Port
fid=tcpipmex(1,-1,'172.17.101.1',51027);

if fid<0;
    disp('Fail to connect!');
    tcpipmex(-1,0);
    return;
end;

%Send Command for ETG-4000
tcpipmex(2,fid,sprintf('++Hello ETG-4000\r\n'));

%Receive Command from ETG-4000
str='';
while length(str)<3
    str=[str,tcpipmex(3,fid,80)];
end
disp(str);
disp('Press ETG-4000 START Button!');

%///////////////////////// Figure ///////////////////////////////
plot_ch=1;%Ex: Probe1(4x4Mode 24ch)=1-24ch, Probe2(4x4Mode 24ch)=25-48ch

figure(1);hold on;box on;grid on;
set(1,'doublebuffer','on');
set(gca,'drawmode','fast');
title(['Probe1 CH',num2str(plot_ch)]);
h1=plot(0,0);
h2=plot(0,0);
drawnow;

%//////////////////////// Get ETG-4000 Data ////////////////////////////////
while(1);
    %///////////////////////// Header Size:12 ///////////////////////////////
    buff1=tcpipmex(3,fid,4);
    % Convert datatype and byte order with help of a file.
    fp=fopen('temp','w+');fwrite(fp,buff1);fseek(fp,-4,'cof');
    hsize=fread(fp,4,'int32');
    fclose(fp);delete('temp');

    if hsize==12;%Data is comming!
        %///////////////////////// Data Number ///////////////////////////////
        buff2=tcpipmex(3,fid,4);
        fp=fopen('temp','w+');fwrite(fp,buff2);fseek(fp,-4,'cof');
        num=fread(fp,4,'int32');%Number of Data
        fclose(fp);delete('temp');

        %//////////////////////////// Data Size:428 ////////////////////////////
        buff3=tcpipmex(3,fid,4);
        %fp=fopen('temp','w+');fwrite(fp,buff3);fseek(fp,-4,'cof');
        %dsize=fread(fp,4,'int32');
        %fclose(fp);delete('temp');

        %/////////////////////////// Hb Data ////////////////////////////////
        for ch=1:52;%Oxy
            buff4=tcpipmex(3,fid,4);
            fp=fopen('temp','w+');fwrite(fp,buff4);fseek(fp,-4,'cof');
            oxy(ch,num)=fread(fp,4,'float');
            fclose(fp);delete('temp');
        end;
        for ch=1:52;%Deoxy
            buff5=tcpipmex(3,fid,4);
            fp=fopen('temp','w+');fwrite(fp,buff5);fseek(fp,-4,'cof');
            deo(ch,num)=fread(fp,4,'float');
            fclose(fp);delete('temp');
        end;
        if num>=1;%Plot Oxy&Deoxy
            set(h1,'xdata',[1:num],'ydata',oxy(plot_ch,[1:num]));set(h1,'color','r');
            set(h2,'xdata',[1:num],'ydata',deo(plot_ch,[1:num]));set(h2,'color','b');
            drawnow;
        end;

        %//////////////////////////// Mark ///////////////////////////////
        buff6=tcpipmex(3,fid,2);
        fp = fopen('temp','w+');fwrite(fp,buff6);fseek(fp,-2,'cof');
        mark(num)=fread(fp,2,'int16');
        fclose(fp);delete('temp');

        %/////////////////////////// Time ////////////////////////////////
        buff7=tcpipmex(3,fid,10);
        %time=char(buff7);
    end;
    if tcpipmex(6,fid)==0;break;end;%Push ETG-4000 Stop Button
end;

%///////////////////////// Close Port ////////////////////////////
tcpipmex(-1,0);

clear buff1;clear buff2;clear buff3;clear buff4;clear buff5;clear buff6;clear buff7;
clear ch;clear fid;clear fp;clear num;clear str;clear hsize;
clear h1;clear h2;clear plot_ch;

You may find that this script is slow, i.e. deliver feedback at some time delay. It’s because the tcpip code is slow. Update 2020-03-06 I think the real reason why it’s slow is not because tcpip, instead it’s because it uses file to convert data to int or float. A much better way is to use bread. Please refer to code in tcpip connection with pnet. Please read this post for fast tcpip connection: tcpip connection with pnet



写作助手,把中式英语变成专业英文


Want to receive new post notification? 有新文章通知我

第五十期fNIRS Journal Club通知2024/03/30, 10am 王一晖

早期的 STEM 教育对于以后的学习至关重要。现有研究尚未就STEM教学法达成共识,学生先验知识对基于故事的STEM教学法的影响还有待探讨。来自澳门大学张娟教授团队的王一晖将会分享基于fNIRS超扫描
Wanling Zhu
9 sec read

第四十九期fNIRS Journal Club视频 高倩博士

Youtube: https://youtu.be/a2QlCFZUytA优酷: https://v.youku.com/v_show/id_XNjM3MjMyNjUxMg==.html 竞技比赛中,
Wanling Zhu
12 sec read

第四十九期fNIRS Journal Club通知2024/02/24, 10am 高倩博士

竞技比赛中,特别是需要注意高度集中的射击项目中,如何保持稳定的注意控制对运动员的表现至关重要。近期的一项研究调查了赛前短时正念练习是否对运动员在射击比赛中的注意控制具有提升作用。来自北京体育大学心理学
Wanling Zhu
9 sec read

One Reply to “Realtime feedback from NIRS recording”

  1. Dear Xu,

    thanks a lot for sharing so much stuff!
    We are planning to do neurofeedback studies in our lab and I’m just trying to implement the real-time setup. I have already tested the RealtimeOT.m script and it works. I was wondering, if you also have a basic matlab script for real-time preprocessing and analysis, that you would be willing to share? That would be a great help.

    BW,
    Simon

Leave a Reply

Your email address will not be published. Required fields are marked *