/* SigLib Correlation Example.
Pulls a signal outof noise with successive auto-correlations */

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

#define	SAMPLE_LENGTH	((SLArrayIndex_t)512)

SLData_t	*pSrc1, *pSrc2;
SLData_t	SinePhase;

void main(int argc, char *argv[])

{
	GraphObject *h2DGraph;							/* Declare graph object */

	SLFixData_t	i;

	if(argc != 3)
	{
		printf ("\nThis demo pulls a signal outof noise with successive auto-correlations\n");
		printf ("\nSyntax: corr2 <Sine level wrt noise> <# iterations>\n");
		printf ("\nTry   : corr2 0.2 5\n");
		exit (0);
	}

	h2DGraph =										/* Initialize graph */
		Create2DGraph ("Correlation",				/* Graph title */
					   "Time",						/* X-Axis label */
					   "Magnitude",					/* Y-Axis label */
					   SV_AUTO_SCALE,				/* Scaling mode */
					   SV_SIGNED,					/* Sign mode */
					   SV_GRAPH_LINE,				/* Graph type */
					   "localhost");				/* Graph server */
	if (h2DGraph == NULL)							/* Graph creation failed - e.g is server running ? */
	{
		printf ("\nGraph creation failure. Please check that the server is running\n");
		exit (1);
	}

	pSrc1 = SUF_VectorArrayAllocate (SAMPLE_LENGTH);
	pSrc2 = SUF_VectorArrayAllocate (SAMPLE_LENGTH);

	SinePhase = SIGLIB_ZERO;

				/* Generate a noisy sinewave */
	SDA_SignalGenerate (pSrc1,						/* Pointer to destination array */
						SIGLIB_SINE_WAVE,			/* Signal type - Sine wave */
						atof (argv[1]),				/* Signal peak level */
						SIGLIB_FILL,				/* Fill (overwrite) or add to existing array contents */
						((SLData_t)0.01),			/* Signal frequency */
						SIGLIB_ZERO,				/* D.C. Offset */
						SIGLIB_ZERO,				/* Unused */
						SIGLIB_ZERO,				/* Signal end value - Unused */
						&SinePhase,					/* Signal phase - maintained across array boundaries */
						SIGLIB_NULL_DATA_PTR,		/* Unused */
						SAMPLE_LENGTH);				/* Output array length */

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Small Amplitude Sine Wave",	/* Title of the dataset */
				    pSrc1,							/* Array of Double dataset */
				    SAMPLE_LENGTH,					/* Number of data points */
					SV_GRAPH_LINE,					/* Graph type */
				    SV_BLUE,						/* Colour */
					SV_HIDE_MARKERS,				/* Marker enable / disable */
					SV_GRAPH_NEW);					/* New graph */
	printf ("\nSmall Amplitude Sine Wave\nPlease hit <Carriage Return> to continue . . ."); getchar ();

	SDA_SignalGenerate (pSrc1,						/* Pointer to destination array */
						SIGLIB_WHITE_NOISE,			/* Signal type - random white noise */
						SIGLIB_ONE,					/* Signal peak level */
						SIGLIB_ADD,					/* Fill (overwrite) or add to existing array contents */
						SIGLIB_ZERO,				/* Signal frequency - Unused */
						SIGLIB_ZERO,				/* D.C. Offset */
						SIGLIB_ZERO,				/* Unused */
						SIGLIB_ZERO,				/* Signal end value - Unused */
						SIGLIB_NULL_DATA_PTR,		/* Unused */
						SIGLIB_NULL_DATA_PTR,		/* Unused */
						SAMPLE_LENGTH);				/* Output array length */

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Noisy Sine Wave",				/* Title of the dataset */
				    pSrc1,							/* Array of Double dataset */
				    SAMPLE_LENGTH,					/* Number of data points */
					SV_GRAPH_LINE,					/* Graph type */
				    SV_BLUE,						/* Colour */
					SV_HIDE_MARKERS,				/* Marker enable / disable */
					SV_GRAPH_NEW);					/* New graph */
	printf ("\nNoisy Sine Wave\nPlease hit <Carriage Return> to continue . . ."); getchar ();

	for (i = 0; i < atoi (argv[2]); i++)
	{
		SDA_CorrelateCircular (pSrc1,				/* Pointer to input array 1 */
							   pSrc1,				/* Pointer to input array 2 */
							   pSrc2,				/* Pointer to destination array */
							   SAMPLE_LENGTH);		/* Length of input arrays */
		
		SDA_ClearLocation (pSrc2,					/* Pointer to array */
						   0,						/* Location to clear */
						   SAMPLE_LENGTH);			/* Array length */
	
		Display2DGraph (h2DGraph,					/* Graph handle */
					    "Autocorrelated Signal",	/* Title of the dataset */
					    pSrc2,						/* Array of Double dataset */
					    SAMPLE_LENGTH,				/* Number of data points */
						SV_GRAPH_LINE,				/* Graph type */
					    SV_BLUE,					/* Colour */
						SV_HIDE_MARKERS,			/* Marker enable / disable */
						SV_GRAPH_NEW);				/* New graph */
		printf ("\nIteration = %d\n", (i+1));
		printf ("Autocorrelated Signal\nPlease hit <Carriage Return> to continue . . ."); getchar ();

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

	while (!kbhit()); getch();

	SUF_MemoryFree (pSrc1);							/* Free memory */
	SUF_MemoryFree (pSrc2);
}

