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. Please read this post for fast tcpip connection. tcpip connection with pnet


Don't want to miss new papers in your field? Check out Stork we developed:

After fNIRS experiment …

Xu Cui
13 sec read

nirs2img, create an image file from NIRS data

I was asked where to get nirs2img script. Here it is. The download link is at the bottom of this article. nirs2img is to...
Xu Cui
51 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 *