title: calculate the crosscorrelation of two vectors more efficiently
body: <p>I recently translated some python code into c#. The c# function is working fine and results in the same output as the python one. However, it takes about 15 times longer. This is mostly due to one of my functions which calculates the cross-correlation of two vectors and takes much longer than numpy's correlate(a,b,&quot;full&quot;)</p>
  <p>I wrote (assume a and b are of the same length):</p>
  <pre><code>double[] CrossCorr(double[] a, double[] b)
      {
          int l = a.Length;
          int jmin, jmax, index;
          index = 0;
          int lmax = 2*l - 1;
          double[] z = new double[lmax];
          for (int i = 0; i &lt; lmax; i++)
          {
              if (i &gt;= l)
              {
                  jmin = i - l + 1;
                  jmax = l - 1;
              }
              else
              {
                  jmax = i;
                  jmin = 0;
              }
              for (int j = jmin; j &lt;= jmax; j++)
              {
                  index = l - i + j - 1;
                  z[i] += (a[j] * b[index]);
              }
          }
          return z;
      }
  </code></pre>
  <p>Another attempt was to use the known equation:</p>
  <pre><code>corr(a, b) = ifft(fft(a_and_zeros) * conj(fft(b_and_zeros)))
  </code></pre>
  <p>which resulted in this function (using Math.Net):</p>
  <pre><code>double[] Corrcorfour(double[] a, double[] b)
      {
          //Fourier transformation
          Complex[] distrib_Pr_Com = new Complex[a.Count()];
          Complex[] distrib_Pi_noise_Com = new Complex[b.Count()];
          for (int k = 0; k &lt; x_count; k++)
          {
              distrib_Pr_Com[k] = (Complex)a[k];
              distrib_Pi_noise_Com[k] = (Complex)b[k];
          }
          MathNet.Numerics.IntegralTransforms.Fourier.Forward(distrib_Pi_noise_Com);
          MathNet.Numerics.IntegralTransforms.Fourier.Forward(distrib_Pr_Com);
  
          //complex conj
          for (int k = 0; k &lt; distrib_Pi_noise_Com.Count(); k++)
          {
              distrib_Pi_noise_Com[k] = Complex.Conjugate(distrib_Pi_noise_Com[k]);
          }
  
  
          //multiply results
          Complex[] test = new Complex[distrib_Pr_Com.Count()];
          for (int k = 0; k &lt; distrib_Pr_Com.Count(); k++)
          {
              test[k] = Complex.Multiply(distrib_Pr_Com[k], distrib_Pi_noise_Com[k]);
          }
          //transformierenback
          MathNet.Numerics.IntegralTransforms.Fourier.Inverse(test);
  
          //transform to double
          double[] finish = new double[test.Count()];
          for (int k = 0; k &lt; test.Count(); k++)
          {
              finish[k] = test[k].Real;
  
          }
  
          return finish;
      }
  </code></pre>
  <p>However, this function somehow throws out an array of different lengths and with different results (so it's wrong). It is also only 2 times quicker than the first function, which would still not be enough.</p>
  <p>I tried to look up some other people functions but they didn't seem to do it much differently from either of the two. is there a shortcut I'm not seeing, or an error?</p>
  <p>I found a few similar questions but I didn't find a satisfying answer in any of them.</p>