title: Matlab: Aligning data by using cross covariance
body: <p>I want to get the offset in samples between two datasets in Matlab (getting them synced in time), a quite common issue. Therefore I use the cross correlation function xcorr or the cross covariance function xcov (both provide similar results in most cases for this purpose). With artificial data it works fine, but I struggle with "real" data, even though it should be pretty much the same. Matlab always says the offset would be zero. I'm using this simple piece of code:</p>
  
  <pre><code>[crossCorr] = xcov(b, c);
  [~, peakIndex] = max(crossCorr())
  offset = peakIndex - length(b)
  </code></pre>
  
  <p><em>I've posted a fully runable example m-file with a downsampled data excerpt on pastebin:
  <a href="http://pastebin.com/2JuRKhai" rel="nofollow">Code with data on pastebin</a></em></p>
  
  <p>EDIT: The downsampled excerpt seems to be not fully suitable for evaluating the effect. <a href="http://en.file-upload.net/download-8639254/variables.mat.html" rel="nofollow">Here's a much larger sample with the original frequency, pease use this one instead</a>. Unfortunately it was too big for pastebin.</p>
  
  <p>As the plot shows it should be no problem at all to get the offset via cross covariance. I also tried to scale the data nicer in order to avoid numerical problems, but that didn't change anything at all.</p>
  
  <p>Would be great if someone could tell me my mistake.</p>