function [subjectPeaks,subjectTime] = ComputePeakValue(dataT,peakDetectLimit,settings)
% For the inputed regression weights, compute the location of the peak with
% a leave-one-participant-out procedure.
% From this group peak time point then extract the individual's regression
% weigth. this is what we output
% in 'peakDetectLimit', we specify the window (in sec) in which to look for
% a peak (positive or negative)
% in 'settings', we read in the settings of upsampling etc.

% Translate the peak detection limit (window) from sec to samples
timepoints = floor(settings.pseudosamples/settings.window*peakDetectLimit(1)):1:ceil(settings.pseudosamples/settings.window*peakDetectLimit(2));

% get data in easier format
dat = dataT.v(:,timepoints);
nsub = size(dat,1);

% Loop over subjects, each time extract their peak value
subjectPeaks = nan(nsub,1);
subjectTime  = nan(nsub,1);
for is = 1:nsub
    % get the index of all but the current subject
    ind = setdiff(1:nsub,is);
    % get the time point of the peak
    mdat= nanmean(dat(ind,:));
    peakTime = abs(mdat) == max(abs(mdat));
    
    subjectTime(is) = find(peakTime);
    subjectPeaks(is) = dat(is,peakTime);
end

%
% figure('color','w')
% errorbar(nanmean(dat),nanstd(dat)./sqrt(nsub-1))
% hold on 
% for il = 1:nsub
%     line([subjectTime(il) subjectTime(il)],[0 0.05],'Color','k')
% end

