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

3G开发

开发平台:

Visual C++

  1. //
  2. //  File = discrete_advance2.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 "discrete_adv_T.h"
  11. #include "model_graph.h"
  12. extern ParmFile *ParmInput;
  13. extern SignalPlotter SigPlot;
  14. extern int PassNumber;
  15. extern int EnclaveNumber;
  16. extern int EnclaveOffset[10];
  17. extern PracSimModel *ActiveModel;
  18. #ifdef _DEBUG
  19.    extern ofstream *DebugFile;
  20. #endif
  21. //======================================================
  22. // general constructor that supports any of the possible 
  23. // advance modes
  24. template< class T >
  25. DiscreteAdvance< T >::DiscreteAdvance( 
  26.                     char* instance_name,
  27.                     PracSimModel* outer_model,
  28.                     Signal<T>* in_sig,
  29.                     Signal<T>* out_sig,
  30.                     Control<int>* dynam_adv_cntrl,
  31.                     Control<bool>* adv_chg_enab_cntrl )
  32.               :PracSimModel(instance_name,
  33.                             outer_model)
  34. {
  35.    this->Constructor_Common_Tasks( instance_name, 
  36.                                   in_sig, out_sig);
  37.    //---------------------------------------------------
  38.    // Controls
  39.    Dynam_Adv_Cntrl = dynam_adv_cntrl;
  40.    Adv_Chg_Enab_Cntrl = adv_chg_enab_cntrl;
  41. };
  42. //======================================================
  43. //  Constructor 2
  44. //  This constructor supports any advance mode except 
  45. //  ADVANCE_MODE_GATED (The calling sequence does not 
  46. //  pass the gating control.)
  47. template< class T >
  48. DiscreteAdvance< T >::DiscreteAdvance(
  49.                        char* instance_name,
  50.                        PracSimModel* outer_model,
  51.                        Signal<T>* in_sig,
  52.                        Signal<T>* out_sig,
  53.                        Control<int>* dynam_adv_cntrl )
  54.               :PracSimModel(instance_name,
  55.                             outer_model)
  56. {
  57.    this->Constructor_Common_Tasks( instance_name, 
  58.                                    in_sig, out_sig);
  59.    //---------------------------------------------------
  60.    // Controls
  61.    Dynam_Adv_Cntrl = dynam_adv_cntrl;
  62.    if(Advance_Mode == ADVANCE_MODE_GATED){
  63.       ostrstream temp_stream;
  64.       temp_stream << "ADVANCE_MODE_GATED is not " 
  65.          << "supported by called constructor (2)"
  66.          << ends;
  67.       char *message = temp_stream.str();
  68.       PsModelError(FATAL, message);
  69.       delete []message;
  70.    }
  71. };
  72. //======================================================
  73. //  Constructor 3
  74. //  This constructor supports only ADVANCE_MODE_NONE 
  75. //  and ADVANCE_MODE_FIXED (The calling sequence does 
  76. //  not pass the advance control or the gating control.)
  77. template< class T >
  78. DiscreteAdvance< T >::DiscreteAdvance(
  79.                              char* instance_name,
  80.                              PracSimModel* outer_model,
  81.                              Signal<T>* in_sig,
  82.                              Signal<T>* out_sig )
  83.               :PracSimModel( instance_name,
  84.                              outer_model)
  85. {
  86.    this->Constructor_Common_Tasks( instance_name, 
  87.       in_sig, out_sig);
  88.    //---------------------------------------------------
  89.    // Controls
  90.    if(Advance_Mode == ADVANCE_MODE_DYNAMIC){
  91.       ostrstream temp_stream;
  92.       temp_stream << "ADVANCE_MODE_DYNAMIC is not " 
  93.          << "supported by called constructor (3)"
  94.          << ends;
  95.       char *message = temp_stream.str();
  96.       PsModelError(FATAL, message);
  97.       delete []message;
  98.    }
  99.    if(Advance_Mode == ADVANCE_MODE_GATED){
  100.       ostrstream temp_stream;
  101.       temp_stream << "ADVANCE_MODE_GATED is not " 
  102.          << "supported by called constructor (3)"
  103.          << ends;
  104.       char *message = temp_stream.str();
  105.       PsModelError(FATAL, message);
  106.       delete []message;
  107.    }
  108. };
  109. //======================================================
  110. template< class T >
  111. void DiscreteAdvance< T >
  112.       ::Constructor_Common_Tasks( char *instance_name,
  113.                                   Signal<T> *in_sig,
  114.                                   Signal<T> *out_sig)
  115. {
  116.    MODEL_NAME(DiscreteAdvance);
  117.    ActiveModel = this;
  118.    OPEN_PARM_BLOCK;
  119.    Advance_Mode = GetAdvanceModeParm("Advance_Mode");
  120.    BasicResults << "   " << "Advance_Mode = " 
  121.                 << Advance_Mode << endl;
  122.    GET_INT_PARM(Initial_Adv_In_Samps);
  123.    switch (Advance_Mode){
  124.    case ADVANCE_MODE_NONE:
  125.    case ADVANCE_MODE_FIXED:
  126.       Max_Adv_In_Samps = Initial_Adv_In_Samps;
  127.       Num_Initial_Passes = 0;
  128.       break;
  129.    case ADVANCE_MODE_DYNAMIC:
  130.    case ADVANCE_MODE_GATED:
  131.       GET_INT_PARM(Max_Adv_In_Samps);
  132.       GET_INT_PARM(Num_Initial_Passes);
  133.       break;
  134.    }
  135.    // Signals
  136.    In_Sig = in_sig;
  137.    Out_Sig = out_sig;
  138.    MAKE_INPUT(In_Sig);
  139.    EnclaveNumber++; // must come after MAKE_INPUT and 
  140.                     // before MAKE_OUTPUT
  141.    MAKE_OUTPUT(Out_Sig);
  142. };
  143. //======================================================
  144. template< class T >
  145. DiscreteAdvance< T >::~DiscreteAdvance( void ){ };
  146. //======================================================
  147. template< class T >
  148. void DiscreteAdvance< T >::Initialize( void )
  149. {
  150.   EnclaveNumber++;
  151.   Block_Size = In_Sig->GetBlockSize();
  152.   Return_Status = _MES_RESTART;
  153.   New_Pass_Number = 0;
  154.   //-----------------------------------------------------
  155.   //  initialize internal buffer
  156.   Blocks_Of_Offset = 
  157.                int(ceil(double(Max_Adv_In_Samps)/
  158.                double(Block_Size)));
  159.   EnclaveOffset[EnclaveNumber]=Blocks_Of_Offset;
  160.   Max_Buffer_Len = Block_Size * Blocks_Of_Offset;
  161.   Start_Of_Buffer = new T[Max_Buffer_Len];
  162.   for(int i=0; i<Max_Buffer_Len; i++)
  163.     {
  164.     *(Start_Of_Buffer+i) = 0;
  165.     }
  166.   End_Of_Buffer = Start_Of_Buffer + Max_Buffer_Len - 1;
  167.   Write_Ptr = Start_Of_Buffer;
  168.   Active_Adv_In_Samps = Initial_Adv_In_Samps;
  169.   Read_Ptr = Start_Of_Buffer + Active_Adv_In_Samps;
  170.   Num_Blocks_Skipped = 0;
  171. };
  172. //======================================================
  173. template< class T >
  174. int DiscreteAdvance< T >::Execute()
  175. {
  176.   T *start_of_buffer, *end_of_buffer;
  177.   T *in_sig_ptr, *out_sig_ptr;
  178.   T *read_ptr, *write_ptr;
  179.   T in_samp, out_samp;
  180.   int is, block_size;
  181.   // collect plotting data for signals in current 
  182.   // block-synchronous enclave before incrementing 
  183.   // to next enclave
  184.   SigPlot.CollectData();
  185.   EnclaveNumber++;
  186.   *DebugFile << "moved to Enclave " << EnclaveNumber << endl;
  187.   //-------------------------------------------------------------
  188.   //  Get pointers for input and output signals
  189.   out_sig_ptr = GET_OUTPUT_PTR(Out_Sig);
  190.   in_sig_ptr = GET_INPUT_PTR(In_Sig);
  191.   block_size = In_Sig->GetValidBlockSize();
  192.   Out_Sig->SetValidBlockSize(block_size);
  193.   //Remainder_Samps_To_Adv = Total_Samps_Adv % Block_Size;
  194.   //Whole_Blocks_To_Adv = (Total_Samps_Adv - Remainder_Samps_To_Adv) / Block_Size;
  195.   //Buf_Len = (Block_Size - Remainder_Samps_To_Adv);
  196.   //-----------------------------------------------------------
  197.   //  do actions peculiar to each advance mode
  198.   switch (Advance_Mode)
  199.     {
  200.     //- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  201.     case ADVANCE_MODE_NONE:
  202.       {
  203.       //...copy input directly to output
  204.       for(is=0; is<Block_Size; is++)
  205.         {
  206.         in_samp = *in_sig_ptr++;
  207.         *out_sig_ptr++ = in_samp;
  208.         }
  209.       return(_MES_AOK);
  210.       }
  211.     //- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  212.     case ADVANCE_MODE_FIXED:
  213.       break;
  214.     //- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  215.     case ADVANCE_MODE_GATED:
  216.       {
  217.       //  If advance change is NOT enabled, get out of switch.
  218.       //  If delay change is enabled, fall through to next case
  219.       //  and get new value for Active_Advance
  220.       if(Adv_Chg_Enab_Cntrl->GetValue() == false )
  221.         {
  222.         break;
  223.         }
  224.       }
  225.     //- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  226.     case ADVANCE_MODE_DYNAMIC:
  227.       {
  228.       if( PassNumber > Num_Initial_Passes)
  229.         {
  230.         Active_Adv_In_Samps = Dynam_Adv_Cntrl->GetValue();
  231.         if(Active_Adv_In_Samps < 0) Active_Adv_In_Samps = 0;
  232.         //Read_Ptr = Write_Ptr - (Max_Adv_In_Samps - Active_Adv_In_Samps);
  233.         Read_Ptr = Write_Ptr - (Max_Buffer_Len - Active_Adv_In_Samps);
  234.         if(Read_Ptr < Start_Of_Buffer) Read_Ptr += Max_Buffer_Len;
  235.         }
  236.       }
  237.     } //end of switch on Advance_mode
  238.   //-----------------------------------------------------------
  239.   //  copy frequently used items into local storage
  240.   //block_size = Block_Size;
  241.   read_ptr = Read_Ptr;
  242.   write_ptr = Write_Ptr;
  243.   start_of_buffer = Start_Of_Buffer;
  244.   end_of_buffer = End_Of_Buffer;
  245.   //------------------------------------------------------
  246.   //----------------------------------------------------------------
  247.   // if the number of blocks already loaded into buffer is less than 
  248.   // the offset between the input enclave and output enclave, then
  249.   // the current input block needs to be loaded into buffer 
  250.   if(Num_Blocks_Skipped < Blocks_Of_Offset)
  251.     {
  252.     for(is=0; is<block_size; is++)
  253.       {
  254.       in_samp = *in_sig_ptr++;
  255.       *write_ptr++ = in_samp;
  256.       }
  257.     if(write_ptr > end_of_buffer) write_ptr = start_of_buffer;
  258.     Num_Blocks_Skipped++;
  259.     }
  260.   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  261.   // if the number of blocks already loaded into the buffer equals 
  262.   // or exceeds the offset between the input enclave and output enclave,
  263.   // then everything is ready for normal operation
  264.   else
  265.     {
  266.     Return_Status = _MES_AOK;
  267.     for(is=0; is<block_size; is++)
  268.       {
  269.       in_samp = *in_sig_ptr++;
  270.       out_samp = *read_ptr++;
  271.       if(read_ptr > end_of_buffer) read_ptr = start_of_buffer;
  272.       *write_ptr++ = in_samp;
  273.       if(write_ptr > end_of_buffer) write_ptr = start_of_buffer;
  274.       *out_sig_ptr++ = out_samp;
  275.       }
  276.     New_Pass_Number++;
  277.     }
  278.   PassNumber = New_Pass_Number;
  279.   Read_Ptr = read_ptr;
  280.   Write_Ptr = write_ptr;
  281.   return(Return_Status);
  282. }
  283. template DiscreteAdvance< int >;
  284. template DiscreteAdvance< float >;