function dop=dopIndexFilters(dop)
% dopOSCCI Mark II: dopIndexFilter~ 17-02-2011 (last edit)
%
% create additional filters in dop.filter variable which examine activation
% windows around peaks and such.
%
% - left only
% - right only
% - lr mean
% - lr diff
%
% indices around
% - absolute maximum value
% - absolute maximum value within poi (lr diff = LI)
%
% these must be based upon selections of epochs
%

% -------------------------------------------------------------------------
%     '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
%

try
    if ~dop.sum.skip && ~dop.sum.quit
        dop.indFilt.translation='Index Filters: select periods of time based upon min or max averages';
        dop.indFilt.filtLabels={'AbsMaxPOI','AbsMax','MaxPOI','Max','AbsMinPOI','AbsMin','Min','MinPOI'}; % filter labels
        dop.indFilt.dataLabels={'left','right','dif','avg'}; % data labels
        dop.indFilt.actWin=dop.def.actWin*.5*dop.def.downHertz; % half of activation window
        for i=1:numel(dop.stack.evNames);
            if dop.def.evType(i); dop.tmp.en=dop.stack.evNames{i}; % event name
                for ii=1:numel(dop.scrn.labels) % for each of the epoch selections
                    if dop.scrn.saveData(ii); dop.tmp.epSel=dop.scrn.labels{ii}; % if saving, use the label
                        dop.tmp.scrn=logical(dop.scrn.(dop.tmp.en)(:,ii));
                        for j=1:numel(dop.indFilt.dataLabels); dop.tmp.ch=dop.indFilt.dataLabels{j}; % for each 'channel'
                            % select the data we want
                            dop.tmp.skip=0;
                            switch dop.tmp.ch
                                case {'left','right'}
                                    switch size(dop.stack.(dop.tmp.en)(dop.tmp.scrn,:,j),1)
                                        case 0
                                            dop.tmp.skip=1;
                                        case 1
                                            dop.tmp.data=dop.stack.(dop.tmp.en)(dop.tmp.scrn,:,j);
                                        otherwise
                                            dop.tmp.data=mean(dop.stack.(dop.tmp.en)(dop.tmp.scrn,:,j));
                                    end
                                case {'avg','dif'}
                                    switch size(dop.(dop.tmp.ch).(dop.tmp.en)(dop.tmp.scrn,:),1)
                                        case 0
                                            dop.tmp.skip=1;
                                        case 1
                                            dop.tmp.data=dop.(dop.tmp.ch).(dop.tmp.en)(dop.tmp.scrn,:);
                                        otherwise
                                            dop.tmp.data=mean(dop.(dop.tmp.ch).(dop.tmp.en)(dop.tmp.scrn,:));
                                    end
                            end
                            if ~dop.tmp.skip
                                for jj=1:numel(dop.indFilt.filtLabels); dop.tmp.fn=dop.indFilt.filtLabels{jj}; % filter name
                                    switch dop.tmp.fn
                                        case 'Min'
                                            [dop.tmp.value dop.tmp.ind]=min(dop.tmp.data);
                                        case 'Max'
                                            [dop.tmp.value dop.tmp.ind]=max(dop.tmp.data);
                                        case 'MinPOI'
                                            [dop.tmp.value dop.tmp.ind]=...
                                                min(dop.tmp.data(:,dop.filter.(dop.tmp.en).poi));
                                            dop.tmp.ind=dop.tmp.ind+find(dop.filter.(dop.tmp.en).poi,1,'first');
                                        case 'MaxPOI'
                                            [dop.tmp.value dop.tmp.ind]=...
                                                max(dop.tmp.data(:,dop.filter.(dop.tmp.en).poi));
                                            dop.tmp.ind=dop.tmp.ind+find(dop.filter.(dop.tmp.en).poi,1,'first');
                                        case 'AbsMin'
                                            [dop.tmp.value dop.tmp.ind]=min(abs(dop.tmp.data));
                                        case 'AbsMinPOI'
                                            [dop.tmp.value dop.tmp.ind]=...
                                                min(abs(dop.tmp.data(:,dop.filter.(dop.tmp.en).poi)));
                                            dop.tmp.ind=dop.tmp.ind+find(dop.filter.(dop.tmp.en).poi,1,'first');
                                        case 'AbsMax'
                                            [dop.tmp.value dop.tmp.ind]=max(abs(dop.tmp.data));
                                        case 'AbsMaxPOI'
                                            [dop.tmp.value dop.tmp.ind]=...
                                                max(abs(dop.tmp.data(:,dop.filter.(dop.tmp.en).poi)));
                                            dop.tmp.ind=dop.tmp.ind+find(dop.filter.(dop.tmp.en).poi,1,'first');
                                    end
                                    dop.tmp.filter=zeros(1,length(dop.filter.(dop.tmp.en).epoch)); % empty filter
                                    % record the timing of the indicies
                                    dop.filter.(dop.tmp.en).(dop.tmp.ch).(dop.tmp.fn).(dop.tmp.epSel).sample=ones(1,1,4)*dop.tmp.ind;
                                    dop.filter.(dop.tmp.en).(dop.tmp.ch).(dop.tmp.fn).(dop.tmp.epSel).sec=ones(1,1,4)*dop.tmp.ind*(1/dop.def.downHertz);
                                    % make sure the index is within data limits
                                    if dop.tmp.ind-dop.indFilt.actWin<1
                                        dop.tmp.ind=1+dop.indFilt.actWin;
                                    elseif dop.tmp.ind+dop.indFilt.actWin>length(dop.tmp.filter)
                                        dop.tmp.ind=length(dop.tmp.filter)-dop.indFilt.actWin;
                                    end
                                    dop.tmp.filter(dop.tmp.ind-dop.indFilt.actWin:dop.tmp.ind+dop.indFilt.actWin)=1;
                                    
                                    dop.filter.(dop.tmp.en).(dop.tmp.ch).(dop.tmp.fn).(dop.tmp.epSel).filter=logical(dop.tmp.filter);
                                end
                            end
                        end
                    end
                end
            end
        end
    end
catch err
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename));rethrow(err);
end




