function dop=dopEventBaseCorrect(dop)
% dopOSCCI Mark II: dopEventBaseCorrect ~ 11-11-2010 (last edit)
%
% corrects the event data epoch by adjusting for average activation in the
% baseline period.
%
% uses dop.filter info to adjust the dop.stack variable - the dop.stack
% variable will be altered.
%
% Stim Only Event Type:
% baseline correction for stim only event type (see dopTaskDef) uses the
% base match (dop.def.evMath) variable to determine the info used for the
% baseline correction.
%

% -------------------------------------------------------------------------
%     'dopOSCCI' summarises functional transcranial Doppler ultrasonography
%     (fTCD) data.  The primary function of the software is to assess the
%     hemispheric lateralization of cognitive function.
%
%     Copyright (C) 2011 the Chancellor, Masters and Scholars of the
%     University of Oxford.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/> 
%     or write to the Free Software Foundation, Inc., 51 Franklin Street,
%     Fifth Floor, Boston, MA 02110-1301, USA.
%
%     Authors: Nic Badcock, Georgina Holt, Anneka Holden, & Dorothy Bishop.
%       contact: nicholas.badcock@gmail.com
%
%     Type 'dopOSCCIlicense' for license details or see dopOSCCIlicense.txt
%
fprintf('\n%s\n\t%s\n',['Running ',mfilename],...
    'Baseline correcting Event Data:');
try
    dop.bc.translation='baseline correction';
    dop.bc.evNames=dop.stack.evNames; % prior 29-10-10 fieldnames(dop.stack);
    dop.bc.done=[]; % keep a record on events corrected as if base type event comes before stim only, need to do stim first
    for i=1:length(dop.bc.evNames)
        dop.bc.evn=i; % event number
        dop.bc.en=dop.bc.evNames{dop.bc.evn}; % event name
        fprintf('\t\t%s',dop.bc.en);
        % subtract mean baseline for each event epoch from each event epoch
        % this uses an array operation which is quite tidy.
        switch dop.def.evType(i)
            case 0 % base - must do matching poi only first!!!
                for j=1:length(dop.def.evMatch)
                    if dop.def.evMatch(j)==i
                        dop.bc.sn=dop.bc.evNames{j}; % base name
                        dop.bc.done(end+1)=j;
                        fprintf('\t%s',['(Correcting ''',dop.bc.sn,''' first)']);
                        dop.stack.(dop.bc.sn)=bsxfun(@minus,dop.stack.(dop.bc.sn),...
                            mean(dop.stack.(dop.bc.en)(:,dop.filter.(dop.bc.en).base,:),2));
                    end
                end
                dop.stack.(dop.bc.en)=bsxfun(@minus,dop.stack.(dop.bc.en),...
                    mean(dop.stack.(dop.bc.en)(:,dop.filter.(dop.bc.en).base,:),2));
            case 1 % stimulus only event marker - requires different base
                if sum(i==dop.bc.done)
                    fprintf('\t%s',('already corrected'));
                else
                    dop.bc.bn=dop.bc.evNames{dop.def.evMatch(i)}; % base name
                    fprintf('\t%s',['(Using ''',dop.bc.bn,''' event for baseline)']);
                    dop.bc.base_mean = mean(dop.stack.(dop.bc.bn)(:,dop.filter.(dop.bc.bn).base,:),2);
                    if length(dop.bc.done)>= dop.def.evMatch(i) && dop.bc.done(dop.def.evMatch(i)) % base event already corrected, dop.bc.base_mean = 0
                        dop.bc.base_mean = dop.bc.base_match{i};
                    end
%                     dop.stack.(dop.bc.en)=bsxfun(@minus,dop.stack.(dop.bc.en),...
%                         mean(dop.stack.(dop.bc.bn)(:,dop.filter.(dop.bc.bn).base,:),2));
                    dop.stack.(dop.bc.en)=bsxfun(@minus,dop.stack.(dop.bc.en),...
                        dop.bc.base_mean);
                end
            otherwise % base or regular
                if sum(dop.def.evMatch==i) % this event is used as a match
                    dop.bc.base_match{dop.def.evMatch==i}=mean(dop.stack.(dop.bc.en)(:,dop.filter.(dop.bc.en).base,:),2);
                end
                dop.stack.(dop.bc.en)=bsxfun(@minus,dop.stack.(dop.bc.en),...
                    mean(dop.stack.(dop.bc.en)(:,dop.filter.(dop.bc.en).base,:),2));
        end
        dop.bc.done(end+1) = i;
        fprintf('\n');
    end
    fprintf('\n\t%s\n','Done');
    %% remove the bc field
    % type of temporary data storage
    dop=rmfield(dop,'bc');
catch err
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename));rethrow(err);
end


