]> git.tdb.fi Git - libs/math.git/blob - source/interpolate/linearspline.h
Add a simple linearly interpolated "spline"
[libs/math.git] / source / interpolate / linearspline.h
1 #ifndef MSP_INTERPOLATE_LINEARSPLINE_H_
2 #define MSP_INTERPOLATE_LINEARSPLINE_H_
3
4 #include <vector>
5 #include "spline.h"
6
7 namespace Msp {
8 namespace Interpolate {
9
10 /**
11 A very simple type of spline.  It interpolates the value linearly between
12 knots.
13 */
14 template<typename T, unsigned N = 1>
15 class LinearSpline: public Spline<T, 1, N>
16 {
17 public:
18         using typename Spline<T, 1, N>::Value;
19         using typename Spline<T, 1, N>::Knot;
20
21         LinearSpline(const std::vector<Knot> &k):
22                 Spline<T, 1, N>(k.front())
23         {
24                 typedef SplineValue<T, N> SV;
25
26                 this->reserve(k.size()-1);
27                 for(unsigned i=1; i<k.size(); ++i)
28                 {
29                         T dx = k[i].x-k[i-1].x;
30                         Value slope = (k[i].y-k[i-1].y)/dx;
31                         Polynomial<T, 1> p[N];
32                         for(unsigned j=0; j<N; ++j)
33                                 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)));
34                         this->add_segment(p, k[i].x);
35                 }
36         }
37 };
38
39 } // namespace Interpolate
40 } // namespace Msp
41
42 #endif