module: amp_ph_ideal_lut_5bit_dtc25_noncenter
description: amplitude and phase LUT
             Digital Predistortion for PWM noncenter
             5bit(Fractional) input.
             Assuming 960ps period, 20ps resolution.
             0 <= bit5 < 32
             Coded by Min Park 03/24/09
             (converted 10/11/10) 
parameters:  
inputs:  double bit10, double bit5
outputs:  double dtc25out, double t_compen
classes: List list1()  
static_variables: double err int nn int kk
                  double tau double v_eq double lut[1024][32]
                  double err_q5 int ind int mm double minerrdiff
                  double T double v_ideal
                  double luttmp[1024] double amplitude[1024][24]
                  double temp double one_msb double tau_table[1024][32]
                  double pulsewidth[24];  
init:  
   list1.load("c:/CppSim/SimRuns/Pwm_polar_tx/distortion_dpa_10bit_tst/table.dat");
   list1.reset();
   for(nn=1023; nn>=0; nn--){
      luttmp[nn] = list1.read();
   }

   T= 960;

   for(mm=0; mm<1024 ; mm++){
      if(mm == 1023)
         one_msb = luttmp[mm]-luttmp[mm-1];
      else
         one_msb = luttmp[mm+1]-luttmp[mm];

      tau = 0;
      for( nn=0; nn<24 ; nn++){
         pulsewidth[nn] = tau;
         printf("tau = %e\n",pulsewidth[nn]);
         temp = pow(luttmp[mm]+one_msb*sin(3.141592*tau/T)*sin(3.141592*tau/T),2);
         temp = temp + pow(one_msb*cos(3.141592*tau/T)*sin(3.141592*tau/T),2);
         v_eq = sqrt(temp);
         amplitude[mm][nn] = v_eq;
         if (tau == 0)
            tau = 35;
         else
            tau += 20;
      }
   }

   for( kk=0; kk<1024; kk++){
      if(kk == 1023)
         one_msb = luttmp[kk]-luttmp[kk-1];
      else
         one_msb = luttmp[kk+1]-luttmp[kk];

      err_q5 = 0.0;
      for( nn=0; nn<32 ; nn++){
         v_ideal = luttmp[kk] + one_msb*err_q5;

         ind = 0;
         minerrdiff = 1e6;
         for( mm=0 ; mm < 24; mm++){
            if( fabs( v_ideal - amplitude[kk][mm]) < minerrdiff ){
               minerrdiff = fabs(v_ideal - amplitude[kk][mm]);
               ind = mm;
            }
         }
         lut[kk][nn] = (double)ind;
         tau_table[kk][nn] = pulsewidth[ind];
         err_q5 += 1.0/32.0;
      }
   }

end:  
code:  
   if( bit5 < 0 || bit5 > 32 ){
      printf("Error! bit5 is out of range. amp_ph_ideal_lut_5bit_noncenter_dtc25\n");
      printf("bit5 = %f\n",bit5);
   }
   else{
      dtc25out = lut[ (int)bit10 ][ (int)bit5 ];
      tau = tau_table[ (int)bit10 ][ (int)bit5 ];
      one_msb = luttmp[(int)bit10+1]-luttmp[(int)bit10];
      t_compen = T*1e-12/4 + T*1e-12/6.2831853*atan2( (-1)*(luttmp[(int)bit10]+one_msb*sin(3.141592*tau/T)*sin(3.141592*tau/T)),(one_msb*cos(3.141592*tau/T)*sin(3.141592*tau/T)) );
   }


functions:  
custom_classes_definition:  
custom_classes_code:  
