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

第十一期 fNIRS Journal Club 通知 2020/8/29,10am

北京时间2020年8月29日周六上午10点, 深圳大学成晓君助理教授将为大家讲解她去年发表的一篇用近红外超扫描揭示人际协调的神经机制的文章。同时,她还会和大家介绍如何用Granger causality分析两个信号的因果关系。欢迎大家参加并参与讨论。 她要讲的文献如下: Cheng, Pan, Hu, Hu (2019) Coordination Elicits Synchronous Brain Activity Between Co-actors: Frequency Ratio Matters Frontiers in neuroscience 13() 1071 Abstract: People...
Xu Cui
48 sec read

第十期 fNIRS Journal Club 视频

在2020/7/25日, 北京航空航天大学的汪待发副教授讲解了他发表的一篇BCI文章。视频如下: Youtube:https://youtu.be/gAQQrmbWSOcYouku:https://v.youku.com/v_show/id_XNDc2OTAxNzkzMg==.html
Xu Cui
4 sec read

第十期 fNIRS Journal Club 通知 2020/7/25,10am

北京时间2020年7月25日周六上午10点,北京航空航天大学的汪待发副教授,博士生导师,将为大家讲解他们组去年发表的一篇脑机交互(BCI)的近红外文章。欢迎大家参加并参与讨论。 他要讲的文献如下:Y. Zheng,D. Zhang, L. Wang, Y. Wang, H. Deng, S. Zhang, D. Li, D. Wang, “Resting-State-Based Spatial Filtering for an fNIRS-Based Motor Imagery...
Xu Cui
1 min 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 *