-
Notifications
You must be signed in to change notification settings - Fork 0
/
libEDM_library.cpp
355 lines (289 loc) · 8.52 KB
/
libEDM_library.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
#define _USE_MATH_DEFINES
#include <complex>
#include <cmath>
#include <string>
#include <libEDM_fft.h>
#include <libEDM_library.h>
#include <libEDM_random.h>
using std::abs;
using std::conj;
using std::min;
using std::max;
using std::norm;
using std::pair;
using std::string;
void error (string &errorMessage)
{
cerr << errorMessage << endl;
abort();
}
void error (char *errorMessage)
{
error(string(errorMessage));
}
cVector conj (const cVector &x)
{
cVector output;
for (size_t i=0; i<x.size(); i++)
output.push_back(conj(x[i]));
return output;
}
dVector norm (const cVector &x)
{
dVector output;
for (size_t i=0; i<x.size(); i++)
output.push_back(norm(x[i]));
return output;
}
dVector real (const cVector &x)
{
dVector output;
for (size_t i=0; i<x.size(); i++)
output.push_back(x[i].real());
return output;
}
dVector imag (const cVector &x)
{
dVector output;
for (size_t i=0; i<x.size(); i++)
output.push_back(x[i].imag());
return output;
}
dVector abs (const cVector &x)
{
dVector output;
for (size_t i=0; i<x.size(); i++)
output.push_back(abs(x[i]));
return output;
}
dVector phase (const cVector &x)
{
dVector output;
for (size_t i=0; i<x.size(); i++)
output.push_back(atan2(x[i].real(), x[i].imag()));
return output;
}
dVector xcorr (const dVector &x, const dVector &y)
{
const size_t vectorLength = x.size();
// copy dVectors to cVectors
cVector cx(vectorLength);
cVector cy(vectorLength);
for (size_t i=0; i<vectorLength; i++)
{
cx[i] = x[i];
cy[i] = y[i];
}
cVector ccorr = xcorr(cx, cy);
// copy real parts to output
dVector output;
for (size_t i=0; i<2*vectorLength; i++)
output.push_back(ccorr[i].real());
return output;
}
cVector xcorr (const cVector &x, const cVector &y)
{
const size_t vectorLength = x.size();
// check vectors are same length
assert( vectorLength == y.size() );
// zero pad vectors
cVector cx = x;
cVector cy = y;
cx.resize(2*vectorLength);
cy.resize(2*vectorLength);
// compute ffts
cVector fftx = FFT::fft(cx);
cVector ffty = FFT::fft(cy);
// take inverse of multiplication to get xcorrelation
cVector ccorr = conj(FFT::ifft(fftx * conj(ffty)));
// re-sort output, most negative lag first
cVector output = ccorr.right(vectorLength);
output.ins(vectorLength, ccorr.left(vectorLength));
return output;
}
size_t combinations (const size_t N, const size_t k)
{
if ( (N==k) || (k==0) )
return 1;
size_t answer = max(N-k+1, k+1);
size_t num = answer+1;
size_t den = 2;
while (num <= N)
{
answer *= num++;
answer /= den++;
}
return answer;
}
double binomial (const size_t N, const size_t k, const double p)
{
return combinations(N,k) * pow(p,k) * pow((1.0-p),(N-k));
}
double binomial_CDF (const size_t N, const size_t y, const double p)
{
double answer = 0.0;
for (size_t k=0; k<=y; k++)
answer += binomial(N,k,p);
return answer;
}
//
// class LookUpTable
//
LookUpTable::LookUpTable (ifstream &inputFile, const double yMinLimit, const double yMaxLimit) : _yMinLimit(yMinLimit), _yMaxLimit(yMaxLimit), _minX(dINFINITY), _minY(dINFINITY), _maxX(-dINFINITY), _maxY(-dINFINITY)
{
if ( !inputFile )
// NULL input file passed
error("NULL input file passed to LookUpTable constructor");
while ( !inputFile.eof() )
{
// read x value
char xBuffer[30];
inputFile.get(&xBuffer[0], 30, ',');
// read comma delimiter
inputFile.get();
// read y value
char yBuffer[30];
inputFile.get(&yBuffer[0], 30);
// read newline character
inputFile.get();
string xString = xBuffer;
string yString = yBuffer;
if ( !(xString.empty() || yString.empty()) )
add_point(atof(&xBuffer[0]), atof(&yBuffer[0]));
}
}
void LookUpTable::add_point (const double x, double y)
{
y = std::max(y, _yMinLimit);
y = std::min(y, _yMaxLimit);
_points.insert(pair<double,double>(x,y));
_minX = min(_minX, x);
_minY = min(_minY, y);
_maxX = max(_maxX, x);
_maxY = max(_maxY, y);
// check if LUT points are regularly sampled
_regularXSampling = true;
_samplingInterval = -1.0;
double currentX = 0.0;
for (const_iterator point = _points.begin(); point != _points.end(); point++)
{
double previousX = currentX;
currentX = point->first;
if (point != _points.begin())
{
double samplingInterval = currentX - previousX;
if (_samplingInterval == -1.0)
_samplingInterval = samplingInterval;
else
if (_samplingInterval != samplingInterval)
{
_regularXSampling = false;
break;
}
}
}
// store y values in vector if regularly sampled
if ( _regularXSampling )
{
_pointVector.clear();
for (const_iterator point = _points.begin(); point != _points.end(); point++)
_pointVector.push_back(point->second);
}
// check if monotonic rising or falling
_monotonicRising = true;
_monotonicFalling = true;
double currentY = 0.0;
for (const_iterator point = _points.begin(); point != _points.end(); point++)
{
double previousY = currentY;
currentY = point->second;
if (point != _points.begin())
{
if (currentY > previousY)
_monotonicFalling = false;
if (currentY < previousY)
_monotonicRising = false;
}
}
}
void LookUpTable::print()
{
for (const_iterator point = _points.begin(); point != _points.end(); point++)
{
cout << point->first << ",";
cout << point->second << endl;
}
cout << endl;
}
double LookUpTable::cdf_sample () const
{
return x(globalRandom.sample());
}
double LookUpTable::random_x () const
{
return x(globalRandom.sample() * (_maxY - _minY) + _minY);
}
double LookUpTable::random_y () const
{
return y(globalRandom.sample() * (_maxX - _minX) + _minX);
}
double LookUpTable::x (const double y) const
{
const_iterator upper = _points.begin(), lower;
for (const_iterator point = _points.begin(); point != _points.end(); point++)
{
lower = upper;
upper = point;
if ( upper != _points.begin() )
{
if ( _monotonicFalling && (upper->second < y) )
break;
if ( !_monotonicFalling && (upper->second > y) )
break;
}
}
double xDiff = upper->first - lower->first;
double yDiff = upper->second - lower->second;
double x = lower->first + xDiff * ((y - lower->second) / yDiff);
return x;
}
int LookUpTable::x (const int y) const
{
return static_cast<int>(x(static_cast<double>(y)));
}
double LookUpTable::y (const double x) const
{
double y;
if ( _regularXSampling )
{
// LUT is regularly sampled, so use _pointVector
int pointId = static_cast<int>((x - _points.begin()->first) / _samplingInterval);
pointId = max(pointId, 0);
pointId = min(pointId, static_cast<int>(numPoints()) - 2);
double upper = _pointVector[pointId + 1];
double lower = _pointVector[pointId];
y = lower + (upper - lower) * (x - (pointId * _samplingInterval + _points.begin()->first)) / _samplingInterval;
}
else
{
// LUT is not regularly sampled
const_iterator upper = _points.begin(), lower;
for (const_iterator point = _points.begin(); point != _points.end(); point++)
{
lower = upper;
upper = point;
if ( (upper != _points.begin()) && (upper->first > x) )
break;
}
double xDiff = upper->first - lower->first;
double yDiff = upper->second - lower->second;
y = lower->second + yDiff * ((x - lower->first) / xDiff);
}
y = std::max(y, _yMinLimit);
y = std::min(y, _yMaxLimit);
return y;
}
int LookUpTable::y(const int x) const
{
return static_cast<int>(y(static_cast<double>(x)));
}