/* 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.

  OUTPUT FILE NAME : r2table2.bin

	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>
#include <conio.h>
#define  stdprn NULL
#define  getch() ;


#define  Q_LEVELS 64  /* Power 6 bits; hence 64 Quantization levels */
#define  P003 0.003
#define  TABELS 16

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

void  generate();

	double x,y,e,bl,P[Q_LEVELS],
		  actual_x,
		  PWR_SAT_U,
		  PWR_SAT_L,
		  old_level,
		  step_size, PwrU[16] ;
	int lines,a,b,c,d,Adr,i,TabelNo,op[4096] ;
	int ma,mb ; /* magnitudes */
	FILE *in;


void main()
{

	clrscr();
	printf("              GAC  POWER LOOK-UP GENERATOR      \n") ;

	in=fopen("r2table2.bin","wb");     /*  Oct 20-96    */


	/* 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] = (P003*e) ;
	      printf("Table %d    at   %14.5f    %14.5f\n",i,PwrU[i],e) ;
	 }

	 PwrU[7] =P003 ;    /* 7th Table operates at the P003 power level */
			    /* Ooty table */

	 printf("\nTable 7           at   %14.5f       1\n\n",PwrU[7] ) ;
	 e= 1.0 ;
	 for(i=6;i>=1;i--)
	 {    e = e*sqrt(2.0) ;
	      PwrU[i] = P003/e ;
	      printf("Table %d    at   %14.5f    %14.5f\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  */

	 printf("\nTable 0           at   %14.5f     Pol Mask\n\n",PwrU[0] ) ;

	/* Generate the table contents */
	for(TabelNo =0; TabelNo<=15;TabelNo++)
	{
	    PWR_SAT_U =  PwrU[TabelNo]    ; /*  15 Mr 96    */
	    PWR_SAT_L =  0.0000                  ;
	    printf("\n\n\nTable  %d Upper Saturation  %4.15f\n",TabelNo,PWR_SAT_U) ;

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

	    generate();

	} /* end of for */

	fclose(in);

}  /* end of main   */


void generate()
{

	bl = (PWR_SAT_U - PWR_SAT_L) / Q_LEVELS ; /* Find the STEP Size */

	printf("Upper Value  %4.15f   Lower Value  %4.15f\n\n",PWR_SAT_U, PWR_SAT_L);
	printf("Step size    %4.15f   Half value   %4.15f (for rounding)\n\n", bl, bl/2.0000000000 );
	printf("No of Quantization Levels %d \n",Q_LEVELS );

	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 ;
	for(c=0; c<(Q_LEVELS); c++)
	{
		P[c] = PWR_SAT_L + (c+1)*bl ;
		printf("%x	 %4.15f    \n",c,P[c]);
		fprintf(stdprn,"%x	%4.15f\n",c,P[c]);
		x = P[c] ;
	}
//	printf("\nPausing......\n");
	getch();

//	fprintf(stdprn,"\n\nAdr\tActual\t\tProm\n");
	lines =0;

	printf("\n\nAdr\tEXP\tIMAG\tREAL\t Actual_x \t  x       \t Prom\n");

	/* Computing the Power Values & the Lookup values     */
	for(c=0;c<=15;c++)
	{	for(b=0;b<=15;b++)
		{	/* IMAGINARY part  varies from 0x0 to 0xf 	   */
			/* Sign bit is also used for Address generation          */

			for(a=0;a<=15;a++)
			{   /* REAL part varies from 0x0 to 0xf 	   */
			    /* Sign bit is also used for Address generation   */

				Adr =((256*c)+(16*b)+a);
				op[Adr]=0;
				ma = a&0x7 ;   /* sign bits removed */
				mb = b&0x7 ;   /*       -do-       */
				actual_x = ((ma*ma)+(mb*mb))/pow(2,(2*c)) ;

				/* Half step size  is added for rounding-off     */

				x = actual_x + (bl/2.0000000000000) ;

				for(d=0; d<Q_LEVELS; d++)
				 {  if( x >= P[d])
				    op[Adr]=d;
				  }

				 if (lines==64 )
				 { // printf("\nPausing......\n");
					getch();
//					fprintf(stdprn,"\n\nAdr\tEXP\tIMAG\tREAL\tActual\tRounded\t\tProm\n");
//					printf("\n\nAdr\tEXP\tIMAG\tREAL\t Actual_x \t  x        \tProm\n");
					lines=0;
				 }
				 lines++;

				 printf("%2d %x\t %x\t %x\t %x\t %4.10f\t  %4.10f\t %x    \t\n",TabelNo,Adr, c, b, a, actual_x, x, op[Adr]);

//				 fprintf(stdprn,"%x\t %4.10f\t %x    \t\n",Adr,x,op[Adr]);

				 fwrite(&op[Adr],1,1,in);


			}  /* end of for(a=0;a<=7;a++)        */


		} /* end of for(b=0;b<=7;b++)        */
	} /* end of for(c=0;c<=15;c++)       */
 } /* end generate */

 /* End of Program          */