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

3G开发

开发平台:

Visual C++

  1. //
  2. //  File = rate_changer_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 "rate_changer_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. //  Constructor
  23. template< class T >
  24. RateChanger< T >::RateChanger( char* instance_name,
  25.                                 PracSimModel* outer_model,
  26.                                 Signal<T>* in_signal,
  27.                                 Signal<T>* out_signal )
  28.               :PracSimModel(instance_name,
  29.                             outer_model)
  30. {  
  31.    MODEL_NAME(RateChanger);
  32.    ENABLE_MULTIRATE;
  33.    ENABLE_CONST_INTERVAL;
  34.    ActiveModel = this;
  35.    //-----------------------------------
  36.    // Read configuration parameters
  37.    OPEN_PARM_BLOCK;
  38.    GET_INT_PARM(Num_Sidelobes);
  39.    GET_DOUBLE_PARM(Rate_Change_Factor);
  40.    //-----------------------------------
  41.    //  Signals
  42.    In_Sig = in_signal;
  43.    Out_Sig = out_signal;
  44.    MAKE_INPUT( In_Sig );
  45.    MAKE_OUTPUT( Out_Sig );
  46.    // one sample per bit at input
  47.    CHANGE_RATE( In_Sig, Out_Sig, Rate_Change_Factor );
  48.   return;
  49.   }
  50. //================================================
  51. template< class T >
  52. RateChanger<T>::~RateChanger( void ){ };
  53. //=======================================================
  54. template< class T >
  55. void RateChanger<T>::Initialize()
  56. {
  57. //  int idx;
  58.   //---------------------------------------
  59.   //  Initialize derived parameters
  60.   Samp_Intvl = In_Sig->GetSampIntvl();
  61.   Out_Samp_Intvl = Samp_Intvl/Rate_Change_Factor;
  62.   Nom_Block_Size = In_Sig->GetBlockSize();
  63.   //---------------------------------------
  64.   //  Initialize physical buffer
  65.   Max_Buffer_Len = 2*Nom_Block_Size;
  66.   Start_Of_Buffer = new T[Max_Buffer_Len];
  67.   for(int i=0; i<Max_Buffer_Len; i++)
  68.     {
  69.     *(Start_Of_Buffer+i) = 1e6;
  70.     }
  71.   End_Of_Buffer = Start_Of_Buffer + Max_Buffer_Len - 1;
  72.   Write_Ptr = Start_Of_Buffer;
  73.   Read_Ptr_Start = Start_Of_Buffer + Max_Buffer_Len - Num_Sidelobes;
  74.   Rt_Brack_Avail = 0;
  75.   Sinc_Val = new float[2*Num_Sidelobes];
  76. }
  77. //=======================================
  78. template< class T >
  79. int RateChanger<T>::Execute()
  80. {
  81.   T *start_of_buffer, *end_of_buffer;
  82.   T *read_ptr, *write_ptr;
  83.   T input_samp, *input_signal_ptr;
  84.   T *output_signal_ptr;
  85.   T *output_base;
  86.   T sum;
  87.   T zero_T;
  88.   T *ptr_to_center;
  89.   int num_sidelobes;
  90.   int max_buf_len;
  91.   int is, input_block_size;
  92.   int output_block_size;
  93.   double samp_intvl;
  94.   float *sinc_val;
  95.   int idx;
  96.   double sinc_offset;
  97.   double rt_brack_needed;
  98.   double samp_inst;
  99.   int num_inputs_avail;
  100.   int rt_brack_avail;
  101.   int beg_samp_count;
  102.   int iflag;
  103.   bool input_has_been_read;
  104.   //-----------------------------------------
  105.   // Get pointers for input and output signals
  106.   
  107.   output_signal_ptr = GET_OUTPUT_PTR(Out_Sig);
  108.   output_base = GET_OUTPUT_PTR(Out_Sig);
  109.   input_signal_ptr = GET_INPUT_PTR(In_Sig);
  110.   input_block_size = In_Sig->GetValidBlockSize();
  111.   *DebugFile << "input_block_size = " << input_block_size << endl;
  112.   //------------------------------------------------
  113.   //  copy frequently used items into local storage
  114.   //block_size = Max_Block_Size;
  115.   samp_intvl = Samp_Intvl;
  116.   read_ptr = Read_Ptr;
  117.   write_ptr = Write_Ptr;
  118.   start_of_buffer = Start_Of_Buffer;
  119.   end_of_buffer = End_Of_Buffer;
  120.   sinc_val = Sinc_Val;
  121. //    case INTERP_MODE_SINC:
  122.       num_sidelobes = Num_Sidelobes;
  123.       max_buf_len = Max_Buffer_Len;
  124.       rt_brack_avail = Rt_Brack_Avail;
  125.       num_inputs_avail = input_block_size;
  126.       if(PassNumber == 1)
  127.       {
  128.         //----------------------------------
  129.         // preload enough input samples to
  130.         // accomodate non-causal tail of sinc window
  131.         for(is=Num_Sidelobes; is>=0; is--)
  132.         {
  133.           *(end_of_buffer-is) = 0.0;
  134.          // Buffer_Fullness++;
  135.         }
  136.         for(is=0; is<Num_Sidelobes; is++)
  137.         {
  138.           input_samp = *input_signal_ptr++;
  139.           *write_ptr++ = input_samp;
  140.           //Buffer_Fullness++;
  141.           num_inputs_avail--;
  142.         }
  143.         Out_Samp_Count = 0;
  144.         rt_brack_avail = 0;
  145.       }
  146.       //------------------------------------
  147.       // processing for every pass
  148.       beg_samp_count = Out_Samp_Count;
  149.       while(num_inputs_avail >= 0)
  150.       {
  151.         samp_inst = Out_Samp_Count*Out_Samp_Intvl/Samp_Intvl;
  152.         rt_brack_needed = ceil(samp_inst);
  153.         if(rt_brack_needed <= rt_brack_avail)
  154.         {
  155.           if(samp_inst == rt_brack_needed)
  156.           {
  157.             // rt_bracket sample is the desired output
  158.             // no interpolation needed
  159.             ptr_to_center = Read_Ptr_Start + Num_Sidelobes;
  160.             if(ptr_to_center > end_of_buffer) ptr_to_center -= Max_Buffer_Len;
  161.             sum = *ptr_to_center;
  162.           }
  163.           else
  164.           {
  165.             // interploation is needed
  166.             // desired output falls between rt_bracket and rt_bracket-1
  167.             // generate sinc window
  168.             sinc_offset = rt_brack_needed - samp_inst;
  169.             for(idx=0; idx < 2*Num_Sidelobes; idx++)
  170.               {
  171.               sinc_val[idx] = float(sinc( idx + sinc_offset-Num_Sidelobes));
  172.               }
  173.             sinc_val[0] = float(0.6*sinc_val[0]);
  174.             sinc_val[2*Num_Sidelobes-1] = float(0.6*sinc_val[2*Num_Sidelobes-1]);
  175.             //---------------------------------------
  176.             read_ptr = Read_Ptr_Start;
  177.             //-----------------------------------------------
  178.             sum = 0.0;
  179.             zero_T = 0.0;
  180.             for(int sum_idx=0; sum_idx< 2*Num_Sidelobes; sum_idx++)
  181.             {
  182.               sum += (*read_ptr) * sinc_val[sum_idx];
  183.               read_ptr++;
  184.               if(read_ptr > end_of_buffer)
  185.               { 
  186.                 read_ptr = start_of_buffer;
  187.                 *DebugFile << "read_ptr wrapped" << endl;
  188.               }
  189.             }
  190.           }
  191.           Out_Samp_Count++;
  192.           if(Out_Samp_Count == 2016)
  193.           {
  194.             iflag = 1;
  195.           }
  196.           *output_signal_ptr++ = sum;
  197.           input_has_been_read = false;
  198.         } // end of output generation
  199.         else
  200.         {
  201.           // need to read some more inputs
  202.           // are any avail?
  203.           if(num_inputs_avail > 0)
  204.           {
  205.             input_samp = *input_signal_ptr++;
  206.             *write_ptr++ = input_samp;
  207.             if(write_ptr > end_of_buffer) 
  208.             {
  209.               write_ptr = start_of_buffer;
  210.             }
  211.             rt_brack_avail++;
  212.             Read_Ptr_Start++;
  213.             if(Read_Ptr_Start > end_of_buffer) Read_Ptr_Start = start_of_buffer;
  214.           }
  215.           num_inputs_avail--;
  216.           input_has_been_read = true;
  217.         }
  218.       }
  219.       output_block_size = Out_Samp_Count - beg_samp_count;
  220.       Out_Sig->SetValidBlockSize(output_block_size);
  221.       *DebugFile << "delayed output block size = " << output_block_size << endl;
  222.       if(input_has_been_read)
  223.       {
  224.         *DebugFile << "input_has_been_read = true" << endl;
  225.       }
  226.       else
  227.       {
  228.         *DebugFile << "input_has_been_read = false" << endl;
  229.       }
  230. //      break;
  231.   Rt_Brack_Avail = rt_brack_avail;
  232.   Read_Ptr = read_ptr;
  233.   Write_Ptr = write_ptr;
  234.   return(_MES_AOK);
  235. }
  236. template RateChanger< int >;
  237. template RateChanger< float >;
  238. template RateChanger< std::complex<float> >;