sablib
Loading...
Searching...
No Matches
snip.cpp
Go to the documentation of this file.
1
6
7#include <algorithm>
8#include <cmath>
9
10#include <Eigen/Eigen>
11
12#include "snip.h"
13
14namespace sablib {
15
18 const std::vector<double> & y, const unsigned int m, const bool decreasing,
19 const SnipPreprocess preprocess, const unsigned int loop
20)
21{
22 if(y.size() == 0) {
23 throw std::invalid_argument("BaselineSnip(): the length of y is zero.");
24 }
25
26 if(y.size() < 2 * m) {
27 throw std::invalid_argument("BaselineSnip(): the length of y is shorter than 2 * m.");
28 }
29
30 if(m == 0) {
31 throw std::invalid_argument("BaselineSnip(): m is zero.");
32 }
33
34 if(loop == 0) {
35 throw std::invalid_argument("BaselineSnip(): loop is zero.");
36 }
37
38 Eigen::VectorXd yy;
39 unsigned int n = (unsigned int)y.size();
40
41 switch(preprocess) {
43 yy = Eigen::VectorXd::Map(y.data(), n).unaryExpr(
44 [](const double x) {
45 return x > 0 ? std::log(std::log(x + 1) + 1) : 0;
46 }
47 );
48 break;
50 yy = Eigen::VectorXd::Map(y.data(), n).unaryExpr(
51 [](const double x) {
52 return x > 0 ? std::log(std::log(std::sqrt(x + 1) + 1) + 1) : 0;
53 }
54 );
55 break;
56 default:
57 yy = Eigen::VectorXd::Map(y.data(), n);
58 }
59
60 std::vector<unsigned int> index;
61
62 for(unsigned int i = 0; i < m; i++){
63 index.emplace_back(decreasing ? (m - i) : (i + 1));
64 }
65
66 Eigen::VectorXd v = yy;
67
68 for(unsigned int i = 0; i < loop; i++) {
69 for(auto j : index) {
70 Eigen::VectorXd v_prev = v;
71
72 for(unsigned int k = j; k < n - j; k++) {
73 double ave = (v_prev(k + j) + v_prev(k - j)) / 2;
74 v(k) = std::min(v_prev(k), ave);
75 }
76 }
77 }
78
79 BaselineResult result;
80 result.baseline.resize(yy.size());
81 result.corrected.resize(yy.size());
82
83 switch(preprocess) {
85 Eigen::VectorXd::Map(result.baseline.data(), result.baseline.size()) = v.unaryExpr(
86 [](const double x) {
87 return std::exp(std::exp(x) - 1) - 1;
88 }
89 );
90 break;
92 Eigen::VectorXd::Map(result.baseline.data(), result.baseline.size()) = v.unaryExpr(
93 [](const double x) {
94 double t = std::exp(std::exp(x) - 1) - 1;
95 return t * t - 1;
96 }
97 );
98 break;
99 default:
100 Eigen::VectorXd::Map(result.baseline.data(), result.baseline.size()) = v;
101 }
102
103 for(unsigned int i = 0; i < result.baseline.size(); i++) {
104 result.corrected[i] = y[i] - result.baseline[i];
105 }
106
107 return result;
108}
109
110}; // namespace sablib
const BaselineResult BaselineSnip(const std::vector< double > &y, const unsigned int m, const bool decreasing, const SnipPreprocess preprocess, const unsigned int loop)
Performs baseline estimation using the Statistics-sensitive Non-linear Iterative Peak-clipping (SNIP)...
Definition snip.cpp:17
Baseline estimation using Statistics-sensitive Non-linear Iterative Peak-clipping(SNIP).
SnipPreprocess
Preprocessing types for the SNIP algorithm.
Definition snip.h:28
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