sablib
Loading...
Searching...
No Matches
modpoly.cpp
Go to the documentation of this file.
1
6
7#include "../misc/polyfit.h"
8
9#include "modpoly.h"
10
11namespace sablib {
12
13//
14// Implementation of BaselineModPoly() function
15//
16const std::vector<double> BaselineModPoly(
17 const std::vector<double> & y, const unsigned int polyorder, const unsigned int loop, const double eps
18)
19{
20 if(y.size() == 0) {
21 throw std::invalid_argument("BaselineModPoly(): the length of y is zero.");
22 }
23
24 if(polyorder == 0) {
25 throw std::invalid_argument("BaselineModPoly(): polyorder is zero.");
26 }
27
28 if(loop == 0) {
29 throw std::invalid_argument("BaselineModPoly(): loop is zero.");
30 }
31
32 if(eps <= 0) {
33 throw std::invalid_argument("BaselineModPoly(): non-positive eps value is given.");
34 }
35
36 Eigen::VectorXd yy = Eigen::VectorXd::Map(y.data(), y.size());
37 Eigen::VectorXd x = Eigen::VectorXd::LinSpaced(y.size(), 0, 1);
38 Eigen::MatrixXd V = Vandermonde(x, polyorder);
39 Eigen::LDLT<Eigen::MatrixXd> ldltV = (V.transpose() * V).ldlt();
40 Eigen::VectorXd y_old = yy;
41
42 for(unsigned int i = 0; i < loop; i++) {
43 Eigen::VectorXd y_new = PolyVal(ldltV.solve(V.transpose() * y_old), V);
44
45 if((y_new - y_old).norm() / y_old.norm() < eps) {
46 break;
47 }
48
49 y_old = (y_new.array() > y_old.array()).select(y_old, y_new);
50 }
51
52 std::vector<double> result(y.size());
53 Eigen::VectorXd::Map(result.data(), result.size()) = y_old;
54
55 return result;
56}
57
58}; // namespace sablib
const std::vector< double > BaselineModPoly(const std::vector< double > &y, const unsigned int polyorder, const unsigned int loop, const double eps)
Estimates the baseline using the Modified Polynomial (ModPoly) method.
Definition modpoly.cpp:16
Baseline estimation using Modified Polynomial(ModPoly) method.
Polynomial fitting using the least squares method (Gauss-Newton for linear models) and evaluating pol...
const Derived::PlainObject PolyVal(const Eigen::MatrixBase< Derived > &coeff, const Eigen::MatrixX< typename Derived::PlainObject::Scalar > &V)
Evaluates the polynomial at specified points using a pre-calculated Vandermonde matrix.
Definition polyfit.h:122
const Eigen::MatrixX< typename Derived::PlainObject::Scalar > Vandermonde(const Eigen::MatrixBase< Derived > &x, const unsigned int polyorder)
Generates a Vandermonde matrix for a given vector and polynomial order.
Definition polyfit.h:25