contin_delay2_T.cpp
上传用户:jtjnyq9001
上传日期:2014-11-21
资源大小:3974k
文件大小:16k
源码类别:

3G开发

开发平台:

Visual C++

  1. //
  2. //  File = contin_delay2_T.cpp
  3. //
  4. #include <stdlib.h>
  5. #include <fstream>
  6. #include <strstream>
  7. #include "parmfile.h"
  8. #include "sigplot.h"
  9. #include "model_error.h"
  10. #include "contin_delay2_T.h"
  11. #include "model_graph.h"
  12. #include "complex_io.h"
  13. #include "sinc.h"
  14. extern SignalPlotter SigPlot;
  15. extern int PassNumber;
  16. extern int EnclaveNumber;
  17. extern int EnclaveOffset[10];
  18. extern ParmFile *ParmInput;
  19. extern ofstream *DebugFile;
  20. extern PracSimModel *ActiveModel;
  21. template< class T >
  22. int ContinuousDelay2< T >::Instance_Count=0;
  23. //==================================================================
  24. // general constructor that supports any of the possible delay modes
  25. template< class T >
  26. ContinuousDelay2< T >::ContinuousDelay2( char* instance_name,
  27.                                 PracSimModel* outer_model,
  28.                                 Signal<T>* in_signal,
  29.                                 Signal<T>* out_signal,
  30.                                 Control<float> *new_delay,
  31.                                 Control<bool> *delay_change_enabled )
  32.               :PracSimModel(instance_name,
  33.                             outer_model)
  34. {  
  35.   this->Constructor_Common_Tasks( instance_name, in_signal, out_signal);
  36.   //------------------------------------------
  37.   //  Controls
  38.   New_Delay = new_delay;
  39.   Delay_Change_Enabled = delay_change_enabled;
  40.   return;
  41.   }
  42. //==================================================================
  43. //  Constructor 2
  44. //  This constructor supports any delay mode except DELAY_MODE_GATED
  45. //  (The calling sequence does not pass the gating control.)
  46. template< class T >
  47. ContinuousDelay2< T >::ContinuousDelay2( char* instance_name,
  48.                                 PracSimModel* outer_model,
  49.                                 Signal<T>* in_signal,
  50.                                 Signal<T>* out_signal,
  51.                                 Control<float> *new_delay )
  52.               :PracSimModel(instance_name,
  53.                             outer_model)
  54. {  
  55.   this->Constructor_Common_Tasks( instance_name, in_signal, out_signal);
  56.   //------------------------------------------
  57.   //  Controls
  58.   New_Delay = new_delay;
  59.   switch (Delay_Mode)
  60.     {
  61.     case DELAY_MODE_NONE:
  62.     case DELAY_MODE_FIXED:
  63.     case DELAY_MODE_DYNAMIC:
  64.       break;
  65.     case DELAY_MODE_GATED:
  66.       {
  67.       ostrstream temp_stream;
  68.       temp_stream << "DELAY_MODE_GATED is not supported by called constructor (2)"
  69.                    << ends;
  70.       char *message = temp_stream.str();
  71.       PsModelError(FATAL, message);
  72.       delete []message;
  73.       }
  74.       break;
  75.     }
  76.   return;
  77.   }
  78. //======================================================================
  79. //  Constructor 3
  80. //  This constructor supports only DELAY_MODE_NONE and DELAY_MODE_FIXED
  81. //  (The calling sequence does not pass the delay control or 
  82. //   the gating control.)
  83. template< class T >
  84. ContinuousDelay2< T >::ContinuousDelay2( char* instance_name,
  85.                                 PracSimModel* outer_model,
  86.                                 Signal<T>* in_signal,
  87.                                 Signal<T>* out_signal )
  88.               :PracSimModel(instance_name,
  89.                             outer_model)
  90. {  
  91.   this->Constructor_Common_Tasks( instance_name, in_signal, out_signal);
  92.   switch (Delay_Mode)
  93.     {
  94.     case DELAY_MODE_NONE:
  95.     case DELAY_MODE_FIXED:
  96.       break;
  97.     case DELAY_MODE_DYNAMIC:
  98.       {
  99.       ostrstream temp_stream;
  100.       temp_stream << "DELAY_MODE_DYNAMIC is not supported by called contructor (3)"
  101.                    << ends;
  102.       char *message = temp_stream.str();
  103.       PsModelError(FATAL, message);
  104.       delete []message;
  105.       }
  106.       break;
  107.     case DELAY_MODE_GATED:
  108.       {
  109.       ostrstream temp_stream;
  110.       temp_stream << "DELAY_MODE_GATED is not supported by called constructor (3)"
  111.                    << ends;
  112.       char *message = temp_stream.str();
  113.       PsModelError(FATAL, message);
  114.       delete []message;
  115.       }
  116.       break;
  117.     }
  118.   return;
  119.   }
  120. //===================================================================
  121. template< class T >
  122. void ContinuousDelay2< T >::Constructor_Common_Tasks( char* instance_name,
  123.                                               Signal<T>* in_signal,
  124.                                               Signal<T>* out_signal)
  125. {
  126.    MODEL_NAME(ContinuousDelay);
  127.    ActiveModel = this;
  128.    Instance_Count++;
  129.    Instance_Number = Instance_Count;
  130.   //-----------------------------------
  131.   // Read configuration parameters
  132.   OPEN_PARM_BLOCK;
  133.   Delay_Mode = GetDelayModeParm("Delay_Mode");
  134.   BasicResults << "   " << "Delay_Mode = " << Delay_Mode << endl;
  135.   Interp_Mode = GetInterpModeParm("Interp_Mode");
  136.   BasicResults << "   " << "Interp_Mode = " << Interp_Mode << endl;
  137.   if( Interp_Mode == INTERP_MODE_SINC )
  138.     {
  139.     GET_INT_PARM(Num_Sidelobes);
  140.     }
  141.   else
  142.     {
  143.     Num_Sidelobes = 0;
  144.     }
  145.   GET_DOUBLE_PARM(Max_Delay);
  146.   GET_DOUBLE_PARM(Initial_Delay);
  147.   //-----------------------------------
  148.   //  Signals
  149.   In_Sig = in_signal;
  150.   Out_Sig = out_signal;
  151.   MAKE_INPUT( In_Sig );
  152.   //EnclaveNumber++; // must come after MAKE_INPUT and before MAKE_OUTPUT
  153.   MAKE_OUTPUT( Out_Sig );
  154. };
  155. //================================================
  156. template< class T >
  157. ContinuousDelay2<T>::~ContinuousDelay2( void ){ };
  158. //=======================================================
  159. template< class T >
  160. void ContinuousDelay2<T>::Initialize()
  161. {
  162.   double active_delay_in_samps;
  163.   double sinc_offset;
  164.   int idx;
  165.   //---------------------------------------
  166.   //  Initialize derived parameters
  167.   Samp_Intvl = In_Sig->GetSampIntvl();
  168.   Nom_Block_Size = In_Sig->GetBlockSize();
  169.   //---------------------------------------
  170.   //  Initialize physical buffer
  171.   Blocks_Of_Offset = 1;
  172.   //---------------------------------------
  173.   //  Initialize active portion of buffer
  174.   Active_Delay = Initial_Delay;
  175.   Longest_Active_Delay = Initial_Delay;
  176.   sinc_offset = ceil(Active_Delay/Samp_Intvl) - (Active_Delay/Samp_Intvl);
  177.   Offset_Sum_Start_To_Out_Samp = Num_Sidelobes + int(ceil(Active_Delay/Samp_Intvl))-1;
  178.   //Offset_Sum_Start_To_Out_Samp = Num_Sidelobes + int(ceil(Active_Delay/Samp_Intvl));
  179.   //Max_Buffer_Len = Nom_Block_Size + Offset_Sum_Start_To_Out_Samp+1;
  180.   Max_Buffer_Len = 2*Nom_Block_Size;
  181.   Start_Of_Buffer = new T[Max_Buffer_Len];
  182.   for(int i=0; i<Max_Buffer_Len; i++)
  183.     {
  184.     //*(Start_Of_Buffer+i) = 1e6;
  185.     *(Start_Of_Buffer+i) = 0.0;
  186.     }
  187.   active_delay_in_samps = Active_Delay/Samp_Intvl;
  188.   Delay_Ceil = int(ceil(active_delay_in_samps));
  189.   Noncausal_Samps = Num_Sidelobes - Delay_Ceil;
  190.   if(Noncausal_Samps < 0) Noncausal_Samps=0;
  191.   Longest_Noncausal_Samps = Noncausal_Samps;
  192.   if(Active_Delay == 0) Longest_Noncausal_Samps = 0;
  193.   Preamb_Len = Delay_Ceil - Num_Sidelobes;
  194.   Preamb_Samps_Remaining = Preamb_Len;
  195.   Preamb_Blocks_Remaining = int(ceil(Preamb_Len/Nom_Block_Size));
  196.   Active_Buffer_Len = 1 + int(floor(active_delay_in_samps));
  197.   Interp_Weight = active_delay_in_samps - floor(active_delay_in_samps); //correct one
  198.   if(Interp_Weight > 0.999999) Interp_Weight = 1.0;
  199.   if(Interp_Weight < 0.000001) Interp_Weight = 0.0;
  200.   One_Minus_Weight = 1.0 - Interp_Weight;
  201.   End_Of_Buffer = Start_Of_Buffer + Max_Buffer_Len - 1;
  202.   Write_Ptr = Start_Of_Buffer;
  203.   //Write_Ptr = End_Of_Buffer;
  204.   Read_Ptr_Start = Start_Of_Buffer + Max_Buffer_Len - Offset_Sum_Start_To_Out_Samp;
  205.   //Read_Ptr_Start = Start_Of_Buffer;
  206.   Num_Blocks_Skipped = 0;
  207.   Sinc_Val = new float[2*Num_Sidelobes];
  208. //  if(sinc_offset==0.0)
  209. //  {
  210.  //     Sinc_Val
  211. //  }
  212. //  else
  213. //  {
  214.      double sum = 0.0;
  215.      double sum_sqrs = 0.0;
  216.      for(idx=0; idx < 2*Num_Sidelobes; idx++)
  217.        {
  218.        Sinc_Val[idx] = float(sinc(Num_Sidelobes - 1 - idx + sinc_offset));
  219.        sum += Sinc_Val[idx];
  220.        sum_sqrs += Sinc_Val[idx] * Sinc_Val[idx];
  221.        //Sinc_Val[idx] = 1.0/float(2*Num_Sidelobes);
  222.        }
  223.      Sinc_Val[0] = float(0.6*Sinc_Val[0]);
  224.      Sinc_Val[2*Num_Sidelobes-1] = float(0.6*Sinc_Val[2*Num_Sidelobes-1]);
  225. //  }
  226. }
  227. //=======================================
  228. template< class T >
  229. int ContinuousDelay2<T>::Execute()
  230. {
  231.   T *start_of_buffer, *end_of_buffer;
  232.   T *read_ptr, *write_ptr;
  233.   T input_samp, *input_signal_ptr;
  234.   T output_samp, *output_signal_ptr;
  235.   T left_samp, right_samp;
  236.   T sum;
  237.   T zero_T;
  238.   T *oldest_window_loc_ptr;
  239.   double actv_dly_in_samps;
  240.   int num_sidelobes;
  241.   int max_buf_len;
  242.   int is, input_block_size;
  243.   int output_block_size;
  244.   double samp_intvl;
  245.   double sinc_offset;
  246.   float *sinc_val;
  247.   float interp_weight, one_minus_weight;
  248.   int avail_output_len;
  249.   int output_idx;
  250.   int idx;
  251.   int output_block_start;
  252.   int old_offset;
  253.   bool noncausal_samps_has_increased;
  254.   bool debug_flag;
  255.   //-----------------------------------------
  256.   // Get pointers for input and output signals
  257.   
  258.   output_signal_ptr = GET_OUTPUT_PTR(Out_Sig);
  259.   input_signal_ptr = GET_INPUT_PTR(In_Sig);
  260.   input_block_size = In_Sig->GetValidBlockSize();
  261.   //valid_block_size = In_Sig->GetValidBlockSize();
  262.   //------------------------------------------------
  263.   //  Do actions peculiar to each delay mode
  264.   switch (Delay_Mode)
  265.     {
  266.     //- - - - - - - - - - - - - - - - - - - - - - 
  267.     case DELAY_MODE_NONE:
  268.     case DELAY_MODE_FIXED:
  269.       {
  270.       break;
  271.       }
  272.     //- - - - - - - - - - - - - - - - - - - - - - 
  273.     case DELAY_MODE_GATED:
  274.       {
  275.       //  If delay change is NOT enabled, get out of switch.
  276.       //  If delay change IS enabled, fall through to next case
  277.       //  and get new value for Active_Delay.
  278.       if( Delay_Change_Enabled->GetValue() == false )
  279.         {
  280.         break;
  281.         }
  282.       }
  283.     //- - - - - - - - - - - - - - - - - - - - - - 
  284.     case DELAY_MODE_DYNAMIC:
  285.       {
  286.       actv_dly_in_samps = 0.0;
  287.       Active_Delay = New_Delay->GetValue();
  288.       noncausal_samps_has_increased = false;
  289.       if(Active_Delay > Longest_Active_Delay)
  290.       {
  291.          actv_dly_in_samps = Active_Delay/Samp_Intvl;
  292.          Delay_Ceil = int(ceil(actv_dly_in_samps));
  293.          Noncausal_Samps = Num_Sidelobes - Delay_Ceil;
  294.          if(Noncausal_Samps > Longest_Noncausal_Samps)
  295.          {
  296.             //if(Noncausal_Samps < 0) Noncausal_Samps=0;
  297.             Longest_Noncausal_Samps = Noncausal_Samps;
  298.             noncausal_samps_has_increased = true;
  299.             old_offset = Offset_Sum_Start_To_Out_Samp;
  300.             Offset_Sum_Start_To_Out_Samp = Num_Sidelobes 
  301.                         + int(ceil(Active_Delay/Samp_Intvl))-1;
  302. //            Read_Ptr_Start = Start_Of_Buffer 
  303. //                        + Max_Buffer_Len - Offset_Sum_Start_To_Out_Samp;
  304.             Read_Ptr_Start += old_offset - Offset_Sum_Start_To_Out_Samp;
  305.             if(Read_Ptr_Start < Start_Of_Buffer) Read_Ptr_Start += Max_Buffer_Len;
  306.             if(Read_Ptr_Start > End_Of_Buffer) Read_Ptr_Start -= Max_Buffer_Len;
  307.          }
  308.          Longest_Active_Delay = Active_Delay;
  309.       }
  310.       sinc_offset = ceil(Active_Delay/Samp_Intvl) - (Active_Delay/Samp_Intvl);
  311.       for(idx=0; idx < 2*Num_Sidelobes; idx++)
  312.       {
  313.          Sinc_Val[idx] = float(sinc(Num_Sidelobes - 1 - idx + sinc_offset));
  314.       }
  315.       Sinc_Val[0] = float(0.6*Sinc_Val[0]);
  316.       Sinc_Val[2*Num_Sidelobes-1] = float(0.6*Sinc_Val[2*Num_Sidelobes-1]);
  317.       double active_delay_in_samps = Active_Delay/Samp_Intvl;
  318.       Active_Buffer_Len = 1 + int(floor(active_delay_in_samps));
  319.       Interp_Weight = active_delay_in_samps - floor(active_delay_in_samps);
  320.       if(Interp_Weight > 0.999999) Interp_Weight = 1.0;
  321.       if(Interp_Weight < 0.000001) Interp_Weight = 0.0;
  322.       One_Minus_Weight = 1.0 - Interp_Weight;
  323.       break;
  324.       }
  325.     } // end of switch on Delay_Mode
  326.   //------------------------------------------------
  327.   //  copy frequently used items into local storage
  328.   samp_intvl = Samp_Intvl;
  329.   write_ptr = Write_Ptr;
  330.   start_of_buffer = Start_Of_Buffer;
  331.   end_of_buffer = End_Of_Buffer;
  332.   interp_weight = Interp_Weight;
  333.   one_minus_weight = One_Minus_Weight;
  334.   sinc_val = Sinc_Val;
  335.   //---------------------------------------------------
  336.   //  error condition if active delay exceeds max delay
  337.   if(Active_Delay > Max_Delay)
  338.     {
  339.     ostrstream temp_stream;
  340.     temp_stream << "Active_Delay (" << Active_Delay
  341.                  << ") is greater than Max_Delay ("
  342.                  << Max_Delay << ")." << ends;
  343.     char *message = temp_stream.str();
  344.     PsModelError(FATAL, message);
  345.     delete []message;
  346.     }
  347.   //--------------------------------------------------
  348.   //  normal sample processing
  349.   switch (Interp_Mode)
  350.     {
  351.     case INTERP_MODE_LINEAR:
  352.       for(is=0; is<input_block_size; is++)
  353.         {
  354.         input_samp = *input_signal_ptr++;
  355.         //---------------------------------------------------------------
  356.         // get samps that bracket desired time instant from delay buffer
  357.   
  358.         left_samp = *read_ptr++;
  359.         if(read_ptr > end_of_buffer) read_ptr = start_of_buffer;
  360.         if(Active_Delay < Samp_Intvl)
  361.           right_samp = input_samp;
  362.         else
  363.           right_samp = *read_ptr;
  364.         //------------------------------------------
  365.         // do interpolation to get output value
  366.         output_samp = interp_weight*left_samp + one_minus_weight*right_samp;
  367.         //------------------------------------
  368.         // put input sample into delay buffer
  369.         *write_ptr++ = input_samp;
  370.         if(write_ptr > end_of_buffer) write_ptr = start_of_buffer;
  371.         *output_signal_ptr++ =output_samp;
  372.         }
  373.       break;
  374.     case INTERP_MODE_SINC:
  375.       num_sidelobes = Num_Sidelobes;
  376.       max_buf_len = Max_Buffer_Len;
  377.       if( (PassNumber == 1) || noncausal_samps_has_increased)
  378.       {
  379.         //----------------------------------
  380.         // preload enough input samples to
  381.         // accomodate non-causal tail of sinc window
  382.         for(is=0; is<Noncausal_Samps; is++)
  383.         {
  384.           input_samp = *input_signal_ptr++;
  385.           if(input_samp > 100.0)
  386.           {
  387.           debug_flag = true;
  388.           }
  389.           *write_ptr++ = input_samp;
  390.           if(write_ptr > end_of_buffer) 
  391.           {
  392.             write_ptr = start_of_buffer;
  393.           }
  394.         }
  395.         output_block_size = input_block_size - Noncausal_Samps;
  396.         output_block_start = Noncausal_Samps;
  397.       }
  398.       else
  399.       {
  400.         output_block_size = input_block_size;
  401.         output_block_start = 0;
  402.       }
  403.          if((Instance_Number == 2) && (PassNumber >= 10)) (*DebugFile)<<"NEW BLOCK" << endl;
  404.       for(is=0; is<output_block_size; is++)
  405.         {
  406.         input_samp = *input_signal_ptr++;
  407.           if(input_samp > 100.0)
  408.           {
  409.           debug_flag = true;
  410.           }
  411.         //------------------------------------
  412.         // put input sample into delay buffer
  413.         *write_ptr++ = input_samp;
  414.         if(write_ptr > end_of_buffer) 
  415.           {
  416.           write_ptr = start_of_buffer;
  417.           }
  418.         //----------------------------------
  419.         read_ptr = Read_Ptr_Start;
  420.         oldest_window_loc_ptr = Read_Ptr_Start;
  421.         Read_Ptr_Start++;
  422.         if(Read_Ptr_Start > end_of_buffer) Read_Ptr_Start = start_of_buffer;
  423.         sum = 0.0;
  424.         zero_T = 0.0;
  425.         for(int sum_idx=0; sum_idx< 2*Num_Sidelobes; sum_idx++)
  426.           {
  427.           sum += (*read_ptr) * sinc_val[sum_idx];
  428.           read_ptr++;
  429.           if(read_ptr > end_of_buffer) read_ptr = start_of_buffer;
  430.           }
  431.         //*oldest_window_loc_ptr = 10e6;
  432.         *output_signal_ptr++ = sum;
  433.         avail_output_len--;
  434.         output_idx++;
  435.         }
  436.       Out_Sig->SetValidBlockSize(output_block_size);
  437.       *DebugFile << "delayed output block size = " << output_block_size << endl;
  438.       break;
  439.   default:
  440.     ostrstream temp_stream;
  441.     temp_stream << "Requested interpolation mode is not supported"
  442.                  << ends;
  443.     char *message = temp_stream.str();
  444.     PsModelError(FATAL, message);
  445.     delete []message;
  446.   } // end of switch om Interp_Mode
  447. //   }
  448. //   *(output_signal_ptr-1)=50.0;
  449.   Write_Ptr = write_ptr;
  450.   return(_MES_AOK);
  451. }
  452. //template ContinuousDelay2< int >;
  453. template ContinuousDelay2< float >;
  454. //template ContinuousDelay2< std::complex<float> >;