]> git.tdb.fi Git - libs/math.git/commitdiff
Add a simple linearly interpolated "spline"
authorMikko Rasa <tdb@tdb.fi>
Wed, 5 Jun 2019 21:22:10 +0000 (00:22 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 5 Jun 2019 21:22:10 +0000 (00:22 +0300)
source/interpolate/linearspline.h [new file with mode: 0644]

diff --git a/source/interpolate/linearspline.h b/source/interpolate/linearspline.h
new file mode 100644 (file)
index 0000000..2c88ceb
--- /dev/null
@@ -0,0 +1,42 @@
+#ifndef MSP_INTERPOLATE_LINEARSPLINE_H_
+#define MSP_INTERPOLATE_LINEARSPLINE_H_
+
+#include <vector>
+#include "spline.h"
+
+namespace Msp {
+namespace Interpolate {
+
+/**
+A very simple type of spline.  It interpolates the value linearly between
+knots.
+*/
+template<typename T, unsigned N = 1>
+class LinearSpline: public Spline<T, 1, N>
+{
+public:
+       using typename Spline<T, 1, N>::Value;
+       using typename Spline<T, 1, N>::Knot;
+
+       LinearSpline(const std::vector<Knot> &k):
+               Spline<T, 1, N>(k.front())
+       {
+               typedef SplineValue<T, N> SV;
+
+               this->reserve(k.size()-1);
+               for(unsigned i=1; i<k.size(); ++i)
+               {
+                       T dx = k[i].x-k[i-1].x;
+                       Value slope = (k[i].y-k[i-1].y)/dx;
+                       Polynomial<T, 1> p[N];
+                       for(unsigned j=0; j<N; ++j)
+                               p[j] = Polynomial<T, 1>(LinAl::Vector<T, 2>(SV::get(slope, j), SV::get(k[i-1].y, j)-k[i-1].x*SV::get(slope, j)));
+                       this->add_segment(p, k[i].x);
+               }
+       }
+};
+
+} // namespace Interpolate
+} // namespace Msp
+
+#endif