%
%  this routine finds trigger points, selects a template, signal averages,
%  then computes the correlation coefficient for SR and VT beats
%
%  trigger on the beats
%
  beats_sr1 = M_and_R_QRS('pt2_endo_sr1.dat',0.95,30,30,0.8);
  beats_sr2 = M_and_R_QRS('pt2_endo_sr2.dat',0.95,30,30,0.8);
  beats_vt1 = M_and_R_QRS('pt2_endo_vt1.dat',0.95,30,30,0.8);
  beats_vt2 = M_and_R_QRS('pt2_endo_vt2.dat',0.95,30,30,0.8);
  beats_vt3 = M_and_R_QRS('pt2_endo_vt3.dat',0.95,30,30,0.8);
  beats_vt4 = M_and_R_QRS('pt2_endo_vt4.dat',0.95,30,30,0.8);
%
%  select a template
%
  [template lefti righti]  = select_template(beats_sr1,2,'pt2_endo_sr1.dat');
%
%  signal average to get a good template
%
  template = signal_average_remove_jitter(beats_sr1,lefti,righti,template,0.9,'pt2_endo_sr1.dat',10);
%
%  now compute the correlation coefficient with each beat in the next
%  sinus rhythm passage
%
  delta = 0;
  sr_ccs = template_match_remove_jitter(beats_sr2,lefti,righti,template,'pt2_endo_sr2.dat',delta);
  vt1_ccs = template_match_remove_jitter(beats_vt1,lefti,righti,template,'pt2_endo_vt1.dat',delta);
  vt2_ccs = template_match_remove_jitter(beats_vt2,lefti,righti,template,'pt2_endo_vt2.dat',delta);
  vt3_ccs = template_match_remove_jitter(beats_vt3,lefti,righti,template,'pt2_endo_vt3.dat',delta);
  vt4_ccs = template_match_remove_jitter(beats_vt4,lefti,righti,template,'pt2_endo_vt4.dat',delta);
 %
 %  plot the results
 %
   figure;
   plot([1:length(sr_ccs)],sr_ccs,'+',[1:length(vt1_ccs)],vt1_ccs,'d', ...
      [1:length(vt2_ccs)],vt2_ccs,'s',[1:length(vt3_ccs)],vt3_ccs,'^', ...
      [1:length(vt4_ccs)],vt4_ccs,'v'); grid;
 
  xlabel('Beats'); ylabel('Correlation Coefficient');
  title('Results for Patient 2');
%
%  save the data
%
  save pt2_sr sr_ccs -ascii
  temp = [vt1_ccs vt2_ccs vt3_ccs vt4_ccs];
  save pt2_vt temp -ascii