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

3G开发

开发平台:

Visual C++

  1. //
  2. //  File = rate_changer.cpp
  3. //
  4. #include <stdlib.h>
  5. //#include <fstream>
  6. #include "parmfile.h"
  7. #include "rate_changer.h"
  8. #include "model_graph.h"
  9. #include "sinc.h"
  10. //#include <iomanip>
  11. extern ParmFile *ParmInput;
  12. #ifdef _DEBUG
  13.   extern ofstream *DebugFile;
  14. #endif
  15. extern int PassNumber;
  16. ofstream SincFile("sinc_nnn.txt", ios::out);
  17. //======================================================
  18. RateChanger::RateChanger( char* instance_name,
  19.                         PracSimModel* outer_model,
  20.                         Signal<float>* in_sig,
  21.                         Signal<float>* out_sig )
  22.                 :PracSimModel(instance_name,
  23.                               outer_model)
  24. {
  25.   MODEL_NAME(RateChanger);
  26.   //---------------------------------------
  27.   //  Read model config parms
  28.   OPEN_PARM_BLOCK;
  29.   GET_DOUBLE_PARM( Rate_Change_Factor );
  30.   GET_INT_PARM( Num_Sidelobes );
  31.   //--------------------------------------
  32.   //  Connect input and output signals
  33.   In_Sig = in_sig;
  34.   Out_Sig = out_sig;
  35.   MAKE_OUTPUT( Out_Sig );
  36.   MAKE_INPUT( In_Sig );
  37.   }
  38. //======================================
  39. RateChanger::~RateChanger( void ){ };
  40. //=======================================
  41. void RateChanger::Initialize(void)
  42. {
  43.   //------------------
  44.   Block_Size = Out_Sig->GetBlockSize();
  45.   Num_Save_Samps = 2*Num_Sidelobes-1;
  46.   Save_Buffer = new float[Num_Save_Samps];
  47.   for(int i=0; i<Num_Save_Samps; i++)
  48.     {
  49.     Save_Buffer[i] = 0.0;
  50.     }
  51.   Cumul_Samp_Count = 0;
  52. }
  53. //=======================================================
  54. int RateChanger::Execute()
  55. {
  56.   float *out_sig_ptr;
  57.   float *in_sig_ptr;
  58.   double work;
  59.   double sum;
  60.   double sampling_offset;
  61.   int is, in_idx, sav_idx;
  62.   out_sig_ptr = GET_OUTPUT_PTR( Out_Sig );
  63.   in_sig_ptr = GET_INPUT_PTR( In_Sig );
  64.   //sampling_offset = (PassNumber-1)*Block_Size*(Rate_Change_Factor-1.0);
  65.   sampling_offset = PassNumber*Block_Size*(Rate_Change_Factor-1.0);
  66.   //-------------------------------------------------------
  67.   //  compute outputs that use inputs from previous block
  68.   *DebugFile << "Previous Block" << endl;
  69.   int neg_terms, pos_terms;
  70.   double first_sinc;
  71.   double term;
  72.   int is_rev;
  73.   #define SAMP_TO_DUMP 609
  74.   #define PASS_TO_DUMP 2
  75.   for (is=0; is<Num_Save_Samps; is++)
  76.   //for (is=Num_Save_Samps-1; is>=0; is--)
  77.     {
  78.     is_rev = Num_Save_Samps - is - 1;
  79.     sum = 0.0;
  80.     neg_terms = 0;
  81.     pos_terms = 0;
  82.     //work = double(is)- Delay_In_Samps;
  83.     //work = sampling_offset + is * Rate_Change_Factor;
  84.     work = Num_Sidelobes + sampling_offset + is_rev * Rate_Change_Factor;
  85.     first_sinc = work;
  86.     //work -= Cumul_Samp_Count;
  87.     //Cumul_Samp_Count++;
  88.     if(is==SAMP_TO_DUMP) *DebugFile << "First Loop" << endl;
  89.     //if( (is==SAMP_TO_DUMP) && (PassNumber==1) )
  90.     //          SincFile << "First Loop" << endl;
  91.     for(sav_idx=is_rev; sav_idx<Num_Save_Samps; sav_idx++)
  92.       {
  93.       term = Save_Buffer[sav_idx] * sinc(work-sav_idx);
  94.       sum += term;
  95.       if( (is==SAMP_TO_DUMP) && (PassNumber==PASS_TO_DUMP) )
  96.                  SincFile << sav_idx << ", " << setprecision(12) 
  97.                           << double(work-sav_idx) << ", "
  98.                           << sinc(work-sav_idx) << ", "
  99.                           << Save_Buffer[sav_idx] << ", "
  100.                           << term << endl;
  101.       if( (work-sav_idx) < 0.0) neg_terms++;
  102.       if( (work-sav_idx) > 0.0) pos_terms++;
  103.       }
  104.     work -= Num_Save_Samps;
  105.     if(is==SAMP_TO_DUMP) *DebugFile << "Second Loop" << endl;
  106.     //if( (is==SAMP_TO_DUMP) && (PassNumber==1) )
  107.     //          SincFile << "Second Loop" << endl;
  108.     for(in_idx=0; in_idx<=is_rev; in_idx++)
  109.       {
  110.       term = in_sig_ptr[in_idx] * sinc(work-in_idx);
  111.       sum += term;
  112.       if( (is==SAMP_TO_DUMP) && (PassNumber==PASS_TO_DUMP) )
  113.                  SincFile << in_idx << ", " << setprecision(12) 
  114.                           << double(work-in_idx) << ", "
  115.                           << sinc(work-in_idx) << ", "
  116.                           << in_sig_ptr[in_idx] << ", "
  117.                           << term << endl;
  118.       if( (work-in_idx) < 0.0) neg_terms++;
  119.       if( (work-in_idx) > 0.0) pos_terms++;
  120.       }
  121.     *DebugFile << is << " sum = " << sum << ", " << pos_terms << ", " 
  122.               << neg_terms << ", " << first_sinc << endl;
  123.     *out_sig_ptr++ = sum;
  124.     //if(is==2) exit(0);
  125.     }
  126.   //----------------------------------------------------------
  127.   //  compute outputs that use only inputs from current block
  128.   *DebugFile << "Current Block" << endl;
  129.   //if( (is==SAMP_TO_DUMP) && (PassNumber==1) )
  130.   //          SincFile << "Current Block" << endl;
  131.   for (is=Num_Sidelobes-1; is<Block_Size-Num_Sidelobes; is++)
  132.     {
  133.     sum = 0.0;
  134.     neg_terms = 0;
  135.     pos_terms = 0;
  136.     //work = double(is)- Delay_In_Samps;
  137.     work = sampling_offset + is * Rate_Change_Factor;
  138.     ///work = 1 + sampling_offset + is * Rate_Change_Factor;
  139.     //work -= Cumul_Samp_Count;
  140.     //Cumul_Samp_Count++;
  141.     first_sinc = work - (is - Num_Sidelobes + 1);
  142.     //for(  in_idx = is - Num_Sidelobes + 1;
  143.     for(  in_idx = is - Num_Sidelobes + 1;
  144.           in_idx <= is + Num_Sidelobes;
  145.           in_idx++)
  146.       {
  147.       term = in_sig_ptr[in_idx] * sinc(work-in_idx);
  148.       sum += term;
  149.       if( (is==((Num_Sidelobes-1)+0)) && (PassNumber==PASS_TO_DUMP) )
  150.                 SincFile << in_idx << ", " << setprecision(12) 
  151.                           << double(work-in_idx) << ", "
  152.                           << sinc(work-in_idx) << ", "
  153.                           << in_sig_ptr[in_idx] << ", "
  154.                           << term << endl;
  155.       if( (work-in_idx) < 0.0) neg_terms++;
  156.       if( (work-in_idx) > 0.0) pos_terms++;
  157.       }
  158.     *DebugFile << is << "sum = " << sum << ", " << pos_terms << ", "
  159.               << neg_terms << ", " << first_sinc << endl;
  160.     *out_sig_ptr++ = sum;
  161. //    *out_sig_ptr++ = 0.0;
  162.     }
  163.   //---------------------------------------------------------
  164.   // save the input samples needed for next pass
  165.   for(sav_idx=0; sav_idx<Num_Save_Samps; sav_idx++)
  166.     {
  167.     Save_Buffer[sav_idx] = in_sig_ptr[Block_Size-Num_Save_Samps+sav_idx];
  168.     }
  169.   return(_MES_AOK);
  170. }