/* SigLib Convolution Example */

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

#define DATA_SET_1				0	/* Select '1' or '0' to choose between test sequences */

#if DATA_SET_1
#define	INPUT_LENGTH			20L	/* Input array length */
#define	IMPULSE_LENGTH			20L	/* Impulse response array length */
#define	PARTIAL_IMPULSE_LENGTH	((SLArrayIndex_t)10)	/* Partial response array length */
SLData_t	input[] = {					/* Input data */
	0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5,
	0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
	};

SLData_t	impulse[] = {				/* Impulse response data */
	1.0, 1.0, 1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 0.5, 0.5,
	0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
	};
#else
#define	INPUT_LENGTH			5L		/* Input array length */
#define	IMPULSE_LENGTH			3L		/* Impulse response array length */
#define	PARTIAL_IMPULSE_LENGTH	3L		/* Partial response array length */
SLData_t	input[] = {					/* Input data */
	1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 0.0, 0.0, 0.0,
	0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
	};

SLData_t	impulse[] = {				/* Impulse response data */
	4.0, 5.0, 6.0, 7.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
	0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
	};
#endif
										/* Result array length */
#define	RESULT_LENGTH			((SLArrayIndex_t)(INPUT_LENGTH + IMPULSE_LENGTH - 1))

SLData_t	dest[RESULT_LENGTH];


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

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

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Source Signal",				/* Title of the dataset */
				    input,							/* Array of Double dataset */
				    INPUT_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 ();

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Impulse Response",				/* Title of the dataset */
				    impulse,						/* Array of Double dataset */
				    IMPULSE_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 ("\nImpulse Response\nPlease hit <Carriage Return> to continue . . ."); getchar ();


	SDA_ConvolveLinear (input,						/* Pointer to input array */
						impulse,					/* Pointer to impulse response data */
						dest,						/* Pointer to destination array */
						INPUT_LENGTH,				/* Input data length */
						IMPULSE_LENGTH);			/* Impulse response length */

	printf ("\ny(t) = x(t)*h(t)\n");
	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Convolved Data",				/* Title of the dataset */
				    dest,							/* Array of Double dataset */
				    RESULT_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 ("Convolved Data\nPlease hit <Carriage Return> to continue . . ."); getchar ();

	SDA_ConvolvePartial (input,						/* Pointer to input array */
						 impulse,					/* Pointer to impulse response data */
						 dest,						/* Pointer to destination array */
						 INPUT_LENGTH,				/* Input data length */
						 PARTIAL_IMPULSE_LENGTH);	/* Impulse response length */

	printf ("\ny(t) = x(t)*h(t)\n");
	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Partially Convolved Data",		/* Title of the dataset */
				    dest,							/* Array of Double dataset */
				    INPUT_LENGTH - PARTIAL_IMPULSE_LENGTH + 1,	/* Number of data points */
					SV_GRAPH_LINE,					/* Graph type */
				    SV_BLUE,						/* Colour */
					SV_HIDE_MARKERS,				/* Marker enable / disable */
					SV_GRAPH_NEW);					/* New graph */
	printf ("Partially Convolved Data\nPlease hit <Carriage Return> to continue . . ."); getchar ();

	SDA_ConvolveCircular (input,					/* Pointer to input array */
						  impulse,					/* Pointer to impulse response data */
						  dest,						/* Pointer to destination array */
						  INPUT_LENGTH);			/* Length of input arrays */

	printf ("\ny(t) = x(t)*h(t)\n");
	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Circularly Convolved Data",	/* Title of the dataset */
				    dest,							/* Array of Double dataset */
				    INPUT_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 ("Circularly Convolved Data\n");
}


