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/12/07, 10am 王硕教授团队

理解噪音中的言语对老年听力损失患者来说是一个重大挑战。来自首都医科大学附属北京同仁医院耳鼻咽喉科研究所王硕教授团队的助理研究员王松建将为大家介绍他们采用同步EEG-fNIRS技术,从神经与血流动力学两
Wanling Zhu
10 sec read

第五十七期fNIRS Journal Club视频 王欣悦博士

Youtube: https://youtu.be/vyo-kECC2Ps 优酷:https://v.youku.com/v_show/id_XNjQzNTA0ODIwMA==.html 肢体语言——
Wanling Zhu
20 sec read

第五十七期fNIRS Journal Club通知2024/11/02, 10am 王欣悦博士

肢体语言——例如人际距离、眼神、手势等,如何影响我们的交流,是一个有趣的谜题。它们是优雅而神秘的代码,无本可依、无人知晓,却又无人不懂。来自南京师范大学的王欣悦博士将分享如何通过fNIRS超扫描技术,
Wanling Zhu
16 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 *