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

3G开发

开发平台:

Visual C++

  1. //
  2. //  File = nonlinear_amp.cpp
  3. //
  4. #include <stdlib.h>
  5. #include <fstream>
  6. #include <strstream>
  7. #include "parmfile.h"
  8. #include "nonlinear_amp.h"
  9. #include "model_graph.h"
  10. extern ParmFile *ParmInput;
  11. //======================================================
  12. NonlinearAmplifier::NonlinearAmplifier( 
  13.                      char* instance_name,
  14.                      PracSimModel* outer_model,
  15.                      Signal< complex<float> >* in_sig,
  16.                      Signal< complex<float> >* out_sig )
  17.              :PracSimModel(instance_name,
  18.                            outer_model)
  19. {
  20.   MODEL_NAME(NonlinearAmplifier);
  21.   //  Read model config parms
  22.    OPEN_PARM_BLOCK;
  23.    GET_DOUBLE_PARM(Output_Power_Scale_Factor);
  24.    GET_DOUBLE_PARM(Phase_Scale_Factor);
  25.    GET_DOUBLE_PARM(Anticipated_Input_Power);
  26.    GET_DOUBLE_PARM(Operating_Point);
  27.    GET_DOUBLE_PARM(Agc_Time_Constant);
  28.    Input_Power_Scale_Factor = 
  29.          float(Operating_Point/Anticipated_Input_Power);
  30.    Am_Am_Fname = new char[64];
  31.    strcpy(Am_Am_Fname, "");
  32.    GET_STRING_PARM(Am_Am_Fname);
  33.    Am_Pm_Fname = new char[64];
  34.    strcpy(Am_Pm_Fname, "");
  35.    GET_STRING_PARM(Am_Pm_Fname);
  36.   //  Connect input and output signals
  37.   In_Sig = in_sig;
  38.   Out_Sig = out_sig;
  39.   MAKE_OUTPUT( Out_Sig );
  40.   MAKE_INPUT( In_Sig );
  41.   Am_Am_Curve = new SampledCurve(Am_Am_Fname);
  42.   Am_Pm_Curve = new SampledCurve(Am_Pm_Fname);
  43.   }
  44. //======================================================
  45. void NonlinearAmplifier::Initialize(void)
  46. {
  47.   double samp_intvl = Out_Sig->GetSampIntvl();
  48.   Out_Avg_Block_Size = Out_Sig->GetBlockSize();
  49. }
  50. //======================================================
  51. NonlinearAmplifier::~NonlinearAmplifier( void ){ };
  52. //======================================================
  53. int NonlinearAmplifier::Execute()
  54. {
  55.    complex<float>  *out_sig_ptr, out_sig;
  56.    complex<float>  *in_sig_ptr, in_sig;
  57.    complex<float>  agc_in_sig;
  58.    float power, power_out;
  59.    float input_phase;
  60.    double phase_shift;
  61.    double amplitude;
  62.    double phase_out;
  63.    double sum_in, sum_out;
  64.    double amp_sqrd;
  65.    double avg_power_in, avg_power_out;
  66.    int block_size, is;
  67.    block_size = In_Sig->GetValidBlockSize();
  68.    Out_Sig->SetValidBlockSize(block_size);
  69.    //-------------------------------------------------
  70.    out_sig_ptr = GET_OUTPUT_PTR( Out_Sig );
  71.    in_sig_ptr = GET_INPUT_PTR( In_Sig );      
  72.    block_size = In_Sig->GetValidBlockSize();
  73.    Out_Sig->SetValidBlockSize(block_size);
  74.    sum_in = 0.0;
  75.    sum_out = 0.0;
  76.    for(is=0; is<block_size; is++){
  77.       in_sig = *in_sig_ptr++;
  78.       agc_in_sig = in_sig;
  79.       amp_sqrd = std::norm(agc_in_sig);
  80.       sum_in += amp_sqrd;
  81.       power = float(0.5 * Input_Power_Scale_Factor *
  82.                      amp_sqrd);
  83.       if(power == 0.0){
  84.          input_phase = 0.0;
  85.       }
  86.       else{
  87.          input_phase = std::arg(agc_in_sig);
  88.       }
  89.       power_out = float(Output_Power_Scale_Factor *
  90.                      Am_Am_Curve->GetValue(power));
  91.       sum_out += power_out;
  92.       phase_shift = Am_Pm_Curve->GetValue(power);
  93.       amplitude = sqrt(2.0*power_out);
  94.       phase_out = input_phase + 
  95.          Phase_Scale_Factor*phase_shift;
  96.       out_sig = complex<float>(  
  97.             float(amplitude*cos(phase_out)),
  98.             float(amplitude*sin(phase_out)));
  99.       *out_sig_ptr++ = out_sig;
  100.    }
  101.    avg_power_out = sum_out/block_size;
  102.    avg_power_in = sum_in/block_size/2;
  103.    BasicResults 
  104.       << "In NonlinearAmplifier avg input power = " 
  105.       << avg_power_in << endl;
  106.    BasicResults 
  107.       << "                      avg output power = " 
  108.       << avg_power_out << endl;
  109.    return(_MES_AOK);
  110. }