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

3G开发

开发平台:

Visual C++

  1. //
  2. //  File =histogram.cpp
  3. //
  4. #include <stdlib.h>
  5. #include <fstream>
  6. #include "parmfile.h"
  7. #include "model_graph.h"
  8. #include "histogram.h"
  9. #ifdef _DEBUG
  10.   extern ofstream *DebugFile;
  11. #endif
  12. extern ParmFile *ParmInput;
  13. extern int PassNumber;
  14. //======================================================
  15. template <class T>
  16. HistogramBuilder<T>
  17.          ::HistogramBuilder( char* instance_name,
  18.                              PracSimModel* outer_model,
  19.                              Signal<T>* in_sig )
  20.                 :PracSimModel( instance_name,
  21.                                 outer_model )
  22. {
  23.    MODEL_NAME(SpectrumAnalyzer_T);
  24.    OPEN_PARM_BLOCK;
  25.    GET_INT_PARM(Num_Segs_To_Tally);
  26.    GET_INT_PARM(Hold_Off);
  27.    Hist_File_Name = new char[64];
  28.    strcpy(Hist_File_Name, "");
  29.    GET_STRING_PARM(Hist_File_Name);
  30.    GET_INT_PARM(Num_Bins);
  31.    GET_DOUBLE_PARM(Bin_Width);
  32.    GET_BOOL_PARM(Positive_Only);
  33.    GET_BOOL_PARM(Halt_When_Completed);
  34.    if(!Positive_Only){
  35.       if(Num_Bins%2 == 0) Num_Bins++;
  36.       Ctr_Bin = (Num_Bins-1)/2;
  37.    }
  38.    else{
  39.       Ctr_Bin = 0;
  40.    }
  41.    In_Sig = in_sig;
  42.    MAKE_INPUT(In_Sig);
  43.    Hist_Bins = new double[Num_Bins];
  44.    for(int is=0; is<Num_Bins; is++){
  45.       Hist_Bins[is] = 0.0;
  46.    }
  47.    Hist_File = new ofstream(Hist_File_Name, ios::out);
  48.    Processing_Completed = false;
  49. }
  50. template <class T>
  51. HistogramBuilder<T>::~HistogramBuilder( void ){ };
  52. template <class T>
  53. void HistogramBuilder<T>::Initialize(void)
  54. {
  55.    Segs_In_Tally = 0;
  56.    Pts_In_Tally = 0;
  57.    Out_Of_Range_Count = 0;
  58.    Block_Size = In_Sig->GetBlockSize();
  59.    Sum = 0.0;
  60.    Sum_Sqrs = 0.0;
  61. };
  62. template <class T>
  63. //======================================================
  64. int HistogramBuilder<T>::Execute()
  65. {
  66.    int is;
  67.    float left_edge, right_edge, gap_on_left;
  68. #ifdef _DEBUG
  69.    *DebugFile << "In HistogramBuilder::Execute" 
  70.               << endl;
  71. #endif
  72.    if(Processing_Completed) return(_MES_AOK);
  73.    if(PassNumber < Hold_Off) return (_MES_AOK);
  74.    //--------------------------------
  75.    //  Get pointers for buffers
  76.    T *in_sig_ptr = GET_INPUT_PTR(In_Sig);
  77.    T in_sig_val;
  78.    int bin_idx;
  79.    double val;
  80.    double edge_offset;
  81.    double mean, variance;
  82.    for(is=0; is<Block_Size; is++){
  83.       in_sig_val = *in_sig_ptr++;
  84.       Sum += in_sig_val;
  85.       Sum_Sqrs += in_sig_val * in_sig_val;
  86.       bin_idx = int(Ctr_Bin + in_sig_val/Bin_Width);
  87.       if(bin_idx>=0 && bin_idx<Num_Bins){
  88.          Hist_Bins[bin_idx]++;
  89.          Pts_In_Tally++;
  90.       }
  91.       else{
  92.          Out_Of_Range_Count++;
  93.       }
  94.    }
  95.    Segs_In_Tally++;
  96.    // is it time to dump the results?
  97.    if(Segs_In_Tally == Num_Segs_To_Tally){
  98.       mean = Sum/Pts_In_Tally;
  99.       variance = (Sum_Sqrs/Pts_In_Tally) - (mean*mean);
  100.       *DebugFile << "mean = " << mean << endl;
  101.       *DebugFile << "variance = " << variance << endl;
  102.       if(Positive_Only)
  103.          edge_offset = 0.0;
  104.       else
  105.          edge_offset = 0.5;
  106.       for(is=0; is<Num_Bins; is++){
  107.          gap_on_left = 
  108.             float(((is-Ctr_Bin)-edge_offset)*Bin_Width);
  109.          left_edge = 
  110.             float(((is-Ctr_Bin)-edge_offset)*Bin_Width);
  111.          right_edge = float(left_edge + Bin_Width);
  112.          val = 
  113.             double(Hist_Bins[is])/double(Pts_In_Tally);
  114.          (*Hist_File) << gap_on_left << ", " << 0 
  115.                       << endl;
  116.          (*Hist_File) << left_edge << ", " << val 
  117.                       << endl;
  118.          (*Hist_File) << right_edge << ", " << val 
  119.                       << endl;
  120.       }
  121.       gap_on_left = 
  122.          float(((Num_Bins-Ctr_Bin)-0.5)*Bin_Width);
  123.       (*Hist_File) << gap_on_left << ", " << 0 << endl;
  124.       Processing_Completed = true;
  125.       Hist_File->close();
  126.       if(Halt_When_Completed){
  127. #ifdef _DEBUG
  128.          *DebugFile << "Execution halted by " 
  129.                     << GetModelName() << endl;
  130. #endif
  131.          exit(0);
  132.       }
  133.    }
  134.    return(_MES_AOK);
  135. }
  136. //======================================================
  137. //template HistogramBuilder<std::complex<float> >;
  138. template HistogramBuilder<float>;