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

3G开发

开发平台:

Visual C++

  1. //
  2. //  File = basewav.cpp
  3. //
  4. #include <stdlib.h>
  5. #include <fstream>
  6. #include "parmfile.h"
  7. #include "basewav.h"
  8. #include "model_graph.h"
  9. extern int PassNumber;
  10. extern ParmFile *ParmInput;
  11. //======================================================
  12. BasebandWaveform::BasebandWaveform( char* instance_name,
  13.                         PracSimModel* outer_model,
  14.                         Signal<bit_t>* in_sig,
  15.                         Signal<float>* out_sig,
  16.                         Signal<bit_t>* bit_clock_out )
  17.                 :PracSimModel(instance_name,
  18.                               outer_model)
  19. {
  20.    MODEL_NAME(BasebandWaveform);
  21.    ENABLE_MULTIRATE;
  22.    //---------------------------------------
  23.    //  Read model config parms
  24.    OPEN_PARM_BLOCK;
  25.    Pcm_Wave_Kind = GetWaveKindParm("Pcm_Wave_Kind");
  26.    BasicResults << "   " << "Pcm_Wave_Kind = " 
  27.     << (int)Pcm_Wave_Kind << endl;
  28.    GET_DOUBLE_PARM( Bit_Duration );
  29.    GET_DOUBLE_PARM( Delay_To_First_Edge );
  30.    GET_FLOAT_PARM( Lo_Lev );
  31.    GET_FLOAT_PARM( Hi_Lev );
  32.    GET_FLOAT_PARM( Samps_Per_Bit );
  33.    Bit_Clock_Enab = true;
  34.    if(Pcm_Wave_Kind == PCM_BIPHASE){
  35.       Samps_Per_Half_Bit = Samps_Per_Bit/2;
  36.    }
  37.    if(Pcm_Wave_Kind == PCM_DELAY_MOD){
  38.       GET_BOOL_PARM(Init_Lev_Is_Lo);
  39.       if(Init_Lev_Is_Lo)
  40.          Init_Lev = Lo_Lev;
  41.       else
  42.          Init_Lev = Hi_Lev;
  43.       Samps_Per_Half_Bit = Samps_Per_Bit/2;
  44.    }
  45.   //--------------------------------------
  46.   //  Connect input and output signals
  47.    In_Sig = in_sig;
  48.    Out_Sig = out_sig;
  49.    Bit_Clock_Out = bit_clock_out;
  50.    MAKE_OUTPUT( Out_Sig );
  51.    MAKE_OUTPUT( Bit_Clock_Out );
  52.    MAKE_INPUT( In_Sig );
  53.    double resamp_rate = double(Samps_Per_Bit);
  54.    // one sample per bit at input
  55.    SET_SAMP_INTVL( In_Sig, Bit_Duration)
  56.    CHANGE_RATE( In_Sig, Out_Sig, resamp_rate );
  57.    CHANGE_RATE( In_Sig, Bit_Clock_Out, resamp_rate );
  58. }
  59. //===================================================
  60. BasebandWaveform::~BasebandWaveform( void ){ };
  61. //===================================================
  62. void BasebandWaveform::Initialize(void)
  63. {
  64.    Last_Bit_Val = 0.0;
  65.    double samp_intvl = Out_Sig->GetSampIntvl();
  66.    if(Pcm_Wave_Kind == PCM_DELAY_MOD){
  67.       Samps_In_Curr_Bit = -1.0;
  68.    }
  69.    else {
  70.       Samps_In_Curr_Bit = (Bit_Duration - 
  71.   Delay_To_First_Edge)/samp_intvl - 1.0;
  72.    }
  73.    In_First_Half = true;
  74.    Out_Block_Size = Out_Sig->GetBlockSize();
  75.    In_Startup_Delay = true;
  76.    Curr_Lev = Init_Lev;
  77. }
  78. //====================================================
  79. int BasebandWaveform::Execute()
  80. {
  81.    float last_bit_val;
  82.    double samps_in_curr_bit, samps_per_bit;
  83.    double samps_per_half_bit;
  84.    float lo_lev, hi_lev, curr_lev;
  85.    bit_t curr_bit, next_bit;
  86.    float *waveform_out;
  87.    bit_t *bits_in;
  88.    bit_t *bit_clock_out_ptr;
  89.    int is;
  90.    int out_samp_cnt;
  91.    int block_size;
  92.    int out_block_size;
  93.    bool in_first_half;
  94.    bool transit_at_end;
  95.    bool transit_in_center;
  96.    last_bit_val = Last_Bit_Val;
  97.    samps_in_curr_bit = Samps_In_Curr_Bit;
  98.    samps_per_bit = Samps_Per_Bit;
  99.    lo_lev = Lo_Lev;
  100.    hi_lev = Hi_Lev;
  101.    curr_lev = Curr_Lev;
  102.    waveform_out = GET_OUTPUT_PTR( Out_Sig );
  103.    bits_in = GET_INPUT_PTR( In_Sig );
  104.    block_size = In_Sig->GetValidBlockSize();
  105.    out_block_size = int(block_size * Samps_Per_Bit);
  106.    Out_Sig->SetValidBlockSize(out_block_size);
  107.    switch (Pcm_Wave_Kind) {
  108.    case PCM_NRZ:
  109.       for (is=0; is<out_block_size; is++){
  110.          samps_in_curr_bit++;
  111.          if(samps_in_curr_bit >= samps_per_bit){
  112.             next_bit = *bits_in++;
  113.             if(next_bit == 0)
  114.             last_bit_val = lo_lev;
  115.             else
  116.             last_bit_val = hi_lev;
  117.             samps_in_curr_bit -= samps_per_bit;
  118. }
  119.         *waveform_out++ = last_bit_val;
  120. }
  121.       break;
  122.    case PCM_BIPHASE:
  123.       samps_per_half_bit = Samps_Per_Half_Bit;
  124.       in_first_half = In_First_Half;
  125.       for (is=0; is<Out_Block_Size; is++){
  126.          samps_in_curr_bit++;
  127.          if(samps_in_curr_bit >= samps_per_bit){
  128.             next_bit = *bits_in++;
  129.             if(next_bit == 0)
  130.                last_bit_val = lo_lev;
  131.             else
  132.                last_bit_val = hi_lev;
  133.             samps_in_curr_bit -= samps_per_bit;
  134.             in_first_half = true;
  135. }
  136.          if( (samps_in_curr_bit >= samps_per_half_bit)
  137.                && in_first_half ){
  138.             last_bit_val = -last_bit_val;
  139.             in_first_half = false;
  140. }
  141.          *waveform_out++ = last_bit_val;
  142.       }
  143.       In_First_Half = in_first_half;
  144.       break;
  145.    case PCM_DELAY_MOD:
  146.       samps_per_half_bit = Samps_Per_Half_Bit;
  147.       transit_at_end = Transit_At_End;
  148.       transit_in_center = Transit_In_Center;
  149.       out_samp_cnt = 0;
  150.       //
  151.       // delay start of output by one bit interval, so
  152.       // fill first bit of output with initial level
  153.       while (In_Startup_Delay){
  154.         samps_in_curr_bit++;
  155.         if(samps_in_curr_bit >= samps_per_bit){
  156. In_Startup_Delay = false;
  157. next_bit = *bits_in++;
  158. transit_at_end = false;
  159. transit_in_center = false;
  160. }
  161.          *waveform_out++ = curr_lev;
  162.          out_samp_cnt++;
  163. }
  164.       for (is=out_samp_cnt; is<Out_Block_Size; is++){
  165.          samps_in_curr_bit++;
  166.          if(samps_in_curr_bit >= samps_per_bit){
  167.             if(transit_at_end){
  168.                if(curr_lev == lo_lev){
  169.                   curr_lev = hi_lev;
  170.    }
  171.                else {
  172.                   curr_lev = lo_lev;
  173.                }
  174.             }
  175.             
  176.             curr_bit = next_bit;
  177.             next_bit = *bits_in++;
  178.             if(curr_bit == 1){
  179.                transit_in_center = true;
  180.                transit_at_end = false;
  181.             }
  182.             else if(next_bit == 1){
  183.                transit_in_center = false;
  184.                transit_at_end = false;
  185.             }
  186.             else{
  187.                transit_in_center = false;
  188.                transit_at_end = true;
  189.             }
  190.             samps_in_curr_bit -= samps_per_bit;
  191.          }
  192.          if( (samps_in_curr_bit >= samps_per_half_bit)
  193.                && transit_in_center ){
  194.             if(curr_lev == lo_lev){
  195.                curr_lev = hi_lev;
  196.             }
  197.             else{
  198.                curr_lev = lo_lev;
  199.             }
  200.             transit_in_center = false;
  201.          }
  202.          *waveform_out++ = curr_lev;
  203.       }
  204.       Transit_At_End = transit_at_end;
  205.       Transit_In_Center = transit_in_center;
  206.       break;
  207.    default:
  208.       break;
  209.    } // end of switch (Pcm_Wave_Kind)
  210.    //------------------------------------------
  211.    // Generate bit clock if needed
  212.    if(Bit_Clock_Enab){
  213.       samps_in_curr_bit = Samps_In_Curr_Bit;
  214.       bit_clock_out_ptr = 
  215.            GET_OUTPUT_PTR( Bit_Clock_Out );
  216.       for (is=0; is<Out_Block_Size; is++){
  217.          samps_in_curr_bit++;
  218.          if(samps_in_curr_bit >= samps_per_bit){
  219.             samps_in_curr_bit -= samps_per_bit;
  220.          }
  221.          if(samps_in_curr_bit+1 >= samps_per_bit){
  222.             *bit_clock_out_ptr++ = 1;
  223.          }
  224.          else {
  225.             *bit_clock_out_ptr++ = 0;
  226.          }
  227.       }
  228.    }
  229.    Last_Bit_Val = last_bit_val;
  230.    Samps_In_Curr_Bit = samps_in_curr_bit;
  231.    return(_MES_AOK);
  232. }