[315] | 1 | #include "toiinterpolator.h"
|
---|
| 2 |
|
---|
| 3 | void TOIInterpolator::enterValue(double value, long sample)
|
---|
| 4 | {
|
---|
| 5 | // $CHECK$ sample > back.sample
|
---|
| 6 | if (!hist.empty() && sample == hist.back().t) return; // pour eviter /0
|
---|
| 7 | hist.push_back(pair(value, sample));
|
---|
| 8 | }
|
---|
| 9 |
|
---|
| 10 | bool TOIInterpolator::needMoreDataFor(long sample)
|
---|
| 11 | {
|
---|
| 12 | if (hist.empty()) return true;
|
---|
| 13 | int xx = hist.back().t;
|
---|
| 14 | return (hist.empty() || sample > xx);
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | bool TOIInterpolator::canGet(long sample)
|
---|
| 18 | {
|
---|
| 19 | return (!hist.empty() && (sample >= hist.front().t && sample <= hist.back().t));
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | double TOIInterpolator::getEValue(long sample) // can forget before sample
|
---|
| 23 | {
|
---|
| 24 | if (!canGet(sample)) return -9.e99; // user should have checked before
|
---|
| 25 | deque<pair>::iterator i = hist.begin();
|
---|
| 26 | while ((*i).t < sample) i++;
|
---|
| 27 | if ((*i).t > sample) i--;
|
---|
| 28 | double value = (*i).val;
|
---|
| 29 | if (i != hist.begin()) {
|
---|
| 30 | i--;
|
---|
| 31 | hist.erase(hist.begin(), i);
|
---|
| 32 | }
|
---|
| 33 | return value;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | double TOIInterpolator::getIValue(long sample) // can forget before sample
|
---|
| 37 | {
|
---|
| 38 | if (!canGet(sample)) return -9.e99; // user should have checked before
|
---|
| 39 |
|
---|
| 40 | deque<pair>::iterator i = hist.begin();
|
---|
| 41 | while ((*i).t < sample) i++;
|
---|
| 42 | double value;
|
---|
| 43 | if ((*i).t == sample) {
|
---|
| 44 | value = (*i).val;
|
---|
| 45 | } else {
|
---|
| 46 | long samp2 = (*i).t;
|
---|
| 47 | double val2 = (*i).val;
|
---|
| 48 | i--;
|
---|
| 49 | long samp1 = (*i).t;
|
---|
| 50 | double val1 = (*i).val;
|
---|
| 51 | value = val1 + (val2-val1)*(sample-samp1)/(samp2-samp1);
|
---|
| 52 | }
|
---|
| 53 | if (i != hist.begin()) {
|
---|
| 54 | i--;
|
---|
| 55 | hist.erase(hist.begin(), i);
|
---|
| 56 | }
|
---|
| 57 | return value;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | long TOIInterpolator::nextSample(long sampleMin)
|
---|
| 61 | {
|
---|
| 62 | for (deque<pair>::iterator i = hist.begin(); i<hist.end(); i++)
|
---|
| 63 | if ((*i).t >= sampleMin) return (*i).t;
|
---|
| 64 | return -1;
|
---|
| 65 | }
|
---|
| 66 |
|
---|