/* SigLib Delta Modulation / Demodulation Example */

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

/* Define constants */
#define	SAMPLE_LENGTH		((SLArrayIndex_t)128)

SLData_t	*input, *modulated, *demodulated;
SLData_t	CosinePhase;
SLData_t	Delta, CurrentModValue, CurrentDeModValue;

void	main(void);

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

	input = SUF_VectorArrayAllocate (SAMPLE_LENGTH);
	modulated = SUF_VectorArrayAllocate (SAMPLE_LENGTH);
	demodulated = SUF_VectorArrayAllocate (SAMPLE_LENGTH);

	h2DGraph =										/* Initialize graph */
		Create2DGraph ("Delta Modulation / Demodulation",	/* 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);
	}

	CosinePhase = SIGLIB_ZERO;
	SDA_SignalGenerate (input,						/* Pointer to destination array */
						SIGLIB_COS_WAVE,			/* Signal type - Cosine wave */
						((SLData_t)32.0),			/* Signal peak level */
						SIGLIB_FILL,				/* Fill (overwrite) or add to existing array contents */
						((SLData_t)0.0025),			/* Signal frequency */
						((SLData_t)16.0),			/* D.C. Offset */
						SIGLIB_ZERO,				/* Unused */
						SIGLIB_ZERO,				/* Signal end value - Unused */
						&CosinePhase,				/* Signal phase - maintained across array boundaries */
						SIGLIB_NULL_DATA_PTR,		/* Unused */
						SAMPLE_LENGTH);				/* Output array length */

	Delta = SIGLIB_ONE;								/* Initialise application variables */
	CurrentModValue = SIGLIB_ZERO;
	CurrentDeModValue = SIGLIB_ZERO;

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Source Signal",				/* Title of the dataset */
				    input,							/* 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 ("\nSource Signal\nPlease hit <Carriage Return> to continue . . ."); getchar ();

	SDA_DeltaModulate (input,						/* Pointer to source array */
					   modulated,					/* Pointer to destination array */
					   &CurrentModValue,			/* Pointer to current modulator integral value */
					   Delta,						/* Delta magnitude */
					   SAMPLE_LENGTH);				/* Input Array length */

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Modulated Signal",				/* Title of the dataset */
				    modulated,						/* Array of Double dataset */
				    SAMPLE_LENGTH,					/* Number of data points */
					SV_GRAPH_LINE,					/* Graph type */
				    SV_RED,							/* Colour */
					SV_HIDE_MARKERS,				/* Marker enable / disable */
					SV_GRAPH_ADD);					/* New graph */
	printf ("\nModulated Signal\nPlease hit <Carriage Return> to continue . . ."); getchar ();

	SDA_DeltaDemodulate (modulated,					/* Pointer to source array */
						 demodulated,				/* Pointer to destination array */
						 &CurrentDeModValue,		/* Pointer to current modulator integral value */
						 SAMPLE_LENGTH);			/* Input array length */

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Demodulated Signal",			/* Title of the dataset */
				    demodulated,					/* Array of Double dataset */
				    SAMPLE_LENGTH,					/* Number of data points */
					SV_GRAPH_LINE,					/* Graph type */
				    SV_GREEN,						/* Colour */
					SV_HIDE_MARKERS,				/* Marker enable / disable */
					SV_GRAPH_ADD);					/* New graph */
	printf ("\nDemodulated Signal\n");

	SUF_MemoryFree (input);							/* Free memory */
	SUF_MemoryFree (modulated);
	SUF_MemoryFree (demodulated);

}


