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

3G开发

开发平台:

Visual C++

  1. //
  2. //  File = arspec.cpp
  3. //
  4. #include <fstream>
  5. #include "ar_spec.h"
  6. #include "misdefs.h"
  7. using namespace std;
  8. //==============================================
  9. //  constructor
  10. ArSpectrum::ArSpectrum( int ar_order,
  11.                            double* ar_coeff,
  12.                            double samp_intvl,
  13.                            double drv_var,
  14.                            int num_pts,
  15.                            double max_freq)
  16. {
  17.   double denom, two_pi_f, psd_val;
  18.   double a_func_real, a_func_imag;
  19.   int f_idx, cof_idx;
  20.   Samp_Intvl = samp_intvl;
  21.   Num_Pts = num_pts;
  22.   Spec_Buf = new double[Num_Pts];
  23.   Max_Freq = max_freq;
  24.   Freq_Delt = Max_Freq*Samp_Intvl/double(Num_Pts-1);
  25.   double total_pwr = 0.0;
  26.   for(f_idx=0; f_idx<Num_Pts; f_idx++)
  27.     {
  28.     a_func_real = 0.0;
  29.     a_func_imag = 0.0;
  30.     two_pi_f = TWO_PI * f_idx * Freq_Delt;
  31.     for(cof_idx=0; cof_idx<=ar_order; cof_idx++)
  32.       {
  33.       a_func_real += (ar_coeff[cof_idx]*cos(cof_idx*two_pi_f));
  34.       a_func_imag -= (ar_coeff[cof_idx]*sin(cof_idx*two_pi_f));
  35.       }
  36.     denom = a_func_real*a_func_real + a_func_imag*a_func_imag;
  37.     psd_val = samp_intvl*drv_var/denom;
  38.     total_pwr += psd_val;
  39.     Spec_Buf[f_idx] = psd_val;
  40.     }
  41.   return;
  42. }
  43. //------------------------
  44. // destructor
  45. ArSpectrum::~ArSpectrum(void){};
  46. void ArSpectrum::DumpSpectrum( char* out_file_nam,
  47.                                   bool db_plot_enab )
  48. {
  49.   int i;
  50.   double freq, vert_offset;
  51.   ofstream out_file(out_file_nam, ios::out);
  52.   vert_offset = 10.0 * log10(Spec_Buf[0]);
  53.   for(i=0; i<Num_Pts; i++)
  54.     {
  55.     freq = i*Freq_Delt/Samp_Intvl;
  56.     if( db_plot_enab) {
  57.       out_file << freq << ", " 
  58.                << ((10.0 * log10(Spec_Buf[i]))-vert_offset) 
  59.                << endl;
  60.       }
  61.     else {
  62.       out_file << freq << ", " << (Spec_Buf[i])
  63.                << endl;
  64.       }
  65.     }
  66.   out_file.close();
  67. }