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

3G开发

开发平台:

Visual C++

  1. //
  2. //  File = discrete_delay_T.cpp
  3. //
  4. #include <stdlib.h>
  5. #include <strstream>
  6. #include "parmfile.h"
  7. #include "model_error.h"
  8. #include "discrete_delay_T.h"
  9. #include "model_graph.h"
  10. extern ParmFile *ParmInput;
  11. extern PracSimModel *ActiveModel;
  12. extern int PassNumber;
  13. using namespace std;
  14. //======================================================
  15. // general constructor that supports any of the possible 
  16. // delay modes
  17. template< class T >
  18. DiscreteDelay< T >::DiscreteDelay( 
  19.                   char* instance_name,
  20.                   PracSimModel* outer_model,
  21.                   Signal<T>* in_sig,
  22.                   Signal<T>* out_sig,
  23.                   Control<int> *dynam_dly_ctl,
  24.                   Control<bool> *delay_chg_enab_ctl )
  25.            :PracSimModel( instance_name,
  26.                           outer_model)
  27. {  
  28.    MODEL_NAME(DiscreteDelay_T);
  29.    this->Constructor_Common_Tasks( instance_name, 
  30.                                    in_sig, out_sig);
  31.    //---------------------------
  32.    //  Controls
  33.    Dynam_Dly_Ctl = dynam_dly_ctl;
  34.    Delay_Chg_Enab_Ctl = delay_chg_enab_ctl;
  35.    return;
  36. }
  37. //======================================================
  38. //  Constructor 2
  39. //  This constructor supports any delay mode except 
  40. //  DELAY_MODE_GATED (The calling sequence does not 
  41. //  pass the gating control.)
  42. template< class T >
  43. DiscreteDelay< T >::DiscreteDelay( 
  44.                           char* instance_name,
  45.                           PracSimModel* outer_model,
  46.                           Signal<T>* in_signal,
  47.                           Signal<T>* out_signal,
  48.                           Control<int> *dynam_dly_ctl )
  49.               :PracSimModel(instance_name,
  50.                             outer_model)
  51. {  
  52.    this->Constructor_Common_Tasks(  instance_name, 
  53.                                     in_signal, 
  54.                                     out_signal);
  55.    //------------------------------------------
  56.    //  Controls
  57.    Dynam_Dly_Ctl = dynam_dly_ctl;
  58.    switch (Delay_Mode){
  59.     case DELAY_MODE_NONE:
  60.     case DELAY_MODE_FIXED:
  61.     case DELAY_MODE_DYNAMIC:
  62.        break;
  63.     case DELAY_MODE_GATED:
  64.        ostrstream temp_stream;
  65.        temp_stream << "DELAY_MODE_GATED is not " 
  66.                << "supported by called constructor (2)"
  67.                << ends;
  68.        char *message = temp_stream.str();
  69.        PsModelError(FATAL, message);
  70.        delete []message;
  71.        break;
  72.    }
  73.    return;
  74. }
  75. //======================================================
  76. //  Constructor 3
  77. //  This constructor supports only DELAY_MODE_NONE and 
  78. //  DELAY_MODE_FIXED  (The calling sequence does not 
  79. //  pass the delay control or the gating control.)
  80. template< class T >
  81. DiscreteDelay< T >
  82.             ::DiscreteDelay( char* instance_name,
  83.                              PracSimModel* outer_model,
  84.                              Signal<T>* in_signal,
  85.                              Signal<T>* out_signal )
  86.               :PracSimModel(instance_name,
  87.                             outer_model)
  88. {  
  89.    this->Constructor_Common_Tasks(  instance_name, 
  90.                                     in_signal, 
  91.                                     out_signal);
  92.    char *message;
  93.    ostrstream temp_stream;
  94.    switch (Delay_Mode){
  95.     case DELAY_MODE_NONE:
  96.     case DELAY_MODE_FIXED:
  97.        break;
  98.     case DELAY_MODE_DYNAMIC:
  99.        temp_stream << "DELAY_MODE_DYNAMIC is not " 
  100.           << "supported by called contructor (3)"
  101.           << ends;
  102.        message = temp_stream.str();
  103.        PsModelError(FATAL, message);
  104.        delete []message;
  105.        break;
  106.     case DELAY_MODE_GATED:
  107.        temp_stream << "DELAY_MODE_GATED is not " 
  108.           << "supported by called constructor (3)"
  109.           << ends;
  110.        message = temp_stream.str();
  111.        PsModelError(FATAL, message);
  112.        delete []message;
  113.        break;
  114.    }
  115.    return;
  116. }
  117. //======================================================
  118. template< class T >
  119. void DiscreteDelay< T >
  120.       ::Constructor_Common_Tasks( char* instance_name,
  121.                                   Signal<T>* in_signal,
  122.                                   Signal<T>* out_signal)
  123. {
  124.    MODEL_NAME(DiscreteDelay);
  125.    ActiveModel = this;
  126.    //-----------------------------------
  127.    // Read configuration parameters
  128.    OPEN_PARM_BLOCK;
  129.    Delay_Mode = GetDelayModeParm("Delay_Mode");
  130.    BasicResults << "   " << "Delay_Mode = " 
  131.                 << Delay_Mode << endl;
  132.    GET_INT_PARM(Initial_Delay_In_Samps);
  133.    Dynamic_Delay_Bias = 0;
  134.    switch (Delay_Mode){
  135.     case DELAY_MODE_NONE:
  136.     case DELAY_MODE_FIXED:
  137.        Max_Delay_In_Samps = Initial_Delay_In_Samps;
  138.        Num_Initial_Passes = 0;
  139.        break;
  140.     case DELAY_MODE_DYNAMIC:
  141.     case DELAY_MODE_GATED:
  142.        GET_INT_PARM(Dynamic_Delay_Bias);
  143.        GET_INT_PARM(Max_Delay_In_Samps);
  144.        GET_INT_PARM(Num_Initial_Passes);
  145.        break;
  146.    }
  147.    //-----------------------------------
  148.    //  Signals
  149.    In_Sig = in_signal;
  150.    Out_Sig = out_signal;
  151.    MAKE_OUTPUT( Out_Sig );
  152.    MAKE_INPUT( In_Sig );
  153. };
  154. //======================================================
  155. template< class T >
  156. DiscreteDelay<T>::~DiscreteDelay( void ){ };
  157. //======================================================
  158. template< class T >
  159. void DiscreteDelay<T>::Initialize()
  160. {
  161.    //---------------------------------------------------
  162.    //  Initialize derived parameters
  163.    Samp_Intvl = In_Sig->GetSampIntvl();
  164.    Block_Size = In_Sig->GetBlockSize();
  165.    //---------------------------------------------------
  166.    //  Initialize physical buffer
  167.    Max_Buffer_Len = Max_Delay_In_Samps;
  168.    Start_Of_Buffer = new T[Max_Buffer_Len];
  169.    for(int i=0; i<Max_Buffer_Len; i++){
  170.       *(Start_Of_Buffer+i) = 0;
  171.    }
  172.    //---------------------------------------------------
  173.    //  Initialize active portion of buffer
  174.    Active_Delay_In_Samps = Initial_Delay_In_Samps;
  175.    End_Of_Buffer = Start_Of_Buffer + Max_Buffer_Len - 1;
  176.    Write_Ptr = Start_Of_Buffer;
  177.    Read_Ptr = Start_Of_Buffer + Max_Buffer_Len - 
  178.                                  Active_Delay_In_Samps;
  179. }
  180. //======================================================
  181. template< class T >
  182. int DiscreteDelay<T>::Execute()
  183. {
  184.   T *start_of_buffer, *end_of_buffer;
  185.   T *read_ptr, *write_ptr;
  186.   T input_samp, *input_signal_ptr;
  187.   T output_samp, *output_signal_ptr;
  188.   int is, block_size;
  189.   //-----------------------------------------
  190.   // Get pointers for input and output signals
  191.   
  192.   output_signal_ptr = GET_OUTPUT_PTR(Out_Sig);
  193.   input_signal_ptr = GET_INPUT_PTR(In_Sig);
  194.   block_size = In_Sig->GetValidBlockSize();
  195.   Out_Sig->SetValidBlockSize(block_size);
  196.   //------------------------------------------------
  197.   //  Do actions peculiar to each delay mode
  198.    switch (Delay_Mode){
  199.    case DELAY_MODE_NONE:
  200.       for(is=0; is<Block_Size; is++){
  201.          input_samp = *input_signal_ptr++;
  202.          *output_signal_ptr++ =input_samp;
  203.       }
  204.       return(_MES_AOK);
  205.    case DELAY_MODE_FIXED:
  206.       break;
  207.    case DELAY_MODE_GATED:
  208.       //  If delay change is NOT enabled, get out of
  209.       //  switch.  If delay change IS enabled, fall 
  210.       //  through to next case and get new value for 
  211.       //  Active_Delay.
  212.       if( Delay_Chg_Enab_Ctl->GetValue() == false ){
  213.          break;
  214.       }
  215.    case DELAY_MODE_DYNAMIC:
  216.       if( PassNumber > Num_Initial_Passes ){
  217.          Active_Delay_In_Samps = 
  218.                         Dynam_Dly_Ctl->GetValue();
  219.          Active_Delay_In_Samps += Dynamic_Delay_Bias;
  220.          Read_Ptr = Write_Ptr - Active_Delay_In_Samps;
  221.          if(Read_Ptr < Start_Of_Buffer) Read_Ptr += 
  222.                                     Max_Buffer_Len;
  223.       }
  224.       break;
  225.    } // end of switch on Delay_Mode
  226.    //------------------------------------------------
  227.    //  copy frequently used items into local storage
  228.    block_size = Block_Size;
  229.    read_ptr = Read_Ptr;
  230.    write_ptr = Write_Ptr;
  231.    start_of_buffer = Start_Of_Buffer;
  232.    end_of_buffer = End_Of_Buffer;
  233.    //---------------------------------------------------
  234.    // if active delay is zero, just copy input to output
  235.    if(Active_Delay_In_Samps == 0){
  236.       for(is=0; is<block_size; is++){
  237.          input_samp = *input_signal_ptr++;
  238.          *output_signal_ptr++ =input_samp;
  239.       }
  240.       return(_MES_AOK);
  241.    }
  242.   //---------------------------------------------------
  243.   //  error condition if active delay exceeds max delay
  244.    if(Active_Delay_In_Samps > Max_Delay_In_Samps){
  245.       ostrstream temp_stream;
  246.       temp_stream 
  247.             << "Active_Delay_In_Samps (" 
  248.             << Active_Delay_In_Samps
  249.             << ") is greater than Max_Delay_In_Samps ("
  250.             << Max_Delay_In_Samps << ")." << endl;
  251.       char *message = temp_stream.str();
  252.       PsModelError(FATAL, message);
  253.       delete []message;
  254.    }
  255.    //  normal processing loop
  256.    for(is=0; is<block_size; is++){
  257.       input_samp = *input_signal_ptr++;
  258.       // get output samp from delay buffer    
  259.       output_samp = *read_ptr++;
  260.       if(read_ptr > end_of_buffer) read_ptr = 
  261.                                     start_of_buffer;
  262.       // put input sample into delay buffer
  263.       *write_ptr++ = input_samp;
  264.       if(write_ptr > end_of_buffer) write_ptr = 
  265.                                     start_of_buffer;
  266.       *output_signal_ptr++ =output_samp;
  267.    } // end of main loop
  268.    Read_Ptr = read_ptr;
  269.    Write_Ptr = write_ptr;
  270.    return(_MES_AOK);
  271. }
  272. template DiscreteDelay< int >;
  273. template DiscreteDelay< float >;
  274. template DiscreteDelay< bit_t >;