%
%  this routine signal averages to determine a template
%  this version slides the beat to the left and right by delta samples to try and find the
%  best alignment
%
function ccs = template_match_remove_jitter(trigger,lefti,righti,template,name,delta);
%
  s = load(name);
%
%  for now, format the data
%
  temp = s';
  data = temp(:);
  N = length(data);
%
%  loop over all beats, and compute the correlation coefficient
%  don't use the beat used to make the template
%
  ccs = [];
  for i = 1:length(trigger);
      if (((trigger(i)-lefti-delta)>0) && (trigger(i)+righti+delta < N))
          %
          % now slide the template across the beat to try and determine the
          % best fit
          %
          all_cc = [];
          for kk = -delta:delta
              x = data(trigger(i)+kk-lefti:trigger(i)+kk+righti);
              y = template;
              xx = x - mean(x);
              yy = y - mean(y);
              all_cc = [all_cc sum(xx.*yy)/sqrt(sum(yy.^2)*sum(xx.^2))];
              cc = max(all_cc);
          end;
          ccs = [ccs cc];
      end;
  end;
