/* SigLib Cornu's Spiral Example */

#include <stdio.h>
#include <siglib.h>
#include "GraphFunctions.h"

/* Define constants */
#define	SAMPLE_LENGTH	((SLArrayIndex_t)2048)		/* # samples */
#define	AMPLITUDE		((SLData_t)8.0)				/* # sine cycles */
#define	OFFSET			SIGLIB_ZERO					/* Ramp offset */

SLData_t		*pData1, *pData2;					/* Data array pointers */
SLComplexRect_s	*pComplexData;
SLData_t		IntegralSum1, IntegralSum2;
SLData_t		RampPhase;

void main(void)
{
	GraphObject *hTestXYGraph;						/* Declare graph object */

													/* Allocate memory */
	pData1 = SUF_VectorArrayAllocate (SAMPLE_LENGTH);
	pData2 = SUF_VectorArrayAllocate (SAMPLE_LENGTH);
	pComplexData = SUF_ComplexRectArrayAllocate (SAMPLE_LENGTH);

	hTestXYGraph =									/* Initialize graph */
		CreateXYGraph ("Cornu's Spiral",			/* Graph title */
					   "X-Axis",					/* X-Axis label */
					   "Y-Axis",					/* Y-Axis label */
					   SV_SIGNED,					/* Sign mode */
					   SV_GRAPH_POINT,				/* Graph type */
					   1.1,							/* Dimension - this is square */
					   "localhost");				/* Graph server */
	if (hTestXYGraph == NULL)						/* Graph creation failed - e.g is server running ? */
	{
		printf ("\nGraph creation failure. Please check that the server is running\n");
		exit (1);
	}


	RampPhase = SIGLIB_ZERO;
	SDA_SignalGenerateRamp  (pData1,				/* Pointer to destination array */
							 AMPLITUDE,				/* Amplitude */
							 OFFSET,				/* D.C. Offset */
							 &RampPhase,			/* Phase - maintained across array boundaries */
							 SAMPLE_LENGTH);		/* Array length */

	SDA_Power (pData1,								/* Pointer to source array */
			   pData1,								/* Pointer to destination array */
			   2,									/* Power to raise souce data by */
			   SAMPLE_LENGTH);						/* Array length */

	SDA_Sin (pData1,								/* Pointer to source array */
			 pData2,								/* Pointer to destination array */
			 SAMPLE_LENGTH);						/* Array length */
	SDA_Cos (pData1,								/* Pointer to source array */
			 pData1,								/* Pointer to destination array */
			 SAMPLE_LENGTH);						/* Array length */

	IntegralSum1 = SIGLIB_ZERO;
	IntegralSum2 = SIGLIB_ZERO;

	SDA_Integrate (pData1,							/* Pointer to source array */
				   pData1,							/* Pointer to destination array */
				   ((SLData_t)10000.0),				/* Maximum value for integral */
				   SIGLIB_ONE,						/* Integral decay value - no decay */
				   &IntegralSum1,					/* Internal integral sum - maintained across array boundaries */
				   SAMPLE_LENGTH);					/* Array length */
	SDA_Integrate (pData2,							/* Pointer to source array */
				   pData2,							/* Pointer to destination array */
				   ((SLData_t)10000.0),				/* Maximum value for integral */
				   SIGLIB_ONE,						/* Integral decay value - no decay */
				   &IntegralSum2,					/* Internal integral sum - maintained across array boundaries */
				   SAMPLE_LENGTH);					/* Array length */

													/* Scale results so peaks equal 1.0 */
	SDA_Scale (pData1,								/* Pointer to source array */
			   pData1,								/* Pointer to destination array */
			   SIGLIB_ONE,							/* Peak level */
			   SAMPLE_LENGTH);						/* Array length */
	SDA_Scale (pData2,								/* Pointer to source array */
			   pData2,								/* Pointer to destination array */
			   SIGLIB_ONE,							/* Peak level */
			   SAMPLE_LENGTH);						/* Array length */

	SDA_CreateComplexRect (pData1,					/* Pointer to real source array */
						   pData2,					/* Pointer to imaginary source array */
						   pComplexData,			/* Pointer to complex destination array */
						   SAMPLE_LENGTH);			/* Array length */

	DisplayXYPlot (hTestXYGraph,					/* Graph handle */
				   "Cornu's Spiral",				/* Title of the dataset */
				   (Complex_s *)pComplexData,		/* Array of complex dataset */
				   (int)SAMPLE_LENGTH,				/* Number of data points */
				   SV_GRAPH_POINT,					/* Graph type */
				   SV_BLUE,							/* Colour */
				   SV_HIDE_MARKERS,					/* Marker enable / disable */
				   SV_GRAPH_NEW);					/* New graph */

	printf ("Cornu's Spiral\n");
	
	SUF_MemoryFree (pData1);						/* Free memory */
	SUF_MemoryFree (pData2);
	SUF_MemoryFree (pComplexData);

}		/* End of main() */

