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