/* NEWVOLT1.c                                          21-02-98 TP @ Khodad
  This is a modified version of the original program used in the simulation */

/* gconvt1.c   GAC PA convert routine
		converts a,b,c to A, B       29/03/97 tp */

/* gpwrF3b.c four  bit version 24Mar97 tp */

/* PWR-R2B.C    last modified on 20 October 96
		This version is aimed for generating the Power Lookup
		Tables which can be used for masking one or both the
		Pols of a given Dish.
		Since Pols are coming time multiplexed from FFT cards
		they have to be Masked at real time.

	     TABLE-0: (newly introduced this time 20 Oct 96)
		Table 0 has all zeros.This is generated by making the
		PwrU[0] >= (98*2)*64; in (98 is Power max; 2 -accounts
		for round-off; 64 is #of levels)
		the main() program

	     POL MASKING with this lookup PROM:
		Pol masking can be achieved by choosing always Table-0
		in the GAC RAM for that corresponding Dish.

		Make every EVEN locations Zero in the RAM for masking Pol-1.
		  -do-     ODD  locations    -do-          -do-       Pol-2.

	     TABLE-1 to TABLE-15: (same as before-15mar96 but a mistake
		is identified and removed in the table generation routine )

		In this set of tables Table 1 to 15 are the Root-2 tables
		with 0.003 (or P003) sitting at Table 7 . Any table below
		table 7 (ie 6,5,4,..1) are at Root-2 step below its
		previous table. (or at 1.5 dB down in power level ).
		Any table above 7 (ie. 8,9,10,..15) are in Root-2 step
		above its previous table (or at 1.5 dB up in power level).

		at making the first Table
		as ALL ZEROs.

	After the above file is generated
	concatanate this ALL16U16.BIN  file with this file. This will make
	the Power Proms second half as Diagnostics bank.

	command    r2table2.bin > P003-r2b.bin
		   ALL16U16.BIn >> P003-r2b.bin
							:   GK and TP
							    20 Oct 96 */
/*Copied from PWR-R2A.C of 15th March 96 */

/* NOTE:  */
/* Peak value (PWR_SAT) of 0.001 is required to SEE  -2.5 dBm Noise level */
/* PWR_SAT =1.25 will work well for  0 dBm CW 		        */
/* This module computes the Power Lookup Values for a Window	*/
/* between PWR_SAT_L and PWR_SAT_U 	                        */
/* refer Pulsar Receiver log note book p# 69 dtd. Nov 20/92   */

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

#define  stdprn NULL
#define  getch() ;

/* ----------------------------------------------- Define CONSTANTS Here---*/
#define  Q_LEVELS 15  /* 4  bits; 15 -ve, 15 +ve Quantization levels */
#define  P003 0.003
#define  TABELS 16
#define  OUTFILE "VolTabl1.txt"  /* main data file in TXT format*/
#define  OUTFILE2 "VolTabl1.tx2" /* doc file contains S# T#,a,b,c, and R,I */
/* ----------------------------------------------------------------------*/
#define NOTE_1 "GAC Voltage LOOKUP TABLE Generator program :TP-21feb98 Khodad"

/*          Copied  PWR-1H.C   to  PWR-16A.C     27 Feb  96               */
/*          GAC POWER LOOKUP TABLE GENERATOR PROGRAM   modified 20 nov 92 */

/* ----------------------------------------------GLOBAL  Declarations---*/
void  generate();

	double x,y,e,bl,P[256],
		  actual_x,
		  actual_r,actual_i,Xr,Xi,
		  PWR_SAT_U,
		  PWR_SAT_L,
		  old_level,
		  step_size, PwrU[64] ;
	int lines,a,b,c,d,Adr,i,TableNo,op[4096] ;
	int ma,mb ; /* magnitudes */
	FILE *out,*out2;
	int r1,i1,e1,tno1 , I1;
	unsigned long int samp;

/* ----------------------------------------------Function ---- convert()---*/
void convert(int r1,int i1,int e1, int Tno, long int sn)
{
	if(fmod(sn,1024)==0)
	 {   printf("%6ld processing [ PA LT] ",sn);
	     printf(" (R=%2d I=%2d  E=%2d) Table=%3d ]\n",r1,i1,e1,Tno);
	 }
	/* Finding the Upper Saturation values for all the 16 tables */
	 e=1.0 ;
	 for(i=8;i<=15;i++)
	 {    e = e*sqrt(2.0) ;
	      PwrU[i] = (float)(P003)*e ;
	      /* printf("gtop %d:   %f  e: %f\n",i,PwrU[i],e); */
	 }

	 PwrU[7] =P003 ;    /* 7th Table operates at the P003 power level */
			    /* Ooty table */
	 /*  printf("gtop %d:   %f\n",7,PwrU[7]); */

	 e= 1.0 ;
	 for(i=6;i>=1;i--)
	 {    e = (float)e*sqrt(2.0) ;
	      PwrU[i] = ((float)P003)/(float)e ;
	      /* printf("gtop %d:   %f  e:%f\n",i,PwrU[i],e); */

	 }

	 PwrU[0] =15000 ;/* This will force all values as zeroes for the
			    0th Table; And this table can be used for masking
			    one or both Pols of a specific Dish.
			    Oct 20-96 GK,TP  */

/* convert to Voltage   */
    /*     printf("gtopRI values\n"); */
	  for(i=1;i<=15;i++)
	  {
	     PwrU[i] = (float)sqrt( PwrU[i] ) ; /* equivalent voltage */
	  /*   printf("%f\n",PwrU[i]); */
	  }
	  /*  printf("Table :%d\n",Tno) ; */

	/* Generate the table contents */

	TableNo= Tno ;

	    PWR_SAT_U =  PwrU[TableNo]    ; /*  15 Mr 96    */
	    PWR_SAT_L =  0.0000                  ;

/*	printf("\nEnter Power Upper Saturation Value :  "); */
/*	scanf("%f ",&PWR_SAT_U ) ;                          */
/*	printf("\nEnter Power Lower saturation Value :  "); */
/*	scanf("%f ",&PWR_SAT_L ) ;                          */

	    generate(r1,i1,e1);

} /*  end of convert  */


void generate(int r1,int i1, int e1)
{       int q_real,q_img, nc, q_pwr, R,I ;

	bl = (PWR_SAT_U - PWR_SAT_L) /(float)Q_LEVELS ; /* Find the STEP Size */
/*        printf("LSB size %f\n",bl); */
/*	fprintf(stdprn,"POWER Lookup Table \nDiff. Quantization   Levels\n\n"); */

	/* Computing the Quantization step values */
	old_level = PWR_SAT_L ;
	step_size = 0 ;
	x= 0 ;  P[0]= 0 ;

/*        printf("Table steps\n");  */
	for(c=0; c<=(Q_LEVELS); c++)
	{	P[c] = c*bl ;
	/*	printf("%4.15f \n",P[c] ); */
	}

	lines =0;

	/* Computing the PA  Lookup values     */

 /* Address generation  is not    required in this routine 24/02/97tp  */
    a=abs(r1); b=abs(i1); c=abs(e1); /* Instead this line is required
					to assign the values of a,b,c */
      /*   printf("a b c : %d %d %d \n",r1,i1,e1) ; */
				Adr =((256*c)+(16*b)+a);
				op[Adr]=0;
				q_pwr=0;
				ma = a&0x7 ;   /* sign bits removed */
				mb = b&0x7 ;   /*       -do-       */
    /*   printf("%d %d %d    %d %d %d \n",a,b,c,ma,mb,c);*/
    /* actual_x = ((float)((ma*ma)+(mb*mb)))/pow(2.0,(float)(2.0*c)) ; */

			   actual_r = ((float)r1)/pow(2.0,(float)abs(e1)) ;
			   actual_i = ((float)i1)/pow(2.0,(float)abs(e1)) ;

		 /* Half step size  is added for rounding-off    */
		 /* if the input is +ve add, if -ve subtract 1/2 step */
	  if(r1!=0)
	      Xr=(actual_r/fabs(actual_r))*(fabs(actual_r)+(bl/(float)2.0)) ;
	  else Xr = 0.0 ;
	  if(i1!=0)
	      Xi=(actual_i/fabs(actual_i))*(fabs(actual_i)+(bl/(float)2.0)) ;
	  else Xi = 0.0 ;
      /*  printf("Ar: %f Ai: %f  Xr: %f  Xi: %f\n",actual_r,actual_i,Xr,Xi);*/

			    R=0 ; I=0;

				for(d=1; d<=( Q_LEVELS ); d++)
				{  if( fabs(Xr) >= P[d] )
				      R = (Xr/fabs(Xr))*d ;
				   if( fabs(Xi) >= P[d] )
				      I = (Xi/fabs(Xi))*d ;
				   op[Adr]=R;

			    /*     printf("%d %f %f %d %d\n",d,Xr,Xi,R,I);*/
				}

				/* fprintf(out,"%d\n",op[Adr]);	*/

			       fprintf(out,"%d %d\n",R,I );
fprintf(out2,"Adr:%5ld  T:%2d (E:%2d I:%2d R:%2d) R:%3d I:%3d\n",samp-1,TableNo,e1,i1,r1,R,I);


} /* end generate */

/* ------------------------------------------- Main Program starts here ----*/
void main(int argc, char *argv[])
{
  int tno2,i,r;

  fprintf(stdout,"\nRunning : %s\n\n%s \n\n\n", argv[0], NOTE_1);

  out=fopen(OUTFILE,"wt");
  out2=fopen(OUTFILE2,"wt");
  fprintf(out2,"\nRunning : %s\n\n%s \n", argv[0], NOTE_1);
  fprintf(out2,"This doc. file Name: %s \n", OUTFILE2);
  fprintf(out2,"Corresp. TXT format PROM file Name: %s\n\n", OUTFILE) ;

  samp=1;                      /* Generate the LT values for */
  for(tno2=0;tno2<=15;tno2++)  /* all 16 Tables, for all combination of the */
    for(e1=0;e1<=15;e1++)      /*   EXP part, */
      for(i=0;i<=15;i++)       /*   Imaginary part, */
	for(r=0;r<=15;r++)     /*  and Real parts. */
	{  /*
	     The few lines below, converts the 4-bit sign magnitude real and
	     imaginary parts (from FFT) to a positive and negative number.
	     This is required for the "convert()" function.
	   */
		 if((r&0x8)==0x8)
		     r1 = -(r&0x7);
		 else  r1 = r;

		 if((i&0x8)==0x8)
		     i1 = -(i&0x7);
		 else  i1 = i;
		/*------------------*/
//	printf("T: %3d  -----%3d   %3d\n",tno2,r1,i1);

	   convert(r1,i1,e1,tno2,samp);
	   samp++;
	}

  printf("\nSaved all values\nExiting\n\n");
  fcloseall();
} /* ---------------------------------------- End of main Program -------- */








