sablib
Loading...
Searching...
No Matches
whittaker.cpp
Go to the documentation of this file.
1
6
7#include "whittaker.h"
8
9namespace sablib {
10
11//
12// Implementation of Whittaker() function, std::vector<double> and using vector w version
13//
14const std::vector<double> Whittaker(
15 const std::vector<double> & y, const std::vector<double> & w,
16 const double lambda, const unsigned int s
17)
18{
19 if(y.size() == 0) {
20 throw std::invalid_argument("Whittaker(): length of y is zero.");
21 }
22
23 if(y.size() != w.size()) {
24 throw std::invalid_argument("Whittaker(): the sizes of y and w do not match.");
25 }
26
27 if(lambda <= 0) {
28 throw std::invalid_argument("Whittaker(): non-positive lambda value is given.");
29 }
30
31 if(s > 3 || s == 0) {
32 throw std::invalid_argument("Whittaker(): s must be 1, 2, or 3.");
33 }
34
35 Eigen::VectorXd yy = Eigen::VectorXd::Map(y.data(), y.size());
36 Eigen::VectorXd ww = Eigen::VectorXd::Map(w.data(), w.size());
37 Eigen::VectorXd z;
38 Eigen::SparseMatrix<double> I, D, lambdaDTD;
39
40 size_t m = y.size();
41
42 I.resize(m, m);
43 I.setIdentity();
44 D = Diff(I, s);
45 lambdaDTD = lambda * (D.transpose() * D);
46
47 z = Whittaker(yy, ww, lambdaDTD);
48
49 std::vector<double> result(z.size());
50 Eigen::VectorXd::Map(result.data(), result.size()) = z;
51
52 return result;
53}
54
55//
56// Implementation of Whittaker() function, std::vector<double> version
57//
58const std::vector<double> Whittaker(
59 const std::vector<double> & y, const double lambda, const unsigned int s
60)
61{
62 std::vector<double> w(y.size());
63
64 for(size_t i = 0; i < w.size(); i++) {
65 w[i] = 1;
66 }
67
68 return Whittaker(y, w, lambda, s);
69}
70
71}; // namespace sablib
const Derived::PlainObject Diff(const Eigen::MatrixBase< Derived > &m0, const int n=1, const Dir dir=Dir::RowWise)
Calculates the n-th discrete difference along the given axis.
Definition diff.h:32
const std::vector< double > Whittaker(const std::vector< double > &y, const std::vector< double > &w, const double lambda, const unsigned int s)
Performs Whittaker smoothing (std::vector<double> version, with weights).
Definition whittaker.cpp:14
Smoothing using Whittaker smoother.