/* SigLib Display A File Of .sig Data Example */

/* Include files */
#include <stdio.h>
#include <siglib.h>
#include <nhl.h>
#include "GraphFunctions.h"

/* Define constants */
#define	INPUT_LENGTH	((SLArrayIndex_t)512)
#define	FFT_SIZE		((SLArrayIndex_t)512)
#define	LOG_FFT_SIZE	((SLArrayIndex_t)9)

/* Declare global variables */
SLData_t	*input, *pRealData, *pImagData, *pResults, *pFFTCoeffs;

/* Function declarations */

void main (int argc, char *argv[]);
void help (void);

void main (int argc, char *argv[])
{
	GraphObject *h2DGraph;							/* Declare graph object */

	SLFixData_t	PerformFFT = SIGLIB_FALSE;

	if (argc == 1)
	{
		help ();
		exit (0);
	}

	if (argc > 2)
	{
		if ((*argv[2] == 'f') || (*argv[2] == 'F'))
		{
			PerformFFT = SIGLIB_TRUE;
		}

		if (*argv[2] == '?')
		{
			help ();
			exit (0);
		}
	}

	input = SUF_VectorArrayAllocate (INPUT_LENGTH);
	pRealData = SUF_VectorArrayAllocate (INPUT_LENGTH);
	pImagData = SUF_VectorArrayAllocate (INPUT_LENGTH);
	pResults = SUF_VectorArrayAllocate (INPUT_LENGTH);
	pFFTCoeffs = SUF_FftCoefficientAllocate (FFT_SIZE);

	SDA_Clear (input,								/* Pointer to destination array */
			   INPUT_LENGTH);						/* Array length */
	SDA_Clear (pRealData,							/* Pointer to destination array */
			   INPUT_LENGTH);						/* Array length */
	SDA_Clear (pImagData,							/* Pointer to destination array */
			   INPUT_LENGTH);						/* Array length */

	read_buffer (input, argv[1], INPUT_LENGTH);		/* Read data from disk */

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

													/* Initialise FFT */
	SIF_Fft (pFFTCoeffs,							/* Pointer to FFT coefficients */
			 SIGLIB_NULL_ARRAY_INDEX_PTR,			/* Pointer to bit reverse address table - NOT USED */
			 FFT_SIZE);								/* FFT Size */

													/* Gen. complex window coeffs */
	printf ("Data file : %s", argv[1]);

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Data File Display",			/* Title of the dataset */
				    input,							/* Array of Double dataset */
				    INPUT_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 (PerformFFT)
	{
		printf ("\nData File Display\nPlease hit <Carriage Return> to continue . . ."); getchar ();

		SDA_Copy (input,							/* Pointer to source array */
				  pRealData,						/* Pointer to destination array */
				  FFT_SIZE);						/* Array length */

													/* Frequency domain calculation */
													/* Perform complex source data FFT */
		SDA_Cfft (pRealData,						/* Pointer to real array */
				  pImagData,						/* Pointer to imaginary array */
				  pFFTCoeffs,						/* Pointer to FFT coefficients */
				  SIGLIB_NULL_ARRAY_INDEX_PTR,		/* Pointer to bit reverse address table - NOT USED */
				  FFT_SIZE,							/* FFT size */
				  LOG_FFT_SIZE);					/* log2 FFT size */

													/* Calculate real power from complex */
		SDA_LogMagnitude (pRealData,				/* Pointer to real source array */
						  pImagData,				/* Pointer to imaginary source array */
						  pResults,					/* Pointer to log magnitude destination array */
						  INPUT_LENGTH);			/* Array length */

													/* Plot FFT magnitude result */
		Display2DGraph (h2DGraph,					/* Graph handle */
					    "Frequency Response",		/* Title of the dataset */
					    pResults,					/* Array of Double dataset */
					    INPUT_LENGTH,				/* Number of data points */
						SV_GRAPH_LINE,				/* Graph type */
					    SV_MAGENTA,					/* Colour */
						SV_HIDE_MARKERS,			/* Marker enable / disable */
						SV_GRAPH_NEW);				/* New graph */
		printf ("\nFrequency Response Display\nPlease hit <Carriage Return> to continue . . ."); getchar ();


													/* Plot wrapped FFT phase result */
													/* Calc phase from complex */
		SDA_PhaseWrapped (pRealData,				/* Pointer to real source array */
						  pImagData,				/* Pointer to imaginary source array */
						  pResults,					/* Pointer to destination array */
						  INPUT_LENGTH);			/* Array length */

		Display2DGraph (h2DGraph,					/* Graph handle */
					    "Wrapped Phase",			/* Title of the dataset */
					    pResults,					/* Array of Double dataset */
					    INPUT_LENGTH,				/* Number of data points */
						SV_GRAPH_LINE,				/* Graph type */
					    SV_MAGENTA,					/* Colour */
						SV_HIDE_MARKERS,			/* Marker enable / disable */
						SV_GRAPH_NEW);				/* New graph */
		printf ("\nWrapped Phase\nPlease hit <Carriage Return> to continue . . ."); getchar ();


													/* Plot FFT unwrapped phase result */
													/* Calc phase from complex */
		SDA_PhaseUnWrapped (pRealData,				/* Pointer to real source array */
							pImagData,				/* Pointer to imaginary source array */
							pResults,				/* Pointer to destination array */
							INPUT_LENGTH);			/* Array length */

		Display2DGraph (h2DGraph,					/* Graph handle */
					    "Unwrapped Phase",			/* Title of the dataset */
					    pResults,					/* Array of Double dataset */
					    INPUT_LENGTH,				/* Number of data points */
						SV_GRAPH_LINE,				/* Graph type */
					    SV_MAGENTA,					/* Colour */
						SV_HIDE_MARKERS,			/* Marker enable / disable */
						SV_GRAPH_NEW);				/* New graph */
		printf ("\nUnwrapped Phase\n");
	}


	SUF_MemoryFree (input);							/* Free memory */
	SUF_MemoryFree (pRealData);
	SUF_MemoryFree (pImagData);
	SUF_MemoryFree (pResults);
	SUF_MemoryFree (pFFTCoeffs);
}


void help ()
{
	printf ("DDF.EXE - Display data file utility (C) Numerix Ltd. 1996\n");
	printf ("Syntax :\n");
	printf ("ddf filename [f]\n");
	printf ("[f] indicates frequency domain display.\n");
}


