/*************************************************************************
 * This code is written by N. Kehtarnavaz and N. Kim as part of the 
 * textbook "Digital Signal Processing System-Level Design Using LabVIEW".
 *************************************************************************/
 
#include <stdio.h>
#include <math.h>

#define PI		3.141592

void main()
{
	short i, gain;
	int ret;
	float fs, f;
	float y[3], a[2], b1, x;	

	short *output;	
	output = (short *) 0x0000FF00;

	// Coefficient Initialization

	fs = 8000;					// Sampling frequency	
	f = 500;					// Signal frequency
	gain = 100;					// Amplitude gain
	
	a[0] = -1;
	a[1] = 2 * cos(2*PI*f/fs);
	b1 = gain;
	
	// Initial Conditions
	
	y[1] = y[2] = 0;
	x = 1;

	printf("BEGIN\n");
	
	for ( i = 0; i < 128; i++ )
	{
	 	y[0] = b1*x + a[1]*y[1] + a[0]*y[2];
		y[2] = y[1];
	 	y[1] = y[0];

		x = 0;
		
		output[i] = (short) y[0];
	}
		
	ret = sum(output,128);
	
	printf("Sum = %d\n", ret);
	printf("END\n");
}

int sum(const short* array,int N)
{
	int count,sum;
	sum = 0;
	
	for(count=0 ; count < N ; count++)
		sum += array[count];

	return(sum);
}
