/***********************************************************/
/* autocorr.c                                              */
/* Copyright August 2004 by Matt Kleffner                  */
/* under the Creative Commons Attribution License          */
/*                                                         */
/* Simple, unoptimized autocorrelation function            */
/* for ECE 420 (TMS320C54X series)                         */
/*                                                         */
/* #defines expected in lab4b.h                            */
/*    L: length of data in autocorr_in buffer              */
/*    N: length of data in autocorr_out buffer             */
/* logL: log base 2 of L (used for scaling output)         */
/*    M: Largest positive lag of autocorrelation desired   */
/*       (must be < L and < N/2)                           */
/*                                                         */
/* 16-bit-limited input/output (must be defined elsewhere) */
/*  autocorr_in: buffer for input data  (L pts)            */
/* autocorr_out: buffer for output data (N pts)            */
/*               N must be >= 2*M+1                        */
/*               assumed to be full of zeros at start      */
/*               output in zero-phase form                 */
/***********************************************************/

#include "lab4b.h"

extern int autocorr_in[L];
extern int autocorr_out[N];

void autocorr(void)
{
   int i,j,temp;

   for(i=0;i<=M;++i)
   {
      long int sum=0;
      for(j=0;j<(L-i);++j)
      {
         temp = ((long int)autocorr_in[j] * (long int)autocorr_in[j+i]) >> 15;
         sum += temp;
      }
      autocorr_out[i]=(int)(sum >> logL);
   }

   /* Copy values for negative indeces at end of buffer */
   for(i=1,j=N-1;i<=M;++i,--j)
   {  autocorr_out[j]=autocorr_out[i]; }
}
