//FFT256c.c FFT implementation calling a C-coded FFT function 

#include <math.h>                          
#define PTS 256			    //# of points for FFT 
#define PI 3.14159265358979
typedef struct {float real,imag;} COMPLEX;
void FFT(COMPLEX *Y, int n);	    //FFT prototype
float iobuffer[PTS];   		    //as input and output buffer
float x1[PTS];         		    //intermediate buffer 
short i;               		    //general purpose index variable                  
short buffercount = 0;     	    //number of new samples in iobuffer         
short flag = 0;        		    //set to 1 by ISR when iobuffer full   
COMPLEX w[PTS];       		    //twiddle constants stored in w 
COMPLEX samples[PTS];  		    //primary working buffer                                              

main()
{
 for (i = 0 ; i<PTS ; i++)	    // set up twiddle constants in w 
  {
   w[i].real = cos(2*PI*i/512.0); //Re component of twiddle constants
   w[i].imag =-sin(2*PI*i/512.0); //Im component of twiddle constants
  }
 comm_intr();			    //init DSK, codec, McBSP

 while(1)				    //infinite loop   
  {
   while (flag == 0) ;            //wait until iobuffer is full 
   flag = 0;                      //reset flag
   for (i = 0 ; i < PTS ; i++)    //swap buffers
    {
     samples[i].real=iobuffer[i]; //buffer with new data
     iobuffer[i] = x1[i];         //processed frame to iobuffer
    } 
   for (i = 0 ; i < PTS ; i++)
     samples[i].imag = 0.0;	    //imag components = 0

   FFT(samples,PTS);              //call function FFT.c

   for (i = 0 ; i < PTS ; i++)    //compute magnitude
    {
     x1[i] = sqrt(samples[i].real*samples[i].real 
	     + samples[i].imag*samples[i].imag)/32;
    }
   x1[0] = 32000.0;               //negative spike(with AD535)for ref
  }                               //end of infinite loop
} 					    //end of main

interrupt void c_int11()	    //ISR
 {
  output_sample((int)(iobuffer[buffercount]));     //out from iobuffer
  iobuffer[buffercount++]=(float)(input_sample()); //input to iobuffer
  if (buffercount >= PTS)				   //if iobuffer full
  {
	buffercount = 0;					   //reinit buffercount
	flag = 1;						   //set flag
  }
}