RTXI 1.3
plugins/include/DSP/swept.cpp
Go to the documentation of this file.
00001 //
00002 //  File = swept.cpp
00003 //
00004 
00005 #include <fstream>
00006 #include <math.h> 
00007 #include <stdlib.h>
00008 #include "swept.h"
00009 #include "typedefs.h"
00010 #include "misdefs.h"
00011 #ifdef _DEBUG
00012 extern std::ofstream DebugFile;
00013 #endif
00014 
00015 SweptResponse::SweptResponse( FilterImplementation *filter_implem,
00016                               double sampling_interval,
00017                               istream& uin,
00018                               ostream& uout )
00019 {
00020  int resp_indx;
00021  double lambda;
00022  double input_val;
00023  double *output_tone;
00024  int samp_indx;
00025  int num_holdoff_samps;
00026  logical default_file_ok;
00027  Filter_Implem = filter_implem;
00028 
00029  int max_num_samps;
00030  int samps_per_corr;
00031  double cycles_per_corr;
00032  double max_output_mag;
00033  
00034  uout << "number of points in plot of frequency response?" << std::endl;
00035  uin >> Num_Resp_Pts;
00036 
00037  uout << "maximum swept frequency?" << std::endl;
00038  uin >> Max_Sweep_Freq;
00039  if(Max_Sweep_Freq > (0.5/sampling_interval) )
00040    {
00041     uout << "maximum swept frequency will be limited\n"
00042          << "to folding frequency of " 
00043          << (0.5/sampling_interval) << std::endl;
00044     Max_Sweep_Freq = 0.5/sampling_interval;
00045    }
00046  
00047  uout << "scaling?\n"
00048       << "  0 = linear, 1 = dB"  << std::endl;
00049  uin >> Db_Scale_Enabled;
00050 
00051  uout << "number of output cycles to examine?" << std::endl;
00052  uin >> cycles_per_corr;
00053   
00054  if( Db_Scale_Enabled != 0) Db_Scale_Enabled = 1;
00055   
00056  uout << "default name for magnitude response output\n"
00057       << "file is win_resp.txt\n\n"
00058       << "is this okay?"
00059       << "  0 = NO, 1 = YES"
00060       << std::endl;
00061  uin >> default_file_ok;
00062   
00063  if( default_file_ok) 
00064     {
00065      Response_File = new ofstream("win_resp.txt", ios::out);
00066     }
00067   else
00068     {
00069      char *file_name;
00070      file_name = new char[31];
00071      
00072      uout << "enter complete name for output file (30 chars max)"
00073           << std::endl;
00074      uin >> file_name;
00075      Response_File = new ofstream(file_name, ios::out);
00076      delete []file_name;
00077     }
00078  Mag_Resp = new double[Num_Resp_Pts];
00079  max_num_samps = int(2*cycles_per_corr*Num_Resp_Pts/
00080                  (Max_Sweep_Freq * sampling_interval));
00081  output_tone = new double[max_num_samps+2]; 
00082  for( resp_indx=1; resp_indx<Num_Resp_Pts; resp_indx++)
00083    {
00084     lambda = resp_indx * Max_Sweep_Freq * 2.0 * PI * 
00085               sampling_interval / (double) Num_Resp_Pts;
00086     samps_per_corr = int(Num_Resp_Pts*cycles_per_corr/
00087          (resp_indx * Max_Sweep_Freq * sampling_interval));
00088     num_holdoff_samps = samps_per_corr;
00089     //num_holdoff_samps = int(Num_Resp_Pts/
00090     //     (resp_indx * Max_Sweep_Freq * sampling_interval));
00091     for( samp_indx=0; samp_indx<num_holdoff_samps;
00092          samp_indx++)
00093       {
00094        input_val = cos(lambda*samp_indx);
00095        output_tone[samp_indx] = 
00096                   filter_implem->ProcessSample(input_val);
00097       }
00098     max_output_mag = 0.0;
00099     for( samp_indx=num_holdoff_samps; 
00100          samp_indx<(samps_per_corr+num_holdoff_samps);
00101          samp_indx++)
00102       {
00103        input_val = cos(lambda*samp_indx);
00104        output_tone[samp_indx] = 
00105                   filter_implem->ProcessSample(input_val);
00106        if(fabs(output_tone[samp_indx]) > max_output_mag)
00107          {
00108          max_output_mag = fabs(output_tone[samp_indx]);
00109          }
00110       } 
00111     if(Db_Scale_Enabled)
00112       {
00113       Mag_Resp[resp_indx] = 
00114           20.0 * log10(max_output_mag);
00115       }
00116     else
00117       {Mag_Resp[resp_indx] = max_output_mag;}
00118     }  // end of loop over resp_indx
00119 
00120  if(Normalize_Enabled) NormalizeResponse();
00121  return;
00122 }
00123 //=======================================================
00124 // destructor
00125 //-------------------------------------------------------
00126 
00127 SweptResponse::~SweptResponse()
00128 {
00129  delete []Mag_Resp;
00130  delete Response_File;
00131 }
00132 //=======================================================
00133 //  method to normalize magnitude response
00134 //-------------------------------------------------------
00135 
00136 void SweptResponse::NormalizeResponse( void )
00137 {
00138  int n;
00139  double biggest;
00140  
00141  if(Db_Scale_Enabled)
00142    {
00143     biggest = -100.0; 
00144     
00145     for( n=1; n < Num_Resp_Pts; n++)
00146       {if(Mag_Resp[n]>biggest) biggest = Mag_Resp[n];}
00147     #ifdef _DEBUG
00148     DebugFile << "before normaliz, biggest Mag_Resp was "
00149               << biggest << std::endl;
00150     #endif
00151     for( n=1; n < Num_Resp_Pts; n++)
00152       {Mag_Resp[n] = Mag_Resp[n] - biggest;}
00153    }
00154  else
00155    {
00156     biggest = 0.0;
00157     
00158     for( n=1; n < Num_Resp_Pts; n++)
00159       {if(Mag_Resp[n]>biggest) biggest = Mag_Resp[n];}
00160     for( n=1; n < Num_Resp_Pts; n++)
00161       {Mag_Resp[n] = Mag_Resp[n] / biggest;}
00162    }
00163  return;
00164 }
00165 //===========================================================
00166 //  method to dump magnitude response to the stream
00167 //  designated by Response_File
00168 //-----------------------------------------------------------
00169 
00170 void SweptResponse::DumpMagResp( void )
00171 {
00172  double freq;
00173  
00174  //Response_File->setf(ios::fixed, ios::doublefield);
00175  for(int n=1; n<Num_Resp_Pts; n++)
00176    {
00177     freq = n * Max_Sweep_Freq / (double) Num_Resp_Pts;
00178     (*Response_File) << freq << ", " 
00179                      << Mag_Resp[n] << std::endl;
00180    }
00181  //Response_File->setf(0, ios::doublefield);
00182  return;
00183 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines