clear all
close all
clc

% dirname=uigetdir('Please select a folder with NIRsports data'); % please make sure the directory name is same with *.wl1 and *.wl2 files
dirname='C:\Users\Alvin\Desktop\2014-06-05\2014-06-05_003';
filename='2014-06-05_003';

if dirname~=0
    ind=find(dirname==filesep);
%     filename=dirname(ind(end)+1:end);

    wl1=load([dirname filesep 'NIRS-' filename '.wl1' ]); % Location of .wl1 file
    wl2=load([dirname filesep 'NIRS-' filename '.wl2' ]); % Location of .wl2 file
    event=load([dirname filesep 'NIRS-' filename '.evt' ]); % Location of .evt file

%     fs=7.81; % Sampling frequency for your experiment, for old data acquired by NIRStar 12.0
    fs=6.25; % Sampling frequency for your experiment, for new data acquired by NIRStar 13.0
    Ts=1/fs;

    t=[0:Ts:(size(wl1,1)-1)*Ts]'; % Time samples for th duration of recording.
    d=[wl1 wl2]; % Your data matrix from .wl1 and .wl2 files

    SD.Lambda=[760 850]; % The Wavelengths in the same order as you arranged your 'd' matrix above. 

    % These will vary from cap to cap i.e.  from one sensor arrangement to another sensor
    % arrangement. 
    % My arrangement was as follows: 
    % S1 X D1 X S2 X D2 X S3
    % X D3 X D4 X D5 X D6 X
    % D7 X S4 X D8 X S5 X D9
    % Were S and D stand for source and detector + I had arranged my sensor in
    % 2D so that is why you see in source and detector section z coordinates
    % are 0. I HAD 5 SOURCES AND 9 DETECTORS. I f you are using the cap then
    % you might need to figure out how your sensor arragement should be made
    % for the SD.SrcPos and SD.DetPos arrays. 

    % the coordinate can be defined using EIguide coordinates
    SD.SrcPos=[-0.0195		0.0964		0.0659;
               0.0195		0.0964		0.0659;
               -0.0510		0.0623		0.0942;
               0.0510		0.0623		0.0942;
               -0.0577		0.0320		0.1074;
               0.0577		0.0320		0.1074;
               -0.0000		0.0679		0.1098;
               0.0000		0.0352		0.1269]; % The x y z cooridnates of your source positions. x y z coordinates of [S1; S2; S3; S4 ;S5] plz check the diagram above for the coordinates.
    SD.DetPos=[-0.0391		0.0943		0.0487;
               0.0391		0.0943		0.0487;
               -0.0646		0.0472		0.0914;
               0.0646		0.0472		0.0914;
               -0.0482		0.0446		0.1081;
               0.0482		0.0446		0.1081;
               -0.0161		0.0511		0.1205;
               0.0161		0.0511		0.1205]; % The x y z coordinates of your Detector positions. x y z coordinates of [D1; D2; D3; D4 ;D5; D6; D7;D8;D9]
    SD.nSrcs=8; % No. of Sources
    SD.nDets=8; % No. of Detectors


    % SD.MeasList is a (nSrcs . nDets . no of wavelengths) X 4 Matrix i.e. for my application it was (5.9.2) X 4 = 90X4 matrix. 
    % the first column is the index of the source from the SD.SrcPos i.e. I had
    % 9 detectors for each source so for the first column the first nine rows
    % would be one then next nine would be two then 3, 4 and 5 and then they
    % would be repeated for second wavelength. In the second columm we have the
    % indices of the detectors, so I had 9 detectors in total which recorded
    % from each source therefore it has to be 1 to 9 repeated 5 time for each
    % wavelength. 3 rd column is by default one and 4 th column is the index of
    % your wavelengths. For me I had two wavlengths 750 and 860 so for each
    % wavlength I had 5(sources) X 9(detectors) sensors therefore my first 45
    % values are one and the next 45 valus are 2. All this is stored in ml and
    % then assigned to SD. MeasList.
    
    m1=[];
    for w=1:2 % 2 wavelength
        for i=1:SD.nSrcs
            m1=[m1; i*ones(SD.nSrcs,1)]; % the index of sourceNo. , repeat 2 wavelength
        end
    end
    m2=[];
    for w=1:2 % 2 wavelength
        for i=1:SD.nDets
            m2=[m2; [1:SD.nDets]']; % the index of detectorNo. , repeat 2 wavelength
        end
    end
    m3=ones(SD.nSrcs*SD.nDets*2,1); % all ones by default
    m4=[ones(SD.nSrcs*SD.nDets,1);2.*ones(SD.nSrcs*SD.nDets,1)];  % % label of wavelength
    
    ml=[m1 m2 m3 m4];

    % mask the signal for only preserving the good channel based on S-D design
    % first column is for source label; second column is for detector label
    GoodChanMask=[1,1;
                  2,2;
                  3,3;
                  3,5;
                  4,4;
                  4,6;
                  5,3;
                  5,5;
                  6,4;
                  6,6;
                  7,7;
                  7,8;
                  8,7;
                  8,8];
    GoodChanRow=[];
    for i=1:size(GoodChanMask,1)
        GoodChanRow=[GoodChanRow (GoodChanMask(i,1)-1)*SD.nSrcs+GoodChanMask(i,2)];
    end
    GoodChanRow=sort(GoodChanRow);
    GoodChanRow=[GoodChanRow GoodChanRow+SD.nSrcs*SD.nDets];
    
    d=d(:,GoodChanRow);
    ml=ml(GoodChanRow,:);
    
    SD.MeasList=ml;

    % Event file is converted from binary to decimal
    for i=1:size(event,1)
    evt(i,1) = event(i,1);
        %translating binary-markers to decimals
        evt(i,2) = bin2dec([num2str(event(i,9)) num2str(event(i,8)) num2str(event(i,7))...
                            num2str(event(i,6)) num2str(event(i,5)) num2str(event(i,4)) ...
                            num2str(event(i,3)) num2str(event(i,2))]);
    end

    id=find(evt(:,1)==0);
    evt(id,:)=[];

    % I had many different event occuring for m experiemnt but they were
    % categorized into 5 different groups shown below: m.nat, m.high etc. you
    % might have to replace the marker values with your own values here.

    m.walk=find(evt(:,2)==1)';  % walking
    m.cognitive=find(evt(:,2)==2)';  % walking + mental calculation
    m.motor=find(evt(:,2)==3)';  % walking + motor balance

    tevt.walk=evt(m.walk,1);
    tevt.cognitive=evt(m.cognitive,1);
    tevt.motor=evt(m.motor,1);


    % the placemrks function develops a marker specific array such that it is 1 at the
    % time points at which the event or marker has occured and 0 otherwise. I
    % had 5 different groups of event for my experiment you might  want to
    % tweak in this for your own expt. 
    s = zeros(length(t),3); % for 3 conditions
    s(tevt.walk,1)=1;
    s(tevt.cognitive,2)=1;
    s(tevt.motor,3)=1;

    aux=t; % This can be any data of length of your 't' variable. 


    % s variable has my event markers for the homer to be used and finally you
    % can save the data i n.nirs format as shown.
    save([dirname filesep 'NIRS-' filename '.nirs' ],'t', 'd', 'SD', 's', 'ml', 'aux');
   

end