function varargout=dopEventAverage(eventData)
% dopOSCCI Mark II: dopEventAverage ~ 9-9-2010 (last edit)
%
% calculate event data average channel = mean(left, right) - use this to
% examine peak activation if desired.
%
% 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 Average = mean(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);
    
    ea.translation='event average';
    ea.structAbbreviations='ea';
    ea.data=eventData; clear eventData
    
    if isstruct(ea.data) % most likely a stack of data from dopEventStack
        ea.fns=ea.data.evNames; % prior 29-10-10 fieldnames(ea.data); % field names
        for i=1:length(ea.fns)
            ea.fn=ea.fns{i};
            ea.out.(ea.fn)=mean(ea.data.(ea.fn),3);
        end
    else % but just in case it's just a 3 dimensional array...
        ea.out.(ea.fn)=mean(ea.data,3);
    end
    
    %% set ouputs
    varargout{1}=ea.out;
    fprintf('\t%s\n','Done');
catch err
    save(dopOSCCIdebug(mfilename));rethrow(err);
    
end

clear ea inputs

