module: c2_packet_detect_and_slice
parameters: int data_length
inputs: double rx_a, double rx_b, Vector preamble_vec
outputs:  double out, double enable_out
static_variables: 
  int sample_offset, int look_for_preamble_flag, int chosen_sample_offset
  int count 
  double prev_ph0_val, double prev_ph1_val, double prev_ph2_val
  double prev_ph_val
classes: 
  List buf0(), List buf1(), List buf2()
  List buf0_mod(), List buf1_mod(), List buf2_mod()
  Vector signal_vec()
init: 
sample_offset = -1;
look_for_preamble_flag = 0;
prev_ph0_val = 0.0;
prev_ph1_val = 0.0;
prev_ph2_val = 0.0;
count = 0;
enable_out = 0;
out = 0.0;

code:
    double ph_val, var0, var1, var2;
    int i;

//    preamble_vec.set_length(10);

    sample_offset = (++sample_offset) % 3;


    if (look_for_preamble_flag < 2)
      {
       // assume phase is in range of -4 to 4
       //  (corresponding to -2*pi to 2*pi)

       if (fabs(rx_a) > 1e-12)
          ph_val = 2.0/pi*atan2(rx_b,rx_a);
       else
          {
	  if (rx_b > 0.0)
	     ph_val = 2.0;
          else
             ph_val = -2.0;
          }

       if (sample_offset == 0)
         {
          buf0_mod.inp(fmod(ph_val-prev_ph0_val+0.5,1));
          buf0.inp(fmod(ph_val-prev_ph0_val+0.5,4));
          prev_ph0_val = ph_val;
          if (buf0.length > preamble_vec.get_length())
	    {
             buf0.remove_first_entry();
             buf0_mod.remove_first_entry();
	    }
         }
       else if (sample_offset == 1)
         {
          buf1_mod.inp(fmod(ph_val-prev_ph1_val+0.5,1));
          buf1.inp(fmod(ph_val-prev_ph1_val+0.5,4));
          prev_ph1_val = ph_val;
          if (buf1.length > preamble_vec.get_length())
	    {
             buf1.remove_first_entry();
             buf1_mod.remove_first_entry();
	    }
         }
       else if (sample_offset == 2)
         {
          buf2_mod.inp(fmod(ph_val-prev_ph2_val+0.5,1));
          buf2.inp(fmod(ph_val-prev_ph2_val+0.5,4));
          prev_ph2_val = ph_val;
          if (buf2.length > preamble_vec.get_length())
	    {
             buf2.remove_first_entry();
             buf2_mod.remove_first_entry();
	     look_for_preamble_flag = 1;
	    }
         }

       if (look_for_preamble_flag == 1 && sample_offset == 2)
         {
	  var0 = buf0_mod.var();
	  var1 = buf1_mod.var();
	  var2 = buf2_mod.var();
          if (var0 <= var1 && var0 <= var2)
	    {
	    chosen_sample_offset = 0;
	    prev_ph_val = prev_ph0_val;
	    copy(buf0,signal_vec);
	    }
          else if (var1 <= var0 && var1 <= var2)
	    {
	    chosen_sample_offset = 1;
	    prev_ph_val = prev_ph1_val;
	    copy(buf1,signal_vec);
	    }
          else if (var2 <= var0 && var2 <= var1)
	    {
	    chosen_sample_offset = 2;
	    prev_ph_val = prev_ph2_val;
	    copy(buf2,signal_vec);
	    }


          for (i = 0; i < signal_vec.get_length(); i++)
	    {
            if (floor(signal_vec.get_elem(i) - preamble_vec.get_elem(i)) != 0.0)
	       break;
	    }
          if (i == signal_vec.get_length())
	     look_for_preamble_flag = 2;
         }
      }
    else
      { 
       if (sample_offset == chosen_sample_offset)
         {
          // assume phase is in range of -4 to 4
          //  (corresponding to -2*pi to 2*pi)

          if (fabs(rx_a) > 1e-12)
             ph_val = 2.0/pi*atan2(rx_b,rx_a);
          else
             {
	     if (rx_b > 0.0)
	        ph_val = 2.0;
             else
                ph_val = -2.0;
             }

	  if (count >= data_length)
            {
            sample_offset = -1;
            look_for_preamble_flag = 0;
            prev_ph0_val = 0.0;
            prev_ph1_val = 0.0;
            prev_ph2_val = 0.0;
            count = 0;
            enable_out = 0;
            out = 0.0;
	    stop_current_alter_run = 1;
            }
	  count++;

          out = floor(fmod(ph_val-prev_ph_val+0.5,4));
	  prev_ph_val = ph_val;
          enable_out = 1;
	 } 
       else
	  enable_out = 0;
      }


