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

3G开发

开发平台:

Visual C++

  1. //
  2. //  File = samp_spect.cpp
  3. //
  4. #include <stdlib.h>
  5. #include <fstream>
  6. #include "parmfile.h"
  7. #include "model_graph.h"
  8. #include "samp_spect.h"
  9. #include "fft_T.h"
  10. #include "dump_spect.h"
  11. #ifdef _DEBUG
  12.   extern ofstream *DebugFile;
  13. #endif
  14. extern ParmFile *ParmInput;
  15. extern int PassNumber;
  16. //======================================================
  17. template <class T>
  18. SampleSpectrum<T>::SampleSpectrum( char* instance_name,
  19.                                   PracSimModel* outer_model,
  20.                                   Signal<T>* in_sig )
  21.                 :PracSimModel( instance_name,
  22.                                 outer_model )
  23. {
  24.   MODEL_NAME(SampleSpectrum);
  25.   OPEN_PARM_BLOCK;
  26.   GET_INT_PARM(Seg_Len);
  27.   GET_INT_PARM(Fft_Len);
  28.   GET_INT_PARM(Hold_Off);
  29.   Psd_File_Name = new char[64];
  30.   strcpy(Psd_File_Name, "");
  31.   GET_STRING_PARM(Psd_File_Name);
  32.   GET_DOUBLE_PARM(Freq_Norm_Factor);
  33.   GET_BOOL_PARM(Output_In_Decibels);
  34.   GET_BOOL_PARM(Plot_Two_Sided);
  35.   GET_BOOL_PARM(Halt_When_Completed);
  36.   In_Sig = in_sig;
  37.   MAKE_INPUT(In_Sig);
  38.   Time_Seg = new T[Seg_Len];
  39.   Sample_Spectrum = new double[Fft_Len];
  40.   Freq_Seg = new std::complex<double>[Fft_Len];
  41.   Psd_File = new ofstream(Psd_File_Name, ios::out);
  42.   Processing_Completed = false;
  43. }
  44. template <class T>
  45. SampleSpectrum<T>::~SampleSpectrum( void ){ };
  46. template <class T>
  47. void SampleSpectrum<T>::Initialize(void)
  48. {
  49.   Segs_In_Est = 0;
  50.   Samps_Needed = Seg_Len;
  51.   Block_Size = In_Sig->GetBlockSize();
  52.   Samp_Intvl = In_Sig->GetSampIntvl();
  53.   Delta_F = 1.0/(Samp_Intvl*Fft_Len);
  54. };
  55. template <class T>
  56. int SampleSpectrum<T>::Execute()
  57. {
  58.    int i,is;
  59.    #ifdef _DEBUG
  60.       *DebugFile << "In SampleSpectrum::Execute" << endl;
  61.    #endif
  62.    if(Processing_Completed) return(_MES_AOK);
  63.    if(PassNumber < Hold_Off+1) return (_MES_AOK);
  64.    //--------------------------------
  65.    //  Get pointers for buffers
  66.    T *in_sig_ptr = GET_INPUT_PTR(In_Sig);
  67.    int samps_avail = Block_Size;
  68.    while(Samps_Needed <= samps_avail)
  69.    {
  70.       //  The new input block has enough samples to finish a segment.
  71.       //  Fill up FFT buffer by getting Samps_Needed input samples.
  72.       for(is=Samps_Needed; is>0; is--)
  73.       {
  74.          Time_Seg[Seg_Len - is] = *in_sig_ptr++;
  75.       }
  76.       samps_avail -= Samps_Needed;
  77.       //  Perform FFT
  78.       FFT<double>(   Time_Seg,
  79.                      Freq_Seg,
  80.                      Seg_Len,
  81.                      Fft_Len);
  82.       for(i=0; i<Seg_Len; i++)
  83.       {
  84.          Sample_Spectrum[i] = Samp_Intvl * std::norm(Freq_Seg[i])/Seg_Len;
  85.       }
  86.       for(is=0; is<Seg_Len; is++)
  87.       {
  88.          Time_Seg[is] = 0.0;
  89.       }
  90.       Samps_Needed = Seg_Len;
  91. //      Segs_In_Est++;
  92.       // is it time to dump the results?
  93. //      if(Segs_In_Est == Num_Segs_To_Avg)
  94. //      {
  95.          DumpSpectrum(  Sample_Spectrum,
  96.                         Fft_Len,
  97.                         Delta_F,
  98.                         Freq_Norm_Factor,
  99.                         Output_In_Decibels,
  100.                         Plot_Two_Sided,
  101.                         Psd_File);
  102.          Processing_Completed = true;
  103.          Psd_File->close();
  104.          if(Halt_When_Completed)
  105.          {
  106.             #ifdef _DEBUG
  107.                *DebugFile << "Execution halted by " << GetModelName() << endl;
  108.             #endif
  109.             exit(0);
  110.          }
  111. //      }
  112.    }// end of while
  113.   //  The number of avail new samples is not sufficient to finish a segment.
  114.   //  Copy the avaialble samples and then wait for the next pass
  115.   //  to get some more.
  116.    for(is=0; is<samps_avail; is++)
  117.    {
  118.       Time_Seg[Seg_Len - Samps_Needed + is] = *in_sig_ptr++;
  119.    }
  120.    Samps_Needed -= samps_avail;
  121.    return(_MES_AOK);
  122. }
  123. template SampleSpectrum<float>;