title: Implementing iterative autocorrelation process in C++ using for loops
body: <p>I am implementing pitch tracking using an autocorrelation method in C++ but I am struggling to write the actual line of code which performs the autocorrelation.</p>
  
  <p>I have an array containing a certain number (<em>'values'</em>) of amplitude values of a pre-recorded signal, and I am performing the autocorrelation function on a set number (N) of these values. </p>
  
  <p>In order to perform the autocorrelation I have taken the original array and reversed it so that point 0 = point N, point 1 = point N-1 etc, this array is called <em>revarray</em></p>
  
  <p>Here is what I want to do mathematically: </p>
  
  <pre><code>(array[0] * revarray[0])
  (array[0] * revarray[1]) + (array[1] * revarray[0])
  (array[0] * revarray[2]) + (array[1] * revarray[1]) + (array[2] * revarray[0])
  (array[0] * revarray[3]) + (array[1] * revarray[2]) + (array[2] * revarray[1]) + (array[3] * revarray[0])
  </code></pre>
  
  <p>...and so on. This will be repeated for array[900]->array[1799] etc until autocorrelation has been performed on all of the samples in the array.</p>
  
  <p>The number of times the autocorrelation is carried out is: </p>
  
  <p>values / N = measurements </p>
  
  <p>Here is the relevent section of my code so far</p>
  
  <pre><code>for (k = 0; k = measurements; ++k){
      for (i = k*(N - 1), j = k*N; i &gt;= 0; i--, j++){
          revarray[j] = array[i];
          for (a = k*N; a = k*(N - 1); ++a){
              autocor[a]=0;
              for (b = k*N; b = k*(N - 1); ++b){
                  autocor[a] += //**Here is where I'm confused**//
              }
          }
      }
  }
  </code></pre>
  
  <p>I know that I want to keep iteratively adding new values to autocor[a], but my problem is that the value that needs to be added to will keep changing. I've tried using an increasing count like so:</p>
  
  <pre><code>for (i = (k*N); i = k*(N-1); ++i){
      autocor[i] += array[i] * revarray[i-1]
  }
  </code></pre>
  
  <p>But I clearly know this <em>won't</em> work as when the new value is added to the previous autocor[i] this previous value will be incorrect, and when i=0 it will be impossible to calculate using revarray[i-1]</p>
  
  <p>Any suggestions? Been struggling with this for a while now. I managed to get it working on just a single array (not taking N samples at a time) as seen <a href="https://stackoverflow.com/questions/41809661/implementing-brute-force-autocorrelation-method-using-c">here</a> but I think using the inverted array is a much more efficient approach, I'm just struggling to implement the autocorrelation  by taking sections of the entire signal.</p>