title: How can I extract peak values from auto-correlated data in MATLAB?
body: <p>I have information (20,000 frames of data) about an audio track that I have auto-correlated using:</p>
  
  <pre><code>[r,lags] = xcorr(XX,XX,'biased');
  </code></pre>
  
  <p>And it looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/u5gWs.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/u5gWs.jpg" alt="alt text"></a>
  </p>
  
  <p>Which hopefully is so far so good. Ideally I would like to be able to take the frame number that corresponds to the highest part of the second peak. I've read around and tried a load of different methods but I just can't seem to get it to retrieve the information for me.</p>
  
  <p>Would anybody be able to shed some light on what I have to do?</p>
  
  <p>Many thanks!</p>
  
  <hr>
  
  <p><strong>edit1:</strong>
  I have tried using <code>findpeaks</code>, but it doesn't seem to work for me. I'm not sure if that's because I'm using the wrong data or not.</p>
  
  <p><strong>edit2:</strong> I'm currently testing a method to use on just this audio track, but soon I want to expand it so that I can perform this method on a whole directory of files, so I kind of need a script that can detect peaks rather than finding the information myself.</p>
  
  <p><strong>edit3:</strong> My .M file:</p>
  
  <pre><code>[y, fs, nb] = wavread('Three.wav');                 %# Load the signal into variable y
  
  frameWidth = 441;                                   %# 10ms
  numSamples = length(y);                             %# Number of samples in y
  numFrames = floor(numSamples/frameWidth);           %# Number of full frames in y
  energy = zeros(1,numFrames);                        %# Initialize energy
  startSample = zeros(1,numFrames);                   %# Initialize start indices
  endSample = zeros(1,numFrames);                     %# Initialize end indices
  
  for frame = 1:numFrames                             %# Loop over frames
    startSample(frame) = (frame-1)*frameWidth+1;      %# Starting index of frame
    endSample(frame) = frame*frameWidth;              %# Ending index of frame
    frameIndex = startSample(frame):endSample(frame); %# Indices of frame samples
    energy(frame) = sum(y(frameIndex).^2);            %# Calculate frame energy
  end                                                 %# End loop
  
  XX = filtfilt(ones(1,10)/10, 1, energy);            %# Smooths signal
  
  [r,lags] = xcorr(XX,XX,'biased');                   %# Auto-correlates the data
  plot(lags,r), xlabel('lag'), ylabel('xcorr')        %# Plots data
  </code></pre>