/* SigLib - Decimation And Interpolation Example */

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

/* Define constants */
#define	SAMPLE_LENGTH			((SLArrayIndex_t)512)
#define	DECIMATION_RATIO		((SLArrayIndex_t)5)
#define	LPF_FILTER_LENGTH		((SLArrayIndex_t)17)

/* Declare arrays and variables */
SLData_t	*pSrcData;				/* Data array pointers */
SLData_t	*pInterpolatedData;

SLData_t	SinePhase;

/* Filter Spec :
Design Type : FIR-Remez
Sample rate : 1.0
Number of coefficients : 17
Fc1 = 0.005000, Fc2 = 0.000000, TBW = 0.150000, PBR = 1.000000, SBA = 70.000000
*/

SLData_t LPFCoefficientArray [] =
{
	2.07352739160608385400e-03, 7.26522635862037607900e-03, 1.76055129619916740600e-02, 3.40183878681010326600e-02,
	5.58186845441603976600e-02, 8.02862272098151808000e-02, 1.03056538811698808480e-01, 1.19282706465355922700e-01,
	1.25169085578150596200e-01, 1.19282706465355922700e-01, 1.03056538811698808480e-01, 8.02862272098151808000e-02,
	5.58186845441603976600e-02, 3.40183878681010326600e-02, 1.76055129619916740600e-02, 7.26522635862037607900e-03,
	2.07352739160608385400e-03
};

SLData_t	pLPFStateArray[LPF_FILTER_LENGTH];
SLArrayIndex_t	LPFIndex;

void main (void)

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

	SLArrayIndex_t	DecimationIndex;
	SLArrayIndex_t	InterpolationIndex;

	pSrcData = SUF_VectorArrayAllocate (SAMPLE_LENGTH);
	pInterpolatedData = SUF_VectorArrayAllocate (SAMPLE_LENGTH);

	h2DGraph =										/* Initialize graph */
		Create2DGraph ("Decimation And Interpolation",	/* 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;
	SDA_SignalGenerate (pSrcData,					/* Pointer to destination array */
						SIGLIB_SINE_WAVE,			/* Signal type - Sine wave */
						((SLData_t)0.9),			/* 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 */
				    "Sine Wave",					/* Title of the dataset */
				    pSrcData,						/* 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 ("\nSine Wave\nPlease hit <Carriage Return> to continue . . ."); getchar ();


	SIF_Decimate (&DecimationIndex);				/* Pointer to source array index */

	SDA_Decimate (pSrcData,							/* Pointer to source array */
				  pSrcData,							/* Pointer to destination array */
				  DECIMATION_RATIO,					/* Decimation ratio */
				  &DecimationIndex,					/* Pointer to source array index */
				  SAMPLE_LENGTH);					/* Source array length */

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


	SIF_Interpolate (&InterpolationIndex);			/* Pointer to destination array index */

	SDA_Interpolate (pSrcData,						/* Pointer to source array */
					 pInterpolatedData,				/* Pointer to destination array */
					 DECIMATION_RATIO,				/* Interpolation ratio */
					 &InterpolationIndex,			/* Pointer to destination array index */
					 SAMPLE_LENGTH);				/* Destination array length */

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Interpolated Sine Wave",		/* Title of the dataset */
				    pInterpolatedData,				/* 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 ("\nInterpolated Sine Wave\nPlease hit <Carriage Return> to continue . . ."); getchar ();


							/* Filter and decimate followed by interpolate and filter */	SinePhase = SIGLIB_ZERO;
	SDA_SignalGenerate (pSrcData,					/* Pointer to destination array */
						SIGLIB_SINE_WAVE,			/* Signal type - Sine wave */
						((SLData_t)0.9),			/* 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 */
				    "Sine Wave",					/* Title of the dataset */
				    pSrcData,						/* 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 ("\nSine Wave\nPlease hit <Carriage Return> to continue . . ."); getchar ();


	SIF_FilterAndDecimate (pLPFStateArray,			/* Pointer to filter state array */
						   &DecimationIndex,		/* Pointer to decimation index register */
						   &LPFIndex,				/* Pointer to filter index register */
						   LPF_FILTER_LENGTH);		/* Filter length */

	SDA_FilterAndDecimate (pSrcData,				/* Pointer to source array */
						   pSrcData,				/* Pointer to destination array */
						   DECIMATION_RATIO,		/* Decimation ratio */
						   &DecimationIndex,		/* Pointer to source array index */
						   pLPFStateArray,			/* Pointer to filter state array */
						   LPFCoefficientArray,		/* Pointer to filter coefficients */
						   &LPFIndex,				/* Pointer to filter index register */
						   LPF_FILTER_LENGTH,		/* Filter length */
						   SAMPLE_LENGTH);			/* Source array length */

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

	SIF_InterpolateAndFilter (pLPFStateArray,		/* Pointer to filter state array */
							  &InterpolationIndex,	/* Pointer to interpolation index register */
							  &LPFIndex,			/* Pointer to filter index register */
							  LPF_FILTER_LENGTH);	/* Filter length */

	SDA_InterpolateAndFilter (pSrcData,				/* Pointer to source array */
							  pInterpolatedData,	/* Pointer to destination array */
							  DECIMATION_RATIO,		/* Interpolation ratio */
							  &InterpolationIndex,	/* Pointer to destination array index */
							  pLPFStateArray,		/* Pointer to filter state array */
							  LPFCoefficientArray,	/* Pointer to filter coefficients */
							  &LPFIndex,			/* Pointer to filter index register */
							  LPF_FILTER_LENGTH,	/* Filter length */
							  SAMPLE_LENGTH);		/* Destination array length */

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Interpolated And Filtered Sine Wave",	/* Title of the dataset */
				    pInterpolatedData,				/* 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 ("\nInterpolated And Filtered Sine Wave\n");

	SUF_MemoryFree (pSrcData);						/* Free memory */
}


