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

3G开发

开发平台:

Visual C++

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