RTXI 1.3
plugins/include/DSP/arspec.cpp
Go to the documentation of this file.
00001 //
00002 //  File = arspec.cpp
00003 //
00004 
00005 #include <stdlib.h>
00006 #include <fstream>
00007 #include "arspec.h"
00008 #include "misdefs.h"
00009 
00010 //==============================================
00011 //  constructor
00012 
00013 template < class T >
00014 ArSpectrum<T>::ArSpectrum( int ar_order,
00015                            T* ar_coeff,
00016                            double samp_intvl,
00017                            double drv_var)
00018 {
00019   double denom, two_pi_f, psd_val;
00020   double a_func_real, a_func_imag;
00021   int f_idx, cof_idx;
00022 
00023   Samp_Intvl = samp_intvl;
00024   std::cout << "drv_var = " << drv_var << std::endl;
00025   std::cout << "How many points in spectrum plot?" << std::endl;
00026   std::cin >> Num_Pts;
00027   Spec_Buf = new double[Num_Pts];
00028   std::cout << "Maximum frequency?" << std::endl;
00029   std::cin >> Max_Freq;
00030   Freq_Delt = Max_Freq/(Num_Pts-1);
00031   double total_pwr = 0.0;
00032   for(f_idx=0; f_idx<Num_Pts; f_idx++)
00033     {
00034     a_func_real = 0.0;
00035     a_func_imag = 0.0;
00036     two_pi_f = TWO_PI * f_idx * Freq_Delt;
00037     for(cof_idx=0; cof_idx<=ar_order; cof_idx++)
00038       {
00039       a_func_real += (ar_coeff[cof_idx]*cos(cof_idx*two_pi_f));
00040       a_func_imag -= (ar_coeff[cof_idx]*sin(cof_idx*two_pi_f));
00041       }
00042     denom = a_func_real*a_func_real + a_func_imag*a_func_imag;
00043     psd_val = samp_intvl*drv_var/denom;
00044     total_pwr += psd_val;
00045     Spec_Buf[f_idx] = psd_val;
00046     }
00047   return;
00048 }
00049 //------------------------
00050 // destructor
00051 
00052 template < class T >
00053 ArSpectrum<T>::~ArSpectrum(void){};
00054 
00055 template < class T >
00056 void ArSpectrum<T>::DumpSpectrum( char* out_file_nam,
00057                                   TheoreticalSpectrum *ref_spectrum,
00058                                   logical db_plot_enab )
00059 {
00060   int i;
00061   double freq, ref_value, ref_offset, vert_offset;
00062   ofstream out_file(out_file_nam, ios::out);
00063 
00064   vert_offset = 10.0 * log10(Spec_Buf[0]);
00065   if( db_plot_enab )
00066     ref_value = 1.0;
00067   else
00068     ref_value = 0.0;
00069   if(ref_spectrum != NULL) ref_offset = 
00070                   10.0 * log10(ref_spectrum->GetPsdValue( 0.0 ));
00071   for(i=0; i<Num_Pts; i++)
00072     {
00073     freq = i*Freq_Delt;
00074     if(ref_spectrum != NULL) 
00075          ref_value = ref_spectrum->GetPsdValue( freq/Samp_Intvl);
00076     if( db_plot_enab) {
00077       out_file << freq << ", " 
00078                << ((10.0 * log10(Spec_Buf[i]))-vert_offset) 
00079                << ", "
00080                << ((10.0* log10(ref_value))-ref_offset) << std::endl;
00081       }
00082     else {
00083       out_file << freq << ", " << (Spec_Buf[i])
00084                << ", " << ref_value << std::endl;
00085       }
00086     }
00087   out_file.close();
00088 }
00089 template < class T >
00090 void ArSpectrum<T>::DumpSpectrum( char* out_file_nam,
00091                                   logical db_plot_enab )
00092 {
00093   int i;
00094   double freq, vert_offset;
00095   ofstream out_file(out_file_nam, ios::out);
00096 
00097   vert_offset = 10.0 * log10(Spec_Buf[0]);
00098   for(i=0; i<Num_Pts; i++)
00099     {
00100     freq = i*Freq_Delt;
00101     if( db_plot_enab) {
00102       out_file << freq << ", " 
00103                << ((10.0 * log10(Spec_Buf[i]))-vert_offset) 
00104                << std::endl;
00105       }
00106     else {
00107       out_file << freq << ", " << (Spec_Buf[i])
00108                << std::endl;
00109       }
00110     }
00111   out_file.close();
00112 }
00113 
00114 //template ArSpectrum<complex>;
00115 template ArSpectrum<double>;
00116 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines