/* SigLib - ADPCM Example */

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

/* Define constants */
#define DEBUG					0					/* Set to '1' to enable debug, '0' otherwise */

#define	SAMPLE_LENGTH			((SLArrayIndex_t)256)
#define INPUT_MAGNITUDE			((SLArrayIndex_t)256)		/* Simulate 8 bit source data */

/* Declare arrays and variables */
SLData_t	SinePhase;
SLData_t	*pSrc, *pDst, *pADPCMData, *pADPCMPlotData;

#if DEBUG
SLData_t	Estimate [SAMPLE_LENGTH];
#endif

void	main(void);

void main (void)

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

	pSrc = SUF_VectorArrayAllocate (SAMPLE_LENGTH);
	pADPCMData = SUF_VectorArrayAllocate (SAMPLE_LENGTH);
	pADPCMPlotData = SUF_VectorArrayAllocate (SAMPLE_LENGTH);
	pDst = SUF_VectorArrayAllocate (SAMPLE_LENGTH);

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

	SinePhase = SIGLIB_ZERO;						/* Different start phases to test encoder and decoder */
//	SinePhase = -1.0;
//	SinePhase = 1.0;
//	SinePhase = SIGLIB_PI;
	SDA_SignalGenerate (pSrc,						/* Pointer to destination array */
						SIGLIB_SINE_WAVE,			/* Signal type - Sine wave */
						INPUT_MAGNITUDE,			/* Signal peak level */
						SIGLIB_FILL,				/* Fill (overwrite) or add to existing array contents */
						((SLData_t)0.005),			/* 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 */
				    "Source sine wave",				/* Title of the dataset */
				    pSrc,							/* Array of Double dataset */
				    SAMPLE_LENGTH,					/* Number of data points */
					SV_GRAPH_LINE,					/* Graph type */
				    SV_MAGENTA,						/* Colour */
					SV_HIDE_MARKERS,				/* Marker enable / disable */
					SV_GRAPH_NEW);					/* New graph */


#if DEBUG											/* If we are in debug mode then save the estimate */
													/* Run the ADPCM encoder */
	SDA_AdpcmEncoderDebug (pSrc,					/* Pointer to source array */
						   pADPCMData,				/* Pointer to destination array */
						   Estimate,				/* Pointer to estimate array */
						   SAMPLE_LENGTH);			/* Array length */

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Estimate",						/* Title of the dataset */
				    Estimate,						/* 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_ADD);					/* New graph */
//	plot_frequency_domain (Estimate, SIGLIB_FLAT_TOP, "Estimate Frequency Spectrum", SAMPLE_LENGTH, SAMPLE_LENGTH);
#else
													/* Run the ADPCM encoder */
	SDA_AdpcmEncoder (pSrc,							/* Pointer to source array */
					  pADPCMData,					/* Pointer to destination array */
					  SAMPLE_LENGTH);				/* Array length */

	SDA_Multiply (pADPCMData, 100.0, pADPCMPlotData, SAMPLE_LENGTH);	/* Copy and scale data for plotting */

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "ADPCM Encoder Output (*100)",	/* Title of the dataset */
				    pADPCMPlotData,					/* 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 */
#endif

													/* Run the ADPCM decoder */
	SDA_AdpcmDecoder (pADPCMData,					/* Pointer to source array */
					  pDst,							/* Pointer to destination array */
					  SAMPLE_LENGTH);				/* Array length */

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "ADPCM Decoder Output",			/* Title of the dataset */
				    pDst,							/* Array of Double dataset */
				    SAMPLE_LENGTH,					/* Number of data points */
					SV_GRAPH_LINE,					/* Graph type */
				    SV_YELLOW,						/* Colour */
					SV_HIDE_MARKERS,				/* Marker enable / disable */
					SV_GRAPH_ADD);					/* New graph */

	SDA_Subtract2 (pSrc, pDst, pDst, SAMPLE_LENGTH);

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Output Error",					/* Title of the dataset */
				    pDst,							/* 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 */

	SUF_MemoryFree (pSrc);							/* Free memory */
	SUF_MemoryFree (pADPCMData);
	SUF_MemoryFree (pADPCMPlotData);
	SUF_MemoryFree (pDst);
}


