-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogarithmicinterpolator.cpp
84 lines (73 loc) · 2.29 KB
/
logarithmicinterpolator.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
/*
Copyright 2011 Arne Jacobs <[email protected]>
This file is part of elektrocillin.
Elektrocillin is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Elektrocillin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Elektrocillin. If not, see <http://www.gnu.org/licenses/>.
*/
#include "logarithmicinterpolator.h"
#include <QDebug>
#include <cmath>
LogarithmicInterpolator::LogarithmicInterpolator(double base_) :
Interpolator(QVector<double>(), QVector<double>(), 2),
base(base_)
{
}
LogarithmicInterpolator::LogarithmicInterpolator(const QVector<double> &xx, const QVector<double> &yy, double base_) :
Interpolator(xx, yy, 2),
base(base_)
{
}
void LogarithmicInterpolator::save(QDataStream &stream) const
{
Interpolator::save(stream);
stream << base;
}
void LogarithmicInterpolator::load(QDataStream &stream)
{
Interpolator::load(stream);
stream >> base;
}
void LogarithmicInterpolator::setBase(double base)
{
this->base = base;
}
double LogarithmicInterpolator::interpolate(int j, double x)
{
Q_ASSERT(xx.size() >= 2);
if (j < 0) {
j = 0;
}
if (j >= xx.size() - 1) {
if (xx[xx.size() - 1] == xx[xx.size() - 2]) {
return yy.last();
} else {
j = xx.size() - 2;
}
}
if (xx[j] == xx[j + 1]) {
return yy[j];
} else if (x == xx[j]) {
return yy[j];
} else if (x == xx[j + 1]) {
return yy[j + 1];
} else {
double weight2 = (x - xx[j]) / (xx[j + 1] - xx[j]);
if (base <= 0.000000000000001) {
return yy[j + 1];
} else if (base >= 1000000000000000.0) {
return yy[j];
} else if (base != 1) {
weight2 = (1.0 - pow(base, weight2)) / (1.0 - base);
}
double weight1 = 1.0 - weight2;
return yy[j] * weight1 + yy[j + 1] * weight2;
}
}