#include <stdio.h>
#include "complex.h"
#include "pi.h"
#define MAXLINE 81
#define EXCESS_BW 0.0
#define T 6.578947e-6 	/* 152 kHz */

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

			t1d1

************************************************************************

Program reads input file (filein) containing the transfer function of the
channel, calculates the equalization required to get a raised cosine
response, and puts that equalization out to another file (fileout)

Programmer: D.G.Messerschmitt
Date: Jan. 2, 1986
*/

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

{
	char line[MAXLINE];
	float f;
	double sin(),arg;
	COMPLEX tranf,rc;

	FILE *fopen();
	FILE *filein,*fileout;

	if(argc != 3)	{
		fprintf(stderr,"Usage: eal filein fileout\n");
		exit(1);
		}

/* open the files */

	if((filein = fopen(argv[1],"r")) == NULL) {
		fprintf(stderr,"File %s could not be opened\n",
			argv[1]);
		exit(1);
		}
	if((fileout = fopen(argv[2],"w")) == NULL) {
		fprintf(stderr,"File %s could not be opened\n",
			argv[2]);
		exit(1);
		}

/* read one line file in turn */

	for(;;)	{
		if(fgets(line,MAXLINE,filein) == NULL)
			exit(0); /* EOF encountered */

	/*  read f= freq, tranf = transfer function of channel */
		sscanf(line,"%f %f %f",&f,&tranf.real,&tranf.imag);

	/* calculate raised cosine response at same freq */
		rc.imag = 0;
		if(f <= (1.-EXCESS_BW)*0.5/T)
			rc.real = T;
		else if(f < (1.+EXCESS_BW)*0.5/T) {
			arg = PI*(2.*T*f-1.)*.5/EXCESS_BW;
			rc.real = 0.5*T*(1.-sin(arg));
			}
		else rc.real = 0.;

	/* equalizer transfer function is raised cosine divided by
		channel transfer function */
		if(rc.real > 1.e-20)
			rc = cdiv(rc,tranf);

		fprintf(fileout,"%f %e %e\n",f,rc.real,rc.imag);
		}

}
