RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
swept.cpp
Go to the documentation of this file.
1 //
2 // File = swept.cpp
3 //
4 
5 #include <fstream>
6 #include <math.h>
7 #include <stdlib.h>
8 #include <iostream>
9 
10 #include "swept.h"
11 #include "misdefs.h"
12 #include "typedefs.h"
13 
14 using namespace std;
15 
16 #ifdef _DEBUG
17 extern std::ofstream DebugFile;
18 #endif
19 
21  double sampling_interval, istream& uin,
22  ostream& uout)
23 {
24  int resp_indx;
25  double lambda;
26  double input_val;
27  double* output_tone;
28  int samp_indx;
29  int num_holdoff_samps;
30  logical default_file_ok;
31  Filter_Implem = filter_implem;
32 
33  int max_num_samps;
34  int samps_per_corr;
35  double cycles_per_corr;
36  double max_output_mag;
37 
38  uout << "number of points in plot of frequency response?" << std::endl;
39  uin >> Num_Resp_Pts;
40 
41  uout << "maximum swept frequency?" << std::endl;
42  uin >> Max_Sweep_Freq;
43  if (Max_Sweep_Freq > (0.5 / sampling_interval)) {
44  uout << "maximum swept frequency will be limited\n"
45  << "to folding frequency of " << (0.5 / sampling_interval)
46  << std::endl;
47  Max_Sweep_Freq = 0.5 / sampling_interval;
48  }
49 
50  uout << "scaling?\n"
51  << " 0 = linear, 1 = dB" << std::endl;
52  uin >> Db_Scale_Enabled;
53 
54  uout << "number of output cycles to examine?" << std::endl;
55  uin >> cycles_per_corr;
56 
57  if (Db_Scale_Enabled != 0)
58  Db_Scale_Enabled = 1;
59 
60  uout << "default name for magnitude response output\n"
61  << "file is win_resp.txt\n\n"
62  << "is this okay?"
63  << " 0 = NO, 1 = YES" << std::endl;
64  uin >> default_file_ok;
65 
66  if (default_file_ok) {
67  Response_File = new ofstream("win_resp.txt", ios::out);
68  } else {
69  char* file_name;
70  file_name = new char[31];
71 
72  uout << "enter complete name for output file (30 chars max)" << std::endl;
73  uin >> file_name;
74  Response_File = new ofstream(file_name, ios::out);
75  delete[] file_name;
76  }
77  Mag_Resp = new double[Num_Resp_Pts];
78  max_num_samps = int(2 * cycles_per_corr * Num_Resp_Pts /
79  (Max_Sweep_Freq * sampling_interval));
80  output_tone = new double[max_num_samps + 2];
81  for (resp_indx = 1; resp_indx < Num_Resp_Pts; resp_indx++) {
82  lambda = resp_indx * Max_Sweep_Freq * 2.0 * PI * sampling_interval /
83  (double)Num_Resp_Pts;
84  samps_per_corr = int(Num_Resp_Pts * cycles_per_corr /
85  (resp_indx * Max_Sweep_Freq * sampling_interval));
86  num_holdoff_samps = samps_per_corr;
87  // num_holdoff_samps = int(Num_Resp_Pts/
88  // (resp_indx * Max_Sweep_Freq * sampling_interval));
89  for (samp_indx = 0; samp_indx < num_holdoff_samps; samp_indx++) {
90  input_val = cos(lambda * samp_indx);
91  output_tone[samp_indx] = filter_implem->ProcessSample(input_val);
92  }
93  max_output_mag = 0.0;
94  for (samp_indx = num_holdoff_samps;
95  samp_indx < (samps_per_corr + num_holdoff_samps); samp_indx++) {
96  input_val = cos(lambda * samp_indx);
97  output_tone[samp_indx] = filter_implem->ProcessSample(input_val);
98  if (fabs(output_tone[samp_indx]) > max_output_mag) {
99  max_output_mag = fabs(output_tone[samp_indx]);
100  }
101  }
102  if (Db_Scale_Enabled) {
103  Mag_Resp[resp_indx] = 20.0 * log10(max_output_mag);
104  } else {
105  Mag_Resp[resp_indx] = max_output_mag;
106  }
107  } // end of loop over resp_indx
108 
109  if (Normalize_Enabled)
110  NormalizeResponse();
111 
112  delete[] output_tone;
113 
114  return;
115 }
116 //=======================================================
117 // destructor
118 //-------------------------------------------------------
119 
121 {
122  delete[] Mag_Resp;
123  delete Response_File;
124 }
125 //=======================================================
126 // method to normalize magnitude response
127 //-------------------------------------------------------
128 
129 void
131 {
132  int n;
133  double biggest;
134 
135  if (Db_Scale_Enabled) {
136  biggest = -100.0;
137 
138  for (n = 1; n < Num_Resp_Pts; n++) {
139  if (Mag_Resp[n] > biggest)
140  biggest = Mag_Resp[n];
141  }
142 #ifdef _DEBUG
143  DebugFile << "before normaliz, biggest Mag_Resp was " << biggest
144  << std::endl;
145 #endif
146  for (n = 1; n < Num_Resp_Pts; n++) {
147  Mag_Resp[n] = Mag_Resp[n] - biggest;
148  }
149  } else {
150  biggest = 0.0;
151 
152  for (n = 1; n < Num_Resp_Pts; n++) {
153  if (Mag_Resp[n] > biggest)
154  biggest = Mag_Resp[n];
155  }
156  for (n = 1; n < Num_Resp_Pts; n++) {
157  Mag_Resp[n] = Mag_Resp[n] / biggest;
158  }
159  }
160  return;
161 }
162 //===========================================================
163 // method to dump magnitude response to the stream
164 // designated by Response_File
165 //-----------------------------------------------------------
166 
167 void
169 {
170  double freq;
171 
172  // Response_File->setf(ios::fixed, ios::doublefield);
173  for (int n = 1; n < Num_Resp_Pts; n++) {
174  freq = n * Max_Sweep_Freq / (double)Num_Resp_Pts;
175  (*Response_File) << freq << ", " << Mag_Resp[n] << std::endl;
176  }
177  // Response_File->setf(0, ios::doublefield);
178  return;
179 }
int logical
Definition: typedefs.h:4
void NormalizeResponse(void)
Definition: swept.cpp:130
#define PI
Definition: misdefs.h:9
std::ofstream DebugFile
virtual double ProcessSample(double input_val)=0
void DumpMagResp(void)
Definition: swept.cpp:168
~SweptResponse()
Definition: swept.cpp:120
SweptResponse(FilterImplementation *filter_implem, double sampling_interval, std::istream &uin, std::ostream &uout)
Definition: swept.cpp:20