previous next Up Title Contents

Generic programming: AudPerGen.c

/*******
 *
 * AudPerGen.c
 *
 * This is a simple test program, running an auditory periphery model.
 * It has been written using generic programming.
 *
 * Unless otherwise stated all code was written by L. P. O'Mard.
 *
 *	E-mail: L.P.OMard@uk.ac.lut
 *
 * COPYRIGHT (c)
 *
 *	Speech and Hearing Laboratory,
 *	Department of Human Sciences,
 *	Loughborough University of Technology,
 *	Loughborough,
 *	Leicestershire LE11 3TU.
 *	United Kingdom.
 *
 *	1st March 1994.
 *
 *	DISCLAIMER OF WARRANTY
 *
 * Although every effort has been made to ensure the integrity of this
code,
 * the author, Loughborough University and its Laboratories disclaim
all
 * liability for direct or consequential damages resulting from its
use.
 *
 ********/

#include <stdlib.h> 
#include <stdio.h>
#include <math.h>
#include <time.h>

#include "CRLHeaders.h"

/******************************************************************************/

/****************************** Constant definitions
**************************/ 
/******************************************************************************/


#define	PARAMETERS_FILE		"AudPerGen.par"	/* Name of paramters file.*/
#define	NUM_CHANNELS		1			/* No. of channels for the filter. */
#define CHANNEL				0			/* Channel no. for test. */

/******************************************************************************/

/****************************** Global variables
******************************/ 
/******************************************************************************/

char	outputFile[MAXLINE], stParFile[MAXLINE], pEParFile[MAXLINE];
char	bMParFile[MAXLINE], rPParFile[MAXLINE], hCParFile[MAXLINE];

char	stModuleName[MAXLINE], pEModuleName[MAXLINE],
bMModuleName[MAXLINE];
char	rPModuleName[MAXLINE], hCModuleName[MAXLINE];

double	rampInterval;

/******************************************************************************/

/****************************** Functions and subroutines
*********************/ 
/******************************************************************************/

/****************************** ReadParsFromFile
******************************/

/*
 * This program reads a specified number of parameters from a file.
 * It expects there to be one parameter per line.
 */
 
void
ReadParsFromFile(char *fileName)
{
	char	line[MAXLINE];
	FILE	*fp;
	
	if ((fp = fopen(fileName, "r")) == NULL) {
		NotifyError("ReadTestPars: Cannot open data file '%s'.\n", fileName);
		exit(1);
	}
	printf("Reading parameters from file: %s\n", fileName);
	Init_ParFile();
	GetPars_ParFile(fp, "%s", outputFile);
	GetPars_ParFile(fp, "%s %s", stParFile, stModuleName);
	GetPars_ParFile(fp, "%s %s", pEParFile, pEModuleName);
	GetPars_ParFile(fp, "%s %s", bMParFile, bMModuleName);
	GetPars_ParFile(fp, "%s %s", rPParFile, rPModuleName);
	GetPars_ParFile(fp, "%s %s", hCParFile, hCModuleName);
	GetPars_ParFile(fp, "%lf", &rampInterval);
	fclose(fp);
	Free_ParFile();
	
}

/******************************************************************************/

/****************************** Main Body
*************************************/ 
/******************************************************************************/


int main()
{
	double	intensity[NUM_CHANNELS];
	EarObjectPtr	stimulus = NULL, pEFilter = NULL, bMFilter = NULL;
	EarObjectPtr	recptPotn = NULL, hairCell = NULL;
	
	printf("Starting Test Harness...\n");
	
	ReadParsFromFile(PARAMETERS_FILE);
	printf("\nIn this test a '%s' stimulus is presented to an\n",
stModuleName);
	printf("auditory periphery model.\n");
	printf("If necessary, a rise time of %g ms is applied to the
stimulus.\n",
	  MSEC(rampInterval));
	printf("The model process contains the following modules:\n\n");
	printf("\tStimulus generation:\t%s\n", stModuleName);
	printf("\tOuter-/middle-ear:\t%s\n", pEModuleName);
	printf("\tBasilar membrane:\t%s\n", bMModuleName);
	printf("\tIHC receptor pot.:\t%s\n", rPModuleName);
	printf("\tInner hair cell (IHC):\t%s\n", hCModuleName);
	printf("\n");
	
	/* Initialise EarObjects. */
	
	stimulus = Init_EarObject(stModuleName);
	pEFilter = Init_EarObject(pEModuleName);
	bMFilter = Init_EarObject(bMModuleName);
	recptPotn = Init_EarObject(rPModuleName);
	hairCell = Init_EarObject(hCModuleName);

	/* Set up EarObject connections. */
	
	ConnectOutSignalToIn_EarObject( stimulus, pEFilter );
	ConnectOutSignalToIn_EarObject( pEFilter, bMFilter );
	ConnectOutSignalToIn_EarObject( bMFilter, recptPotn );
	ConnectOutSignalToIn_EarObject( recptPotn, hairCell );

	/* Initialise modules. */

	printf("Module parameters...\n\n" );

	DoFun1( ReadPars, stimulus, stParFile);
	DoFun( PrintPars, stimulus );

	DoFun1( ReadPars, pEFilter, pEParFile);
	DoFun( PrintPars, pEFilter );

	DoFun1( ReadPars, bMFilter, bMParFile);
	DoFun( PrintPars, bMFilter );

	DoFun1( ReadPars, recptPotn, rPParFile);
	DoFun( PrintPars, recptPotn );
	
	DoFun1( ReadPars, hairCell, hCParFile);
	DoFun( PrintPars, hairCell );
	
	/* Start main process and print diagonstics. */
	
	printf("\nStarting main process...\n\n" );
	DoProcess( GenerateSignal, stimulus );
	PrintProcessName_EarObject("1-stimulus: '%s'.\n", stimulus );
	if (!stimulus->outSignal->rampFlag ) {
		RampUpOutSignal_Ramp(stimulus, Sine_Ramp, rampInterval );
		printf("\tAudPerGen: Stimulus has been ramped.\n" );
	}
	CalcIntensities_GenAnalysis(intensity, stimulus->outSignal,
rampInterval );
	printf("\tStimulus intensity = %g dB SPL.\n", intensity[CHANNEL] );
	DoProcess( RunModel, pEFilter );
	PrintProcessName_EarObject("2-Outer-/middle-ear: '%s'.\n", pEFilter );
	DoProcess( RunModel, bMFilter );
	PrintProcessName_EarObject("3-Basilar membrane: '%s'.\n", bMFilter );
	DoProcess( RunModel, recptPotn );
	PrintProcessName_EarObject("4-IHC Receptor potential: '%s'.\n", recptPotn
);
	DoProcess( RunModel, hairCell );
	PrintProcessName_EarObject("5-Inner hair cell (IHC): '%s'.\n", hairCell
);
	WriteOutSignal_DataFile(outputFile, hairCell );

	FreeAll_EarObject();

	printf("Finished test.\n");
    exit(0);
	
}



previous next Up Title Contents