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

3G开发

开发平台:

Visual C++

  1. //
  2. //  File = linear_pll.cpp
  3. //
  4. //#include <stdlib.h>
  5. //#include <fstream.h>
  6. #include "parmfile.h"
  7. #include "misdefs.h"
  8. #include "model_error.h"
  9. #include "linear_pll.h"
  10. #include "butt_filt_iir.h"
  11. #include "model_graph.h"
  12. //#include "sinc.h"
  13. extern ParmFile *ParmInput;
  14. extern PracSimModel *ActiveModel;
  15. //======================================================
  16. LinearPLL::LinearPLL( char* instance_name,
  17.                         PracSimModel* outer_model,
  18.                         Signal<float>* fsig_input,
  19.                         Signal<float>* fsig_phase_error,
  20.                         Signal<float>* fsig_filtered_error,
  21.                         Signal<float>* fsig_vco_output,
  22.                         Signal<float>* vco_freq_sig,
  23.                         Signal<float>* ref_phase_sig,
  24.                         Signal<float>* out_sig )
  25.                 :PracSimModel(instance_name,
  26.                               outer_model)
  27. {
  28.    MODEL_NAME(LinearPLL);
  29.    //---------------------------------------
  30.    //  Read model config parms
  31.    OPEN_PARM_BLOCK;
  32.    GET_DOUBLE_PARM(K_Sub_D);
  33.    GET_DOUBLE_PARM(K_Sub_0);
  34.    GET_DOUBLE_PARM(Center_Freq_Hz);
  35.    GET_BOOL_PARM(m_UsingDco);
  36.    GET_DOUBLE_PARM(Scaler_Divisor);
  37.    //--------------------------------------
  38.    //  Connect input and output signals
  39.    fsig_Input = fsig_input;
  40.    fsig_Phase_Error = fsig_phase_error;
  41.    fsig_Filtered_Error = fsig_filtered_error;
  42.    fsig_Osc_Output = fsig_vco_output;
  43.    fsig_Osc_Freq = vco_freq_sig;
  44.    fsig_Osc_Phase = ref_phase_sig;
  45.    fsig_Output = out_sig;
  46.    MAKE_OUTPUT( fsig_Output );
  47.    MAKE_OUTPUT( fsig_Phase_Error );
  48.    MAKE_OUTPUT( fsig_Filtered_Error );
  49.    MAKE_OUTPUT( fsig_Osc_Output );
  50.    MAKE_OUTPUT( fsig_Osc_Freq );
  51.    MAKE_OUTPUT( fsig_Osc_Phase );
  52.    MAKE_INPUT( fsig_Input );
  53.    //----------------------------------
  54.    //  Compute derived parameters
  55.    Omega_Sub_0 = TWO_PI * Center_Freq_Hz;
  56.    //--------------------------------
  57.    // create loop filter object
  58.    char sub_name[50];
  59.    strcpy(sub_name, GetInstanceName());
  60.    strcat(sub_name, ":Filter");
  61.    Filter_Core = new ButterworthFilterByIir<float>( sub_name, this);
  62. }
  63. //======================================
  64. LinearPLL::~LinearPLL( void ){ };
  65. //=======================================
  66. void LinearPLL::Initialize(void)
  67. {
  68.    //------------------
  69.    int block_size = fsig_Output->GetBlockSize();
  70.    Samp_Intvl = fsig_Input->GetSampIntvl();
  71.    Filter_Core->Initialize(block_size, Samp_Intvl);
  72.    Osc_Output_Prev_Val = 0.0;
  73.    OscOutput = 0;
  74.    Phi_Sub_2 = 0;
  75.    Phi_Divided = 0;
  76. }
  77. //=======================================================
  78. int LinearPLL::Execute()
  79. {
  80.    // pointers for signal data
  81.    float *fsOutput_ptr;
  82.    float *fsPhaseError_ptr;
  83.    float *fs_filtered_error_ptr;
  84.    float *fsOscOutput_ptr;
  85.    float *fsOscFreq_ptr;
  86.    float *fsOscPhase_ptr;
  87.    float *fsInput_ptr;
  88.    float input_val;
  89.    float osc_output_val;
  90.    float filt_val;
  91.    float inst_freq;
  92.    double samp_intvl;
  93.    double phase_error;
  94.    double err_sum=0;
  95.    double err_avg;
  96.    int is;
  97.    int block_size;
  98.    //--------------------------------------------------------------
  99.    // set up pointers to data buffers for input and output signals
  100.    fsOutput_ptr = GET_OUTPUT_PTR( fsig_Output );
  101.    fsPhaseError_ptr = GET_OUTPUT_PTR( fsig_Phase_Error );
  102.    fs_filtered_error_ptr = GET_OUTPUT_PTR( fsig_Filtered_Error );
  103.    fsOscOutput_ptr = GET_OUTPUT_PTR( fsig_Osc_Output );
  104.    fsOscFreq_ptr = GET_OUTPUT_PTR( fsig_Osc_Freq );
  105.    fsOscPhase_ptr = GET_OUTPUT_PTR( fsig_Osc_Phase );
  106.    fsInput_ptr = GET_INPUT_PTR( fsig_Input );
  107.    //---------------------------------------------------------------
  108.    samp_intvl = Samp_Intvl;
  109.    osc_output_val = Osc_Output_Prev_Val;
  110.    block_size = fsig_Input->GetValidBlockSize();
  111.    fsig_Output->SetValidBlockSize(block_size);
  112.    fsig_Phase_Error->SetValidBlockSize(block_size);
  113.    fsig_Filtered_Error->SetValidBlockSize(block_size);
  114.    fsig_Osc_Output->SetValidBlockSize(block_size);
  115.    fsig_Osc_Freq->SetValidBlockSize(block_size);
  116.    fsig_Osc_Phase->SetValidBlockSize(block_size);
  117.    //-------------------------------------------------------
  118.    for (is=0; is<block_size; is++)
  119.    {
  120.       *fsOscOutput_ptr++ = osc_output_val;
  121.       input_val = *fsInput_ptr;
  122.       phase_error = K_Sub_D * input_val * OscOutput;
  123.       *fsPhaseError_ptr++ = phase_error;
  124.       //--------------------------------
  125.       //  filter the error signal
  126.       filt_val = Filter_Core->ProcessSample(phase_error);
  127.       *fs_filtered_error_ptr++ = filt_val;
  128.       //---------------------------------------
  129.       //  just for educational purposes, 
  130.       //  compute the average of the filtered error signal
  131.       err_sum += filt_val;
  132.       //----------------------------------------
  133.       // use filtered error signal to drive VCO
  134.       inst_freq = Omega_Sub_0 + K_Sub_0 * filt_val;
  135.       Phi_Sub_2 += inst_freq * samp_intvl;
  136.       if(Phi_Sub_2 > PI) Phi_Sub_2 -= TWO_PI;
  137.       if(m_UsingDco) // make discrete valued output
  138.       {
  139.          if( (Phi_Sub_2 < -PI_OVER_TWO) || (Phi_Sub_2 > PI_OVER_TWO))
  140.          {
  141.             OscOutput = -1;
  142.          }
  143.          else
  144.          {
  145.             OscOutput = 1;
  146.          }
  147.       }
  148.       else // simulate analog VCO output
  149.       {
  150.          OscOutput = cos(Phi_Sub_2);
  151.       }
  152.       *fsOscPhase_ptr++ = Phi_Sub_2;
  153.       *fsOscFreq_ptr++ = inst_freq/TWO_PI;
  154.       Phi_Divided += inst_freq * samp_intvl/Scaler_Divisor;
  155.       if(Phi_Divided > PI) Phi_Divided -= TWO_PI;
  156.       *fsOutput_ptr++ = sin(Phi_Divided);
  157.       fsInput_ptr++;
  158.    }
  159.    err_avg = err_sum / block_size;
  160.    BasicResults << "avg PLL error = " << err_avg << endl;
  161.    Osc_Output_Prev_Val = osc_output_val;
  162.   return(_MES_AOK);
  163. }