%
%  this routine computes the (normalized) autocorrelation of a template
%  with a signal. 
%
%  template is the template we are to use
%  signal is the signal we are comparing the template to
%
function Rxx = normalized_auto_correlation(template,name,start)
%
%  load the signal file
%
  signal = load(name);
  signal = signal(start:end);
%
%  get the length of the signal and the length of the template
%
  Nt = length(template)-1;
  Nlag = 100;
%
%  for k = 1:Nlag
%        num = sum(template.*??);
%        den = 1/??;
%        Rxx(k) = num/den;
%  end;
%
%  Normalize by Rxx(1) (which is really Rxx(0))
%
    scale = 1
%
%  set the sampling rate
%
  fs = 100;
%
 % figure; 
 orient tall;
  
  subplot(4,1,1); plot([0:Nt]/fs,template); grid; xlabel('Time (sec)'); axis('tight'); ylabel('Template'); 
  title([' Autocorrelation using file ' name ', starting at ' , num2str(start)]);
  
  subplot(4,1,2); plot([0:Nt+Nlag-1]/fs, signal(1:Nt+Nlag)); grid; xlabel('Time (sec)'); axis('tight');
  ylabel('Signal for R_{xx}');
  
% subplot(4,1,3); plot([0:Nlag-1]/fs,Rxx*scale); grid;
%  ylabel('\rho_{xx} '); xlabel('Delay in Seconds'); axis([0 0.5 -0.4 1.0]);
%
%  Now try and find the power spectrum
%
%  [Xmag,Xphase,df,frq] = baf_fft;
%
%  XdB = 20*log10(Xmag);
%  subplot(4,1,4); plot(frq,XdB); grid; axis([0 max(frq) min(XdB) max(XdB)]);
%     ylabel('S_{xx} (in dB)');
%     xlabel('Frequency in Hz ');