-
Notifications
You must be signed in to change notification settings - Fork 4
/
DataInterpolation.hpp
215 lines (197 loc) · 9.78 KB
/
DataInterpolation.hpp
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
/******************************************************************************/
/* */
/* Copyright 2016-2017 Steven Dolly */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at: */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* */
/******************************************************************************/
////////////////////////////////////////////////////////////////////////////////
// //
// DataInterpolation.hpp //
// Data Interpolation Functions //
// Created September 27, 2016 (Steven Dolly) //
// //
// This header file contains template functions to interpolate one- and two- //
// dimensional data tables. //
// //
////////////////////////////////////////////////////////////////////////////////
// Header guard
#ifndef DATAINTERPOLATION_HPP
#define DATAINTERPOLATION_HPP
// Standard C++ header files
#include <vector>
#include <utility>
// Standard C header files
#include <cmath>
namespace solutio
{
// Utility function to find index (vector)
template <class T>
int FindIndex(const std::vector<T> &axis_data, T value)
{
int index = 0;
if(axis_data[0] < axis_data[1])
{
while((index < axis_data.size()) && (value >= axis_data[index])) index++;
}
else
{
while((index < axis_data.size()) && (value <= axis_data[index])) index++;
}
if(index <= 0) index = 1;
if(index >= axis_data.size()) index = axis_data.size()-1;
return index;
}
// Utility function to find index (pair vector)
template <class T>
int FindIndex(const std::vector< std::pair<T,T> > &data, T value)
{
int index = 0;
if(data[0].first < data[1].first)
{
while((index < data.size()) && (value >= data[index].first)) index++;
}
else
{
while((index < data.size()) && (value <= data[index].first)) index++;
}
if(index <= 0) index = 1;
if(index >= data.size()) index = data.size()-1;
return index;
}
//////////////////////////////////////////////////////////////////////////////
// //
// Normal linear interpolation: unspecified sample size //
// //
// Normal interpolation for data with unknown and/or irregularly-spaced //
// samples. The row and column indices are found by search comparison, //
// followed by linear interpolation. Various dimensions (e.g. 1D, 2D) and //
// data containers (e.g. array, vector, pair) are included in the template //
// functions). //
// //
//////////////////////////////////////////////////////////////////////////////
// Normal linear interpolation for 1D vector data
template <class T>
T LinearInterpolation(const std::vector<T> &x_data, const std::vector<T> &y_data, T x_value)
{
int index = FindIndex(x_data, x_value);
T f = (x_value - x_data[(index-1)]) / (x_data[index] - x_data[(index-1)]);
T y_value = f*y_data[index] + (1-f)*y_data[(index-1)];
return y_value;
}
// Normal linear interpolation for 2D vector data
template <class T>
T LinearInterpolation(const std::vector<T> &x_data, const std::vector<T> &y_data,
const std::vector< std::vector<T> > &table, T x_value, T y_value)
{
int index = FindIndex(x_data, x_value);
T y_1 = LinearInterpolation(y_data, table[(index-1)], y_value);
T y_2 = LinearInterpolation(y_data, table[index], y_value);
T f = (x_value - x_data[(index-1)]) / (x_data[index] - x_data[(index-1)]);
T t_value = f*y_2 + (1-f)*y_1;
return t_value;
}
// Normal linear interpolation for 1D vector<pair> data
template <class T>
T LinearInterpolation(const std::vector< std::pair<T,T> > &data, T x_value)
{
int index = FindIndex(data, x_value);
T f = (x_value - data[(index-1)].first) / (data[index].first - data[(index-1)].first);
T y_value = f*data[index].second + (1-f)*data[(index-1)].second;
return y_value;
}
//////////////////////////////////////////////////////////////////////////////
// //
// Fast linear interpolation: specified sample size //
// //
// Fast interpolation for data known and regularly-spaced sample size. The //
// row and column indices calculated from the given row and column spacing, //
// followed by linear interpolation. Various dimensions (e.g. 1D, 2D) and //
// data containers (e.g. array, vector, pair) are included in the template //
// functions). //
// //
//////////////////////////////////////////////////////////////////////////////
// Fast linear interpolation for 1D vector data
template <class T>
T LinearInterpolationFast(const std::vector<T> &x_data, const std::vector<T> &y_data,
T x_value, T delta_x)
{
int index = ceil((x_value-x_data[0])/delta_x);
if(index <= 0) index = 1;
if(index >= x_data.size()) index = x_data.size()-1;
T f = (x_value - x_data[(index-1)]) / (x_data[index] - x_data[(index-1)]);
T y_value = f*y_data[index] + (1-f)*y_data[(index-1)];
return y_value;
}
// Fast linear interpolation for 2D vector data
template <class T>
T LinearInterpolationFast(const std::vector<T> &x_data, const std::vector<T> &y_data,
const std::vector< std::vector<T> > &table, T x_value, T y_value, T delta_x,
T delta_y)
{
int index = ceil((x_value-x_data[0])/delta_x);
if(index <= 0) index = 1;
if(index >= x_data.size()) index = x_data.size()-1;
T y_1 = LinearInterpolationFast(y_data, table[(index-1)], y_value, delta_y);
T y_2 = LinearInterpolationFast(y_data, table[index], y_value, delta_y);
T f = (x_value - x_data[(index-1)]) / (x_data[index] - x_data[(index-1)]);
T t_value = f*y_2 + (1-f)*y_1;
return t_value;
}
// Fast linear interpolation for 1D vector<pair> data
template <class T>
T LinearInterpolationFast(const std::vector< std::pair<T,T> > &data,
T x_value, T delta_x)
{
int index = ceil((x_value-data[0].first)/delta_x);
T f = (x_value - data[(index-1)].first) / (data[index].first - data[(index-1)].first);
T y_value = f*data[index].second + (1-f)*data[(index-1)].second;
return y_value;
}
//////////////////////////////////////////////////////////////////////////////
// //
// Normal logarithmic interpolation: unspecified sample size //
// //
// Normal interpolation for data with unknown and/or irregularly-spaced //
// samples. The row and column indices are found by search comparison, //
// followed by logarithmic interpolation. Various dimensions (e.g. 1D, 2D) //
// and data containers (e.g. array, vector, pair) are included in the //
// template functions). //
// //
//////////////////////////////////////////////////////////////////////////////
// Normal log interpolation for 1D vector data
template <class T>
T LogInterpolation(const std::vector<T> &x_data, const std::vector<T> &y_data, T x_value)
{
int index = FindIndex(x_data, x_value);
T f = (log10(x_value) - log10(x_data[(index-1)])) /
(log10(x_data[index]) - log10(x_data[(index-1)]));
T y_value = (pow(y_data[index], f) * pow(y_data[(index-1)],(1-f)));
return y_value;
}
// Normal log interpolation for 2D vector data
template <class T>
T LogInterpolation(const std::vector<T> &x_data, const std::vector<T> &y_data,
const std::vector< std::vector<T> > &table, T x_value, T y_value)
{
int index = FindIndex(x_data, x_value);
T y_1 = LogInterpolation(y_data, table[(index-1)], y_value);
T y_2 = LogInterpolation(y_data, table[index], y_value);
T f = (log10(x_value) - log10(x_data[(index-1)])) /
(log10(x_data[index]) - log10(x_data[(index-1)]));
T t_value = (pow(y_2, f) * pow(y_1, (1-f)));
return t_value;
}
};
// End header guard
#endif