function [timelock] = timelockbaseline(cfg, timelock);

% TIMELOCKBASELINE performs baseline correction for ERF and ERP data
%
% Use as
%    [timelock] = timelockbaseline(cfg, timelock)
% where the timelock data comes from TIMELOCKANALYSIS and the
% configuration should contain
%   cfg.baseline     = [begin end]
%   cfg.channel      = cell-array, see CHANNELSELECTION
%
% See also TIMELOCKANALYSIS, FREQBASELINE

% Copyright (C) 2006, Robert Oostenveld
%
% $Log: timelockbaseline.m,v $
% Revision 1.1  2006/03/09 10:34:17  roboos
% ne wimplementation, used for plotting functions (c.f. freqbaseline)
%

% the cfg.blc/blcwindow options are used in preprocessing and in
% timelockanalysis (i.e. in private/preproc), hence make sure that
% they can also be used here for consistency
if isfield(cfg, 'baseline') && (isfield(cfg, 'blc') || isfield(cfg, 'blcwindow'))
  error('conflicting configuration options, you should use cfg.basline');
elseif isfield(cfg, 'blc') && strcmp(cfg.blc, 'no')
  cfg.baseline = 'no';
  cfg = rmfield(cfg, 'blc');
  cfg = rmfield(cfg, 'blcwindow');
elseif isfield(cfg, 'blc') && strcmp(cfg.blc, 'yes')
  cfg.baseline = cfg.blcwindow;
  cfg = rmfield(cfg, 'blc');
  cfg = rmfield(cfg, 'blcwindow');
end

% set the defaults
if ~isfield(cfg, 'baseline'), cfg.baseline = 'no'; end

% for backward compatibility with old data structures
timelock = fixdimord(timelock);

if ischar(cfg.baseline)
  if strcmp(cfg.baseline, 'yes')
    % do correction on the whole time interval
    cfg.baseline = [-inf inf];
  elseif strcmp(cfg.baseline, 'all')
    % do correction on the whole time interval
    cfg.baseline = [-inf inf];
  end
end

if ~(ischar(cfg.baseline) && strcmp(cfg.baseline, 'no'))
  % determine the time interval on which to apply baseline correction
  tbeg = nearest(timelock.time, cfg.baseline(1));
  tend = nearest(timelock.time, cfg.baseline(2));
  % update the configuration
  cfg.baseline(1) = timelock.time(tbeg);
  cfg.baseline(2) = timelock.time(tend);

  if isfield(cfg, 'channel')
    % only apply on selected channels
    cfg.channel = channelselection(cfg.channel, timelock.label);
    chansel = match_str(timelock.label, cfg.channel);
    timelock.avg(chansel,:) = blc(timelock.avg(chansel,:), tbeg:tend);
  else
    % apply on all channels
    timelock.avg = blc(timelock.avg, tbeg:tend);
  end

  if strcmp(timelock.dimord, 'rpt_chan_time')
    fprintf('applying baseline correction on each individual trial\n');
    ntrial = size(timelock.trial,1);
    if isfield(cfg, 'channel')
      % only apply on selected channels
      for i=1:ntrial
        timelock.trial(i,chansel,:) = blc(timelock.trial(i,chansel,:), tbeg:tend);
      end
    else
      % apply on all channels
      for i=1:ntrial
        timelock.trial(i,:,:) = blc(timelock.trial(i,:,:), tbeg:tend);
      end
    end
  elseif strcmp(timelock.dimord, 'subj_chan_time')
    fprintf('applying baseline correction on each individual subject\n');
    nsubj = size(timelock.subj,1);
    if isfield(cfg, 'channel')
      % only apply on selected channels
      for i=1:nsubj
        timelock.subj(i,chansel,:) = blc(timelock.subj(i,chansel,:), tbeg:tend);
      end
    else
      % apply on all channels
      for i=1:nnsubj
        timelock.subj(i,:,:) = blc(timelock.subj(i,:,:), tbeg:tend);
      end
    end
  end

  if isfield(timelock, 'var')
    fprintf('baseline correction invalidates previous variance estimate, removing var\n');
    timelock = rmfield(timelock, 'var');
  end

  if isfield(timelock, 'var')
    fprintf('baseline correction invalidates previous covariance estimate, removing cov\n');
    timelock = rmfield(timelock, 'cov');
  end

end % ~strcmp(cfg.baseline, 'no')

% add version information to the configuration
try
  % get the full name of the function
  cfg.version.name = mfilename('fullpath');
catch
  % required for compatibility with Matlab versions prior to release 13 (6.5)
  [st, i] = dbstack;
  cfg.version.name = st(i);
end
cfg.version.id = '$Id: timelockbaseline.m,v 1.1 2006/03/09 10:34:17 roboos Exp $';
% remember the configuration details of the input data
try, cfg.previous = timelock.cfg; end
% remember the exact configuration details in the output 
timelock.cfg = cfg;

