sablib
Loading...
Searching...
No Matches
airpls.cpp
Go to the documentation of this file.
1
6
7#include <cmath>
8
10
11#include "airpls.h"
12
13namespace sablib {
14
15//
16// Implementation of BaselineAirPLS() function
17//
18const std::vector<double> BaselineAirPLS(
19 std::vector<double> & y, const double lambda, const unsigned int s,
20 const unsigned int loop, const double eps
21)
22{
23 if(y.size() == 0) {
24 throw std::invalid_argument("BaselineAirPLS(): the length of y is zero.");
25 }
26
27 if(lambda <= 0) {
28 throw std::invalid_argument("BaselineAirPLS(): non-positive lambda value is given.");
29 }
30
31 if(s == 0 || s > 3) {
32 throw std::invalid_argument("BaselineAirPLS(): s must be 1, 2 or 3.");
33 }
34
35 if(loop == 0) {
36 throw std::invalid_argument("BaselineAirPLS(): loop is zero.");
37 }
38
39 if(eps <= 0) {
40 throw std::invalid_argument("BaselineAirPLS(): non-positive eps value is given.");
41 }
42
43 size_t m = y.size();
44 Eigen::VectorXd yy, w, z, d;
45 Eigen::SparseMatrix<double> I, D, lambdaDTD;
46 double y_abs_sum, d_sum_abs;
47
48 yy = Eigen::VectorXd::Map(y.data(), m);
49
50 w.setOnes(m);
51 y_abs_sum = yy.array().abs().matrix().sum();
52
53 I.resize(m, m);
54 I.setIdentity();
55 D = Diff(I, s);
56 lambdaDTD = lambda * (D.transpose() * D);
57
58 for(unsigned int i = 0; i < loop; i++) {
59 z = Whittaker(yy, w, lambdaDTD);
60
61 d = (yy.array() >= z.array()).select(0, yy - z);
62 d_sum_abs = std::fabs(d.sum());
63
64 if (d_sum_abs < eps * y_abs_sum) {
65 break;
66 }
67
68 w = (yy.array() >= z.array()).select(0, ((loop * d.array().abs()) / d_sum_abs).exp());
69 w(0) = w(w.size() - 1) = std::exp((loop * d.maxCoeff() / d_sum_abs));
70 }
71
72 std::vector<double> result(z.size());
73 Eigen::VectorXd::Map(result.data(), result.size()) = z;
74
75 return result;
76}
77
78}; // namespace sablib
const std::vector< double > BaselineAirPLS(std::vector< double > &y, const double lambda, const unsigned int s, const unsigned int loop, const double eps)
Performs baseline estimation using adaptive iteratively reweighted Penalized Least Squares(airPLS).
Definition airpls.cpp:18
Baseline estimation using adaptive iteratively reweighted Penalized Least Squares(airPLS).
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.