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