-
Notifications
You must be signed in to change notification settings - Fork 2
/
Perform.cpp
387 lines (360 loc) · 10.2 KB
/
Perform.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#ifdef PATHSIM_TESTMODE
#include "Perform.h"
namespace PathSim {
#define K_2PI ( 8.0 * atan(1.0) ) // 2 Pi
#define K_HIST_RES 500
static double sum = 0.0;
static double rms = 0.0;
static int cnt = 0;
static bool delay = false;
static FILE *stream = NULL;
static double timeinc = 0.0;
static int HistArray[K_HIST_RES];
static double endfreq;
static double freqinc;
static double testfreq = -1.0;
double gDebug1 = 0.0;
double gDebug2 = 0.0;
int iDebug3 = 0;
static LONGLONG StartTime;
static LONGLONG StopTime;
static LONGLONG DeltaTime;
static LONGLONG CountFreq;
static LONGLONG DeltaTimeMax;
static LONGLONG DeltaTimeMin;
static LONGLONG DeltaTimeAve;
static LONGLONG DeltaSamples;
// call to initialize the prformance timer
void InitPerformance()
{
QueryPerformanceFrequency( (LARGE_INTEGER*)&CountFreq ); //get clock freq
DeltaTimeMax = 0;
DeltaTimeAve = 0;
DeltaSamples = 0;
DeltaTimeMin = 0x7FFFFFFFFFFFFFFF;
}
// Starts the performance timer
void StartPerformance()
{
QueryPerformanceCounter( (LARGE_INTEGER*)&StartTime );
}
// Stop performance timer and calculate timing values
void StopPerformance()
{
QueryPerformanceCounter( (LARGE_INTEGER*)&StopTime );
DeltaTime = StopTime-StartTime;
DeltaTimeAve += DeltaTime;
DeltaSamples++;
if( DeltaTime>DeltaTimeMax )
DeltaTimeMax = DeltaTime;
if( DeltaTime<DeltaTimeMin )
DeltaTimeMin = DeltaTime;
}
// Call this to measure time between succesive calls to SamplePerformance()
void SamplePerformance()
{
if( DeltaSamples == 0 )
{
QueryPerformanceCounter( (LARGE_INTEGER*)&StartTime );
}
else
{
QueryPerformanceCounter( (LARGE_INTEGER*)&StopTime );
DeltaTime = StopTime-StartTime;
DeltaTimeAve += DeltaTime;
if( DeltaTime>DeltaTimeMax )
DeltaTimeMax = DeltaTime;
if( DeltaTime<DeltaTimeMin )
DeltaTimeMin = DeltaTime;
QueryPerformanceCounter( (LARGE_INTEGER*)&StartTime );
}
DeltaSamples++;
}
// output various timing statistics to Message Box
// DeltaTimeMax == maximum time between start()-stop() or sample()-Sample()
// DeltaTimeMin == minimum time between start()-stop() or sample()-Sample()
// DeltaTimeAve == average time between start()-stop() or sample()-Sample()
// DeltaSamples == number of time samples captured
void ReadPerformance()
{
char buf[200];
if(DeltaSamples != 0 )
{
DeltaTime = (DeltaTime*1000000)/CountFreq;
DeltaTimeMin = (DeltaTimeMin*1000000)/CountFreq;
DeltaTimeMax = (DeltaTimeMax*1000000)/CountFreq;
DeltaTimeAve = DeltaTimeAve/DeltaSamples;
DeltaTimeAve = (DeltaTimeAve*1000000)/CountFreq;
sprintf( buf, " Max=%I64u uSec Min=%I64u uSec\nAve=%I64u uSec #Samps=%I64u",
DeltaTimeMax,DeltaTimeMin,DeltaTimeAve,DeltaSamples);
AfxMessageBox( buf );
}
}
/////////////////////////////////////////////////////////////////////////
////// P a t h S i m T e s t B e n c h R o u t i n e s ////////////
/////////////////////////////////////////////////////////////////////////
// Measure RMS power of complex sample over 'samplength' samples
// from sweep generator. Save results into a file.
void CalcCpxSweepRMS(cmplx sample, int samplength)
{
if(testfreq<0.0) //if first call then ignor this call
return;
if( delay ) //allow things to settle before starting sampling
{
if(++cnt >= samplength )
{
delay = false;
cnt = 0;
}
}
else
{
sum = sum + ( sample.x*sample.x + sample.y*sample.y);
if(++cnt >= samplength)
{
rms = sqrt(sum/(double)samplength);
sum = 0.0;
cnt = 0;
delay = true; // delay one run to allow things to settle
if(testfreq <= endfreq)
{
fprintf( stream, "%g, %g\n", testfreq, 20*log10(rms + 1e-100) );
testfreq += freqinc;
gDebug1 = rms;
gDebug2 = testfreq;
}
}
}
}
// Measure RMS power of real sample over 'samplength' samples
// from sweep generator. Save results into a file.
void CalcSweepRMS(double sample, int samplength)
{
if(testfreq<0.0) //if first call then ignor this call
return;
if( delay ) //allow things to settle before starting sampling
{
if(++cnt >= samplength )
{
delay = false;
cnt = 0;
}
}
else
{
sum = sum + (sample*sample);
if(++cnt >= samplength)
{
rms = sqrt(sum/(double)samplength);
sum = 0.0;
cnt = 0;
delay = true; // delay one run to allow things to settle
if(testfreq <= endfreq)
{
fprintf( stream, "%g, %g\n", testfreq, 20*log10(rms) );
testfreq += freqinc;
gDebug1 = rms;
gDebug2 = testfreq;
}
}
}
}
// Measure phase angle of complex sinwave sample over 'samplength' samples
// from sweep generator. Save results into a file.
// Uses trig identity and assumes sinwave inputs of same frequency:
// sin(a)sin(b) = 1/2 cos(a-b) - 1/2 cos(a+b)
// LP filter(average) the cos(a+b) term out and it leaves 1/2cos(phzedif)
// then take inv cos to get angle.
void CalcCpxSweepPhz(cmplx sample, int samplength)
{
double mag;
if(testfreq<0.0) //if first call then ignor this call
return;
if( delay ) //allow things to settle before starting sampling
{
if(++cnt >= samplength )
{
delay = false;
cnt = 0;
}
}
else
{ //normalize amplitude.
mag = (sample.x*sample.x+sample.y*sample.y);
sum = sum + sample.x*sample.y/mag; //add sin(a)*sin(b) term
if(++cnt >= samplength)
{
rms = 360.0*acos(2*sum/(double)samplength)/K_2PI; //convert to angle
sum = 0.0;
cnt = 0;
delay = true; // delay one run to allow things to settle
if(testfreq <= endfreq)
{
fprintf( stream, "%g, %g\n", testfreq, rms);
testfreq += freqinc;
gDebug1 = rms;
gDebug2 = testfreq;
}
}
}
}
// Generate a RMS=1 real sinwave sweep frequency from start to stop
// with given sample rate and step size.
void SweepGen( double* output, double samprate,
double start, double stop, double step )
{
if(testfreq<0.0) //if first call then initialize everything
{
timeinc = 0.0;
testfreq = start;
endfreq = stop;
freqinc = step;
sum = 0.0;
rms = 0.0;
cnt = 0;
delay = true; // delay one run to allow things to settle
stream = fopen( "c:\\Data.prn", "wt" ); //Open file for write
}
*output = 1.414213562*sin(timeinc);
timeinc += ( (K_2PI/samprate)*testfreq);
timeinc = fmod(timeinc,K_2PI); //keep radian counter bounded
}
// Generate a RMS=1 complex sinwave sweep frequency from start to stop
// with given sample rate and step size.
void SweepGenCpx( cmplx* output, double samprate,
double start, double stop, double step )
{
if(testfreq<0.0) //if first call then initialize everything
{
timeinc = 0.0;
testfreq = start;
endfreq = stop;
freqinc = step;
sum = 0.0;
rms = 0.0;
delay = true; // delay one run to allow things to settle
cnt = 0;
stream = fopen( "c:\\Data.prn", "wt" ); //Open file for write
}
output->x = cos(timeinc);
output->y = sin(timeinc);
timeinc += ( (K_2PI/samprate)*testfreq);
timeinc = fmod(timeinc,K_2PI); //keep radian counter bounded
}
void HistogramSamp( double sample, double min, double max, int numsamps )
{
double K;
int i;
int hmax;
if(!delay) //if first call then initialize everything
{
cnt = 0;
delay = true; // delay one run to allow things to settle
stream = fopen( "c:\\Data.prn", "wt" ); //Open file for write
for(i=0;i<K_HIST_RES; i++)
HistArray[i] = 0;
}
if(cnt < numsamps)
{
K = (K_HIST_RES-1)/(max-min);
i = (int)( K*(sample-min) );
if( i>0 && i <K_HIST_RES)
HistArray[i]++;
cnt++;
gDebug1 = (double)cnt;
}
if(cnt == numsamps)
{
hmax = 0;
for(i=0;i<K_HIST_RES; i++)
{
if( HistArray[i] > hmax)
hmax = HistArray[i];
}
for(i=0;i<K_HIST_RES; i++)
{
K = (double)i*(max-min)/(double)K_HIST_RES + min;
fprintf( stream, "%g, %g\n", K,
(double)HistArray[i]/(double)hmax);
}
cnt++;
}
}
//call this to end the test and close the data file if open;
void EndTest(void)
{
delay = false;
timeinc = 0.0;
testfreq = -1.0;
if (stream)
fclose( stream );
}
///////////////////////////////////////////////////////////////////
//calculates RMS value of complex sample over numsamps
// called once for each sample being measured
///////////////////////////////////////////////////////////////////
double CalcCpxRMS(cmplx sample, int numsamps)
{
static double sum = 0.0;
static double rms = 0.0;
sum = sum + ( sample.x*sample.x + sample.y*sample.y);
if(++cnt >= numsamps)
{
rms = sqrt(sum/(double)numsamps);
sum = 0.0;
cnt = 0;
}
return rms;
}
///////////////////////////////////////////////////////////////////
//calculates running average RMS value of buf[] samples
// called once for each buffer to be measured
///////////////////////////////////////////////////////////////////
double CalcRunAveRMS(double* buf, int bufsize)
{
#define RMSBAVE 1000
static double sum = 0.0;
static double rms = 0.0;
sum = 0.0;
for( int i = 0; i<bufsize; i++)
sum = sum + ( buf[i]*buf[i] );
rms = (1.0/RMSBAVE)*sqrt(sum/bufsize) + (1.0-1.0/RMSBAVE)*rms;
return rms;
}
///////////////////////////////////////////////////////////////////
//calculates running average RMS value of buf[] complex samples
// called once for each buffer to be measured
///////////////////////////////////////////////////////////////////
double CalcCpxRunAveRMS(cmplx* buf, int bufsize)
{
#define RMSCPXAVE 300
static double sum = 0.0;
static double rms = 0.0;
for( int i = 0; i<bufsize; i++)
sum = sum + ( buf[i].x*buf[i].x + buf[i].y*buf[i].y);
rms = (1.0/RMSCPXAVE)*sqrt(sum/bufsize) + (1.0-1.0/RMSCPXAVE)*rms;
sum = 0.0;
return rms;
}
///////////////////////////////////////////////////////////////////
//calculates RMS value of buf[] samples over RMS_SIZE*numsamps
// called once for each buffer to be measured
///////////////////////////////////////////////////////////////////
double CalcTotalRMS(double* buf, int numsamps)
{
#define RMS_SIZE 7000 //abt 30 minutes at 8000 KSP
static double sum = 0.0;
static int cnt = 0;
static double rms = 0.0;
for( int i = 0; i<numsamps; i++)
sum = sum + ( buf[i]*buf[i] );
if( ++cnt >= RMS_SIZE)
{
rms = sqrt(sum/(cnt*numsamps));
sum = 0.0;
cnt = 0;
}
return rms;
}
} // namespace PathSim
#endif // PATHSIM_TESTMODE