/* SigLib Correlation Example */

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

#define	SAMPLE_LENGTH			((SLArrayIndex_t)512)
#define	NUMBER_OF_CORRELATIONS	256L

SLData_t	*pSrc1, *pSrc2, *dest;
SLData_t	SinePhase;

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

	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);
	dest = 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 */
						((SLData_t)0.2),			/* 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 */

	SDA_SignalGenerate (pSrc1,						/* Pointer to destination array */
						SIGLIB_WHITE_NOISE,			/* Signal type - random white noise */
						SIGLIB_HALF,				/* 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 */

				/* Generate another noisy sinewave */
	SDA_SignalGenerate (pSrc2,						/* Pointer to destination array */
						SIGLIB_SINE_WAVE,			/* Signal type - Sine wave */
						((SLData_t)0.2),			/* 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 */

	SDA_SignalGenerate (pSrc2,						/* Pointer to destination array */
						SIGLIB_WHITE_NOISE,			/* Signal type - random white noise */
						SIGLIB_HALF,				/* 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 A",			/* 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 A\nPlease hit <Carriage Return> to continue . . ."); getchar ();

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Noisy Sine Wave B",			/* 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 ("\nNoisy Sine Wave B\nPlease hit <Carriage Return> to continue . . ."); getchar ();

	SDA_CorrelateCircular (pSrc1,					/* Pointer to input array 1 */
						   pSrc2,					/* Pointer to input array 2 */
						   dest,					/* Pointer to destination array */
						   SAMPLE_LENGTH);			/* Length of input arrays */

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Circularly Correlated Signals",	/* Title of the dataset */
				    dest,							/* 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 ("\nCircularly Correlated Signals\nPlease hit <Carriage Return> to continue . . ."); getchar ();

	SDA_CorrelateLinear (pSrc1,						/* Pointer to input array #1 */
						 pSrc2,						/* Pointer to input array #2 */
						 dest,						/* Pointer to destination array */
						 SAMPLE_LENGTH,				/* Length of array #1 */
						 SAMPLE_LENGTH,				/* Length of array #2 */
						 NUMBER_OF_CORRELATIONS);	/* Number of correlations */

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Linearly Correlated Signals",	/* Title of the dataset */
				    dest,							/* Array of Double dataset */
				    NUMBER_OF_CORRELATIONS,			/* Number of data points */
					SV_GRAPH_LINE,					/* Graph type */
				    SV_BLUE,						/* Colour */
					SV_HIDE_MARKERS,				/* Marker enable / disable */
					SV_GRAPH_NEW);					/* New graph */
	printf ("\nLinearly Correlated Signals\n");

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


