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

3G开发

开发平台:

Visual C++

  1. //
  2. //  File = contin_adv_T.cpp
  3. //
  4. #include <stdlib.h>
  5. #include <fstream>
  6. #include <strstream>
  7. #include "parmfile.h"
  8. #include "model_error.h"
  9. #include "contin_adv_T.h"
  10. #include "model_graph.h"
  11. #include "sinc.h"
  12. extern ParmFile *ParmInput;
  13. extern PracSimModel *ActiveModel;
  14. //==================================================================
  15. // general constructor that supports any of the possible delay modes
  16. template< class T >
  17. ContinuousAdvance< T >::ContinuousAdvance( char* instance_name,
  18.                                 PracSimModel* outer_model,
  19.                                 Signal<T>* in_signal,
  20.                                 Signal<T>* out_signal,
  21.                                 Control<float> *new_advance,
  22.                                 Control<bool> *advance_change_enabled )
  23.               :PracSimModel(instance_name,
  24.                             outer_model)
  25. {  
  26.   this->Constructor_Common_Tasks( instance_name, in_signal, out_signal);
  27.   //------------------------------------------
  28.   //  Controls
  29.   New_Advance = new_advance;
  30.   Advance_Change_Enabled = advance_change_enabled;
  31.   return;
  32.   }
  33. //==================================================================
  34. //  Constructor 2
  35. //  This constructor supports any delay mode except DELAY_MODE_GATED
  36. //  (The calling sequence does not pass the gating control.)
  37. template< class T >
  38. ContinuousAdvance< T >::ContinuousAdvance( char* instance_name,
  39.                                 PracSimModel* outer_model,
  40.                                 Signal<T>* in_signal,
  41.                                 Signal<T>* out_signal,
  42.                                 Control<float> *new_advance )
  43.               :PracSimModel(instance_name,
  44.                             outer_model)
  45. {  
  46.   this->Constructor_Common_Tasks( instance_name, in_signal, out_signal);
  47.   //------------------------------------------
  48.   //  Controls
  49.   New_Advance = new_advance;
  50.   switch (Advance_Mode)
  51.     {
  52.     case ADVANCE_MODE_NONE:
  53.     case ADVANCE_MODE_FIXED:
  54.     case ADVANCE_MODE_DYNAMIC:
  55.       break;
  56.     case ADVANCE_MODE_GATED:
  57.       {
  58.       ostrstream temp_stream;
  59.       temp_stream << "ADVANCE_MODE_GATED is not supported by called constructor (2)"
  60.                    << ends;
  61.       char *message = temp_stream.str();
  62.       PsModelError(FATAL, message);
  63.       delete []message;
  64.       }
  65.       break;
  66.     }
  67.   return;
  68.   }
  69. //======================================================================
  70. //  Constructor 3
  71. //  This constructor supports only ADVANCE_MODE_NONE and ADVANCE_MODE_FIXED
  72. //  (The calling sequence does not pass the delay control or 
  73. //   the gating control.)
  74. template< class T >
  75. ContinuousAdvance< T >::ContinuousAdvance( char* instance_name,
  76.                                 PracSimModel* outer_model,
  77.                                 Signal<T>* in_signal,
  78.                                 Signal<T>* out_signal )
  79.               :PracSimModel(instance_name,
  80.                             outer_model)
  81. {  
  82.   this->Constructor_Common_Tasks( instance_name, in_signal, out_signal);
  83.   switch (Advance_Mode)
  84.     {
  85.     case ADVANCE_MODE_NONE:
  86.     case ADVANCE_MODE_FIXED:
  87.       break;
  88.     case ADVANCE_MODE_DYNAMIC:
  89.       {
  90.       ostrstream temp_stream;
  91.       temp_stream << "ADVANCE_MODE_DYNAMIC is not supported by called contructor (3)"
  92.                    << ends;
  93.       char *message = temp_stream.str();
  94.       PsModelError(FATAL, message);
  95.       delete []message;
  96.       }
  97.       break;
  98.     case ADVANCE_MODE_GATED:
  99.       {
  100.       ostrstream temp_stream;
  101.       temp_stream << "ADVANCE_MODE_GATED is not supported by called constructor (3)"
  102.                    << ends;
  103.       char *message = temp_stream.str();
  104.       PsModelError(FATAL, message);
  105.       delete []message;
  106.       }
  107.       break;
  108.     }
  109.   return;
  110.   }
  111. //===================================================================
  112. template< class T >
  113. void ContinuousAdvance< T >::Constructor_Common_Tasks( char* instance_name,
  114.                                               Signal<T>* in_signal,
  115.                                               Signal<T>* out_signal)
  116. {
  117.   MODEL_NAME(ContinuousAdvance);
  118.   ActiveModel = this;
  119.   //-----------------------------------
  120.   // Read configuration parameters
  121.   OPEN_PARM_BLOCK;
  122.   Advance_Mode = GetAdvanceModeParm("Advance_Mode");
  123.   BasicResults << "   " << "Advance_Mode = " << Advance_Mode << endl;
  124.   Interp_Mode = GetInterpModeParm("Interp_Mode");
  125.   BasicResults << "   " << "Interp_Mode = " << Interp_Mode << endl;
  126.   if( Interp_Mode == INTERP_MODE_SINC )
  127.     {
  128.     GET_INT_PARM(Num_Sidelobes);
  129.     }
  130.   else
  131.     {
  132.     Num_Sidelobes = 0;
  133.     }
  134.   GET_DOUBLE_PARM(Max_Advance);
  135.   GET_DOUBLE_PARM(Initial_Advance);
  136.   //-----------------------------------
  137.   //  Signals
  138.   In_Sig = in_signal;
  139.   Out_Sig = out_signal;
  140.   MAKE_OUTPUT( Out_Sig );
  141.   MAKE_INPUT( In_Sig );
  142. };
  143. //================================================
  144. template< class T >
  145. ContinuousAdvance<T>::~ContinuousAdvance( void ){ };
  146. //=======================================================
  147. template< class T >
  148. void ContinuousAdvance<T>::Initialize()
  149.   {
  150.   double active_adv_in_samps;
  151.   //---------------------------------------
  152.   //  Initialize derived parameters
  153.   Samp_Intvl = In_Sig->GetSampIntvl();
  154.   Block_Size = In_Sig->GetBlockSize();
  155.   //---------------------------------------
  156.   //  Initialize physical buffer
  157.   Max_Buffer_Len = 1 + Num_Sidelobes + int(Max_Advance / Samp_Intvl);
  158.   Start_Of_Buffer = new T[Max_Buffer_Len];
  159.   for(int i=0; i<Max_Buffer_Len; i++)
  160.     {
  161.     *(Start_Of_Buffer+i) = 0;
  162.     }
  163.   //---------------------------------------
  164.   //  Initialize active portion of buffer
  165.   Active_Advance = Initial_Advance;
  166.   active_adv_in_samps = Active_Advance/Samp_Intvl;
  167.   Active_Buffer_Len = 1 + int(floor(active_adv_in_samps));
  168.   Interp_Weight = active_adv_in_samps - floor(active_adv_in_samps); //correct one
  169.   if(Interp_Weight > 0.999999) Interp_Weight = 1.0;
  170.   if(Interp_Weight < 0.000001) Interp_Weight = 0.0;
  171.   One_Minus_Weight = 1.0 - Interp_Weight;
  172.   End_Of_Buffer = Start_Of_Buffer + Max_Buffer_Len - 1;
  173.   Write_Ptr = Start_Of_Buffer;
  174.   Read_Ptr = Start_Of_Buffer + Max_Buffer_Len - Active_Buffer_Len;
  175. }
  176. //=======================================
  177. template< class T >
  178. int ContinuousAdvance<T>::Execute()
  179. {
  180.   T *start_of_buffer, *end_of_buffer;
  181.   T *read_ptr, *write_ptr;
  182.   T input_samp, *input_signal_ptr;
  183.   T output_samp, *output_signal_ptr;
  184.   T *oldest_lead_samp_ptr, *newest_lag_samp_ptr;
  185.   T *samp_ptr;
  186.   T left_samp, right_samp;
  187.   int num_sidelobes;
  188.   int max_buffer_len;
  189.   int is, block_size;
  190.   double samp_intvl;
  191.   float interp_weight, one_minus_weight;
  192.   //-----------------------------------------
  193.   // Get pointers for input and output signals
  194.   
  195.   output_signal_ptr = GET_OUTPUT_PTR(Out_Sig);
  196.   input_signal_ptr = GET_INPUT_PTR(In_Sig);
  197.   //------------------------------------------------
  198.   //  Do actions peculiar to each delay mode
  199.   switch (Advance_Mode)
  200.     {
  201.     //- - - - - - - - - - - - - - - - - - - - - - 
  202.     case ADVANCE_MODE_NONE:
  203.       {
  204.       //...copy input directly to output
  205.       for(is=0; is<Block_Size; is++)
  206.         {
  207.         input_samp = *input_signal_ptr++;
  208.         *output_signal_ptr++ =input_samp;
  209.         }
  210.       return(_MES_AOK);
  211.       }
  212.     //- - - - - - - - - - - - - - - - - - - - - - 
  213.     case ADVANCE_MODE_FIXED:
  214.       {
  215.       break;
  216.       }
  217.     //- - - - - - - - - - - - - - - - - - - - - - 
  218.     case ADVANCE_MODE_GATED:
  219.       {
  220.       //  If advance change is NOT enabled, get out of switch.
  221.       //  If advance change IS enabled, fall through to next case
  222.       //  and get new value for Active_Delay.
  223.       if( Advance_Change_Enabled->GetValue() == false )
  224.         {
  225.         break;
  226.         }
  227.       }
  228.     //- - - - - - - - - - - - - - - - - - - - - - 
  229.     case ADVANCE_MODE_DYNAMIC:
  230.       {
  231.       Active_Advance = New_Advance->GetValue();
  232.       double active_adv_in_samps = Active_Advance/Samp_Intvl;
  233.       Active_Buffer_Len = 1 + int(floor(active_adv_in_samps));
  234.       Interp_Weight = active_adv_in_samps - floor(active_adv_in_samps);
  235.       if(Interp_Weight > 0.999999) Interp_Weight = 1.0;
  236.       if(Interp_Weight < 0.000001) Interp_Weight = 0.0;
  237.       One_Minus_Weight = 1.0 - Interp_Weight;
  238.       Read_Ptr = Write_Ptr - Active_Buffer_Len;
  239.       if(Read_Ptr < Start_Of_Buffer) Read_Ptr += Max_Buffer_Len;
  240.       break;
  241.       }
  242.     } // end of switch on Delay_Mode
  243.   //------------------------------------------------
  244.   //  copy frequently used items into local storage
  245.   block_size = Block_Size;
  246.   samp_intvl = Samp_Intvl;
  247.   read_ptr = Read_Ptr;
  248.   write_ptr = Write_Ptr;
  249.   start_of_buffer = Start_Of_Buffer;
  250.   end_of_buffer = End_Of_Buffer;
  251.   interp_weight = Interp_Weight;
  252.   one_minus_weight = One_Minus_Weight;
  253.   //-----------------------------------------------------
  254.   // if active delay is zero, just copy input to output
  255.   if(Active_Advance == 0)
  256.     {
  257.     for(is=0; is<block_size; is++)
  258.       {
  259.       input_samp = *input_signal_ptr++;
  260.       *output_signal_ptr++ =input_samp;
  261.       }
  262.     return(_MES_AOK);
  263.     }
  264.   //---------------------------------------------------
  265.   //  error condition if active delay exceeds max delay
  266.   if(Active_Advance > Max_Advance)
  267.     {
  268.     ostrstream temp_stream;
  269.     temp_stream << "Active_Advance (" << Active_Advance
  270.                  << ") is greater than Max_Advance ("
  271.                  << Max_Advance << ")." << ends;
  272.     char *message = temp_stream.str();
  273.     PsModelError(FATAL, message);
  274.     delete []message;
  275.     }
  276.   //--------------------------------------------------
  277.   //  normal sample processing
  278.   switch (Interp_Mode)
  279.     {
  280.     case INTERP_MODE_LINEAR:
  281.       for(is=0; is<block_size; is++)
  282.         {
  283.         input_samp = *input_signal_ptr++;
  284.         //---------------------------------------------------------------
  285.         // get samps that bracket desired time instant from delay buffer
  286.     
  287.         left_samp = *read_ptr++;
  288.         if(read_ptr > end_of_buffer) read_ptr = start_of_buffer;
  289.         if(Active_Advance < Samp_Intvl)
  290.           right_samp = input_samp;
  291.         else
  292.           right_samp = *read_ptr;
  293.         //------------------------------------------
  294.         // do interpolation to get output value
  295.         output_samp = interp_weight*left_samp + one_minus_weight*right_samp;
  296.         //------------------------------------
  297.         // put input sample into delay buffer
  298.         *write_ptr++ = input_samp;
  299.         if(write_ptr > end_of_buffer) write_ptr = start_of_buffer;
  300.         *output_signal_ptr++ =output_samp;
  301.         }
  302.       break;
  303.     case INTERP_MODE_SINC:
  304.       num_sidelobes = Num_Sidelobes;
  305.       max_buffer_len = Max_Buffer_Len;
  306.       for(is=0; is<block_size; is++)
  307.         {
  308.         input_samp = *input_signal_ptr++;
  309.         //---------------------------------------------------------------
  310.         // get samps that bracket desired time instant from delay buffer
  311.     
  312.         oldest_lead_samp_ptr = read_ptr - num_sidelobes + 1;
  313.         if(oldest_lead_samp_ptr < start_of_buffer) oldest_lead_samp_ptr += max_buffer_len;
  314.         left_samp = *read_ptr++;
  315.         if(read_ptr > end_of_buffer) read_ptr = start_of_buffer;
  316.         newest_lag_samp_ptr = read_ptr + max_buffer_len - 1;
  317.         if(newest_lag_samp_ptr > end_of_buffer) newest_lag_samp_ptr -=max_buffer_len; 
  318.         if(Active_Advance < Samp_Intvl)
  319.           right_samp = input_samp;
  320.         else
  321.           right_samp = *read_ptr;
  322.         //------------------------------------------
  323.         // do interpolation to get output value
  324.         output_samp = 0.0;
  325.         int lobe_idx;
  326.         //  Add in sidelobe contributions from leading samples
  327.         samp_ptr = oldest_lead_samp_ptr;
  328.         for(lobe_idx=num_sidelobes-1; lobe_idx>=0; lobe_idx--)
  329.           {
  330.           output_samp += (*samp_ptr++) * float(sinc(double(one_minus_weight + lobe_idx)));
  331.           if(samp_ptr > end_of_buffer) samp_ptr = start_of_buffer;
  332.           }
  333.         //  Add in sidelobe contributions from leading samples
  334.         for(lobe_idx=0; lobe_idx<num_sidelobes; lobe_idx++)
  335.           {
  336.           output_samp += float(sinc(double(interp_weight + lobe_idx))) * (*samp_ptr++);
  337.           if(samp_ptr > end_of_buffer) samp_ptr = start_of_buffer;
  338.           }
  339.         //------------------------------------
  340.         // put input sample into delay buffer
  341.         *write_ptr++ = input_samp;
  342.         if(write_ptr > end_of_buffer) write_ptr = start_of_buffer;
  343.         *output_signal_ptr++ =output_samp;
  344.         }
  345.       break;
  346.     default:
  347.       ostrstream temp_stream;
  348.       temp_stream << "Requested interpolation mode is not supported"
  349.                    << ends;
  350.       char *message = temp_stream.str();
  351.       PsModelError(FATAL, message);
  352.       delete []message;
  353.     } // end of switch om Interp_Mode
  354.   Read_Ptr = read_ptr;
  355.   Write_Ptr = write_ptr;
  356.   return(_MES_AOK);
  357. }
  358. template ContinuousAdvance< int >;
  359. template ContinuousAdvance< float >;
  360. template ContinuousAdvance< std::complex<float> >;