sablib
Loading...
Searching...
No Matches
polynomial.cpp
Go to the documentation of this file.
1
6
7#include "../misc/polyfit.h"
8
9#include "polynomial.h"
10
11namespace sablib {
12
13//
14// Implementation of BaselineLinear() function
15//
16const BaselineResult BaselineLinear(const std::vector<double> & y, const unsigned int index1, const unsigned int index2)
17{
18 if(y.size() == 0) {
19 throw std::invalid_argument("BaselineLinear(): the length of y is zero.");
20 }
21
22 if(index1 >= index2 || y.size() <= index1 || y.size() <= index2) {
23 throw std::invalid_argument("BaselineLinear(): illegal indices");
24 }
25
26 BaselineResult result;
27 result.baseline = y;
28 result.corrected = y;
29
30 double m = (y[index2] - y[index1]) / (index2 - index1);
31
32 auto f = [&](const unsigned int x) {
33 return m * (x - index1) + y[index1];
34 };
35
36 for(unsigned int x = index1; x <= index2; x++) {
37 result.baseline[x] = f(x);
38 }
39
40 for(unsigned int i = 0; i < y.size(); i++) {
41 result.corrected[i] = y[i] - result.baseline[i];
42 }
43
44 return result;
45}
46
47//
48// Implementation of BaselinePolynomial() function
49//
51 const std::vector<double> & y, const unsigned int polyorder, const std::vector<unsigned int> & indices
52)
53{
54 if(y.size() == 0 || indices.size() == 0) {
55 throw std::invalid_argument("BaselinePolynomial(): the length of y or indices is zero.");
56 }
57
58 if(y.size() < indices.size()) {
59 throw std::invalid_argument("BaselinePolynomial(); the length of indices is larger than y.");
60 }
61
62 if(polyorder >= indices.size()) {
63 throw std::invalid_argument("BaselinePolynomial(): Too few indices.");
64 }
65
66 double max_index = y.size() - 1;
67 std::vector<unsigned int> sorted_indices = indices;
68 Eigen::VectorXd xx(indices.size()), yy(indices.size());
69
70 std::sort(sorted_indices.begin(), sorted_indices.end());
71
72 for(unsigned int i = 0; i < indices.size(); i++) {
73 xx(i) = sorted_indices[i] / max_index;
74 yy(i) = y[sorted_indices[i]];
75 }
76
77 Eigen::VectorXd coefficients = PolyFit(xx, yy, polyorder);
78
79 BaselineResult result;
80 result.baseline = y;
81 result.corrected = y;
82
83 for(unsigned int i = sorted_indices[0]; i < sorted_indices.back(); i++) {
84 double x = i / max_index;
85 double fx = 0;
86 double k = 1;
87
88 for(int j = 0; j < coefficients.size(); j++) {
89 fx += coefficients(j) * k;
90 k *= x;
91 }
92
93 result.baseline[i] = fx;
94 }
95
96 for(unsigned int i = 0; i < y.size(); i++) {
97 result.corrected[i] = y[i] - result.baseline[i];
98 }
99
100 return result;
101}
102
103}; // namespace sablib
Polynomial fitting using the least squares method (Gauss-Newton for linear models) and evaluating pol...
const Derived::PlainObject PolyFit(const Eigen::MatrixX< typename Derived::PlainObject::Scalar > &V, const Eigen::MatrixBase< Derived > &y)
Solves the polynomial least squares fitting problem using a pre-calculated Vandermonde matrix.
Definition polyfit.h:66
const BaselineResult BaselineLinear(const std::vector< double > &y, const unsigned int index1, const unsigned int index2)
Performs baseline estimation with a linear line between two points.
const BaselineResult BaselinePolynomial(const std::vector< double > &y, const unsigned int polyorder, const std::vector< unsigned int > &indices)
Performs baseline estimation by fitting a polynomial to specified points.
Baseline estimation with polynomial line.
Return value structure for baseline estimation functions.
Definition result_type.h:19
std::vector< double > baseline
Definition result_type.h:20
std::vector< double > corrected
Definition result_type.h:21