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
16const std::vector<double>
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 std::vector<double> result(v.size());
80
81 switch(preprocess) {
83 Eigen::VectorXd::Map(result.data(), result.size()) = v.unaryExpr(
84 [](const double x) {
85 return std::exp(std::exp(x) - 1) - 1;
86 }
87 );
88 break;
90 Eigen::VectorXd::Map(result.data(), result.size()) = v.unaryExpr(
91 [](const double x) {
92 double t = std::exp(std::exp(x) - 1) - 1;
93 return t * t - 1;
94 }
95 );
96 break;
97 default:
98 Eigen::VectorXd::Map(result.data(), result.size()) = v;
99 }
100
101 return result;
102}
103
104}; // namespace sablib
const std::vector< double > 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:26