Skip to content

Instantly share code, notes, and snippets.

@gorbatschow
Last active November 8, 2021 16:57
Show Gist options
  • Save gorbatschow/68ede79e1b724dea520e20a245e67925 to your computer and use it in GitHub Desktop.
Save gorbatschow/68ede79e1b724dea520e20a245e67925 to your computer and use it in GitHub Desktop.
Linear interpolation C++
// Linear interpolation procedure
// c++ code by [email protected]
// SEE
// http://www.cplusplus.com/forum/general/216928/
template<typename T>
inline T InterpolateLin(const T* data_x, const T* data_y, size_t data_length, T x, size_t& i)
{
if (x >= data_x[data_length - 2]) i = data_length - 2;
else while (x > data_x[i + 1]) i++;
const T dydx = (data_y[i + 1] - data_y[i]) / (data_x[i + 1] - data_x[i]);
return data_y[i] + dydx * (x - data_x[i]);
}
template<typename T>
inline void InterpolateLin(const T* data_x, const T* data_y, size_t data_length, const T* sampled_x, T* sampled_y, size_t threshold)
{
size_t j = 0;
for (size_t i = 0; i < threshold; i++) {
sampled_y[i] = InterpolateLin(data_x, data_y, data_length, sampled_x[i], j);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment