title: cross correlation C program not working
body: <p>I am trying to find cross correlation. So, I decide to make a sample program in which I copy data of 64 samples in an array (arr1[]), now I shift this data with 10 samples and store it in another array (arr3[]). So, I got two sample array which are similar but have lag of ten samples. Then I multiply each sample of arr1[] and arr3[] and add all 64 results. I shift arr3[] 64 times and each time I multiply arr1[[ and arr3[] and add the values. I store the result values in csv file so that I can check the graph finally.Here is my program:-</p>
  
  <pre><code>    #include "stdafx.h"
      #include&lt;stdio.h&gt;
      #include&lt;Windows.h&gt;
      int _tmain()
      {
          //taking data of 64 samples.
          float arr1[64] = { 0.98822 ,0.98822 ,0.98822 ,0.98822 ,0.98822 ,0.98822 ,1.00391, 0.98822 ,0.98822 ,0.972534 ,1.00391 ,1.01959 ,1.00391, 0.956848, 0.98822, 1.00391 ,1.06665 ,1.22351 ,1.03528 ,0.925476 ,0.862732 ,0.643127 ,0.611755 ,0.956848, 1.09802, 1.12939 ,1.09802 ,1.03528 ,0.98822, 0.98822 ,0.98822 ,1.00391 ,0.98822 ,1.00391 ,0.98822 ,1.00391 ,0.98822 ,0.98822 ,0.98822, 1.00391 ,0.98822 ,1.00391 ,1.00391 ,0.98822 ,1.00391 ,0.98822 ,0.98822 ,0.98822 ,0.972534 ,0.972534 ,0.98822 ,0.972534 ,0.98822 ,0.98822 ,1.00391 ,0.98822 ,1.00391 ,0.98822 ,1.00391 ,0.972534 ,0.98822 ,0.98822 ,0.972534 ,0.98822};
  
  float arr3[64];
  
  
  float res,result[64];
  int count,tab,com;
  //copying arr1[] into arr3[] but with 10 samples lag
          for(count = 0;count&lt;54;count++)
          {
              arr3[count] = arr1[count+10];
          }
          for(count=0;count&lt;10;count++)
          {
          arr3[count+54] = arr1[count];
          }
  //clearing result[] so that fresh result can be store here
          for(count=0;count&lt;64;count++)
          {
  
          result[count]=0;
          }
  // declaring file pointer to store arr1[] in csv file
          FILE * qFile;
             qFile = fopen ("ar1.csv","w");
  // storing arr1[] into csc file.
             for(tab=0;tab&lt;64;tab++)
             {
                 fprintf (qFile, "%f\n",arr1[tab]);
             }
  // declaring file pointer to store arr3[] in csv file  
             FILE * rFile;
             rFile = fopen ("ar3.csv","w");
  // storing arr3[] into csc file.
             for(tab=0;tab&lt;64;tab++)
             {
                 fprintf (rFile, "%f\n",arr3[tab]);
             }
  // declaring file pointer to store arr3[] in csv file
             FILE * pFile;
             pFile = fopen ("myfile.csv","w");
  
  // applying cross correlation and storing it in csv file
             for(tab=0;tab&lt;64;tab++)
             {
                 fprintf (pFile, "%f\n",arr3[tab]);
                  for(count=0;count&lt;64;count++)
                  {
  
  
                      if(count&lt;(64-tab))
                      {
                      // multiplying arr1[] sample with arr3[] sample
                      res= ((arr1[count]-1)) * ((arr3[count+tab]-1));
                      // adding result in result[]
                      result[tab] += res;
                      }
                      else
                      {
                       result[tab] += res;
                      }
  
                  }
                  result[tab]= result[tab];
                  // storing result in csv file
                      fprintf (pFile, "%f\n",result[tab]);
  
             }
  
  
          fclose(pFile);fclose(qFile);fclose(rFile);
          system("pause");
          return 0;
      }
  </code></pre>
  
  <p>Here my final graph:-</p>
  
  <p><img src="https://i.stack.imgur.com/StTjH.png" alt="enter image description here"></p>
  
  <p>here series 1 is arr1[], series 2 is arr[3] and series 3 is final output.
  So, please tell me why I am getting this random graph of output.</p>
  
  <p>After removing "fprintf(pFile,"%f\n",arr3[tab]);" as user3386109 suggested, I got this result:- <img src="https://i.stack.imgur.com/HW6rH.png" alt="enter image description here"></p>
  
  <p>Here is the algorithm of my code:-</p>
  
  <p>Step1:- Subtract each sample of both signals(arr1[] &amp; arr3[]) by 1.</p>
  
  <p>Step2:- Take all 64 samples from arr1[] &amp; arr3[] then multiply each sample of arr1[] with corresponding sample of arr3[].</p>
  
  <p>Step3:- Add all 64 result sample ( here result means result of multiplication between samples of arr1[] &amp; arr3[] ) and consider it as final sample of cross correlation </p>
  
  <p>Step4:- shift arr3[] by 1 position on left side and repeat above steps.</p>
  
  <p>Step5:- repeat step4 for 64 times.  </p>
  
  <p>Is it ok now? (I am not sure..)</p>