//SweepDE.c  Generates a sweeping sinusoid using a difference equation

#include <math.h>
#define  two_pi    (2*3.1415926) 	 //2*pi
#define  two_14           16384	 //2^14
#define  T             0.000125 	 //sample period = 1/Fs
#define  MIN_FREQ	          500      //initial frequency of sweep
#define  MAX_FREQ	         3500	 //max frequency of sweep
#define  STEP_FREQ           10	 //step frequency
#define  SWEEP_PERIOD       200	 //lasting time at one frequency
short	   y0  = 0;				 //initial output
short    y_1 = -6270;			 //y(-1)=-sinwT(scaled)  f=500 Hz
short    y_2 = -11585;			 //y(-2_=-sin2wT(scaled) f=500 Hz
short    A = 30274;			 //A = 2*coswT scaled by 2^14
short	   freq = MIN_FREQ; 		 //current frequency
short    sweep_count = 0; 		 //counter for lasting time
void     coeff_gen(short);		 //function prototype generate coeff

interrupt void c_int11()  		 //ISR
{
 sweep_count++;				 //inc lasting time at one frequency
 if(sweep_count >= SWEEP_PERIOD)     //lasting time reaches max duration
  {
   if(freq >= MAX_FREQ)              //if the current frequency is max
	freq = MIN_FREQ; 	        	 //reinit to initial frequency 
   else
	freq = freq + STEP_FREQ;  	 //incr to next higher frequency  		
	
   coeff_gen(freq);			 //function for new set of coeff
   sweep_count = 0;			 //reset counter for lasting time	
  }
 y0=(((int)A * (int)y_1)>>14) - y_2; //y(n) = A*y(n-1) - y(n-2)
 y_2 = y_1;              	       //update y(n-2)
 y_1 = y0;					 //update y(n-1)
 output_sample(y0);		       //output result
}

void coeff_gen(short freq)	   	 //calculate new set of coeff
{
 float w;				       //angular frequency

 w = two_pi*freq;	   			 //w = 2*pi*f
 A = 2*cos(w*T)*two_14;			 //A = 2*coswT * (2^14)
 y_1 = -sin(w*T)*two_14;		 //y_1 = -sinwT *(2^14)
 y_2 = -sin(2*T*w)*two_14;		 //y_2 = -sin2wT * (2^14)
 return;
}

void main()
{
 comm_intr();            	       //init DSK, codec, McBSP
 while(1);                     	 //infinite loop
}