function varargout=dopEventDifference(eventData)
% dopOSCCI Mark II: dopEventDifference ~ 2-12-2010 (last edit)
%
% calculate event data difference channel = left - right
%
% expects the inputted eventData to be an X (epoch) by Y (samples) by Z = 2
% (doppler channels - left right) array.
%
% copes with it being within a structure for multiple events:
% dop.stack.evenName1: [10x501x2 double], dop.stack.evenName2: [10x501x2
% double], etc.
% OR can just be the data itself, not in a structure.
%

% -------------------------------------------------------------------------
%     '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',['Running ',mfilename],...
    'Calculating Event Difference (left - right):');
try
    %     inputs.varargin=varargin;
    %     inputs.defaults=struct('epoch',[-10 10],... % epoch around event marker in seconds
    %         'sample',25,... % sample rate of input (to figure out timing)
    %         'eventLabels','mark1',...); % names of event channels
    %         'evChs',0,... % doppler external channel numbers
    %         'fillMethod','mirror'); % method to fill empty data with
    %
    %     ed=setGetInputsStruct(inputs);
    
    ed.translation='event difference';
    ed.structAbbreviations='ed';
    ed.data=eventData; clear eventData
    
    if isstruct(ed.data) % most likely a stack of data from dopEventStack
        ed.fns=ed.data.evNames; % prior 29-10-10 fieldnames(ed.data); % field names
        for i=1:length(ed.fns)
            ed.fn=ed.fns{i};
            ed.out.(ed.fn)=diff(flipdim(ed.data.(ed.fn),3),1,3);
        end
    else % but just in case it's just a 3 dimensional array...
        ed.out.(ed.fn)=diff(ed.data,1,3)*-1;
    end
    
    %% set ouputs
    varargout{1}=ed.out;
    fprintf('\t%s\n','Done');
catch err
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename));rethrow(err);
end

clear ed inputs

