sablib
Loading...
Searching...
No Matches
smoothing.cpp
Go to the documentation of this file.
1
6
7#include <algorithm>
8#include <vector>
9
10#include "smoothing.h"
11
17
18//
19// Implementation of Sablib_MovingAverage() function.
20//
21const SABLIB_DATA_PTR Sablib_MovingAverage(const SABLIB_DATA_PTR y, const unsigned int n)
22{
23 std::vector<double> yy;
24 yy.assign(y->data, y->data + y->size);
25
26 auto result = sablib::MovingAverage(yy, n);
27
28 SABLIB_DATA_PTR p = AllocSablibData(result.size());
29 std::copy(result.begin(), result.end(), p->data);
30
31 return p;
32}
33
34//
35// Implementation of Sablib_WeightedMovingAverage() function.
36//
37const SABLIB_DATA_PTR Sablib_WeightedMovingAverage(const SABLIB_DATA_PTR y, const SABLIB_DATA_PTR w)
38{
39 std::vector<double> yy, ww;
40 yy.assign(y->data, y->data + y->size);
41 ww.assign(w->data, w->data + w->size);
42
43 auto result = sablib::WeightedMovingAverage(yy, ww);
44
45 SABLIB_DATA_PTR p = AllocSablibData(result.size());
46 std::copy(result.begin(), result.end(), p->data);
47
48 return p;
49}
50
51//
52// Implementation of Sablib_GaussianKernel() function.
53//
54const SABLIB_DATA_PTR Sablib_GaussianKernel(const unsigned int n, const double sigma)
55{
56 auto result = sablib::GaussianKernel(n, sigma);
57
58 SABLIB_DATA_PTR p = AllocSablibData(result.size());
59 std::copy(result.begin(), result.end(), p->data);
60
61 return p;
62}
63
64//
65// Implementation of Sablib_GaussianFilter() function.
66//
67const SABLIB_DATA_PTR Sablib_GaussianFilter(const SABLIB_DATA_PTR y, const unsigned int n, const double sigma)
68{
69 std::vector<double> yy;
70 yy.assign(y->data, y->data + y->size);
71
72 auto result = sablib::GaussianFilter(yy, n, sigma);
73
74 SABLIB_DATA_PTR p = AllocSablibData(result.size());
75 std::copy(result.begin(), result.end(), p->data);
76
77 return p;
78}
79
80//
81// Implementation of Sablib_MovingMedian() function.
82//
83const SABLIB_DATA_PTR Sablib_MovingMedian(const SABLIB_DATA_PTR y, const unsigned int n)
84{
85 std::vector<double> yy;
86 yy.assign(y->data, y->data + y->size);
87
88 auto result = sablib::MovingMedian(yy, n);
89
90 SABLIB_DATA_PTR p = AllocSablibData(result.size());
91 std::copy(result.begin(), result.end(), p->data);
92
93 return p;
94}
95
96//
97// Implementation of Sablib_PSpline() function.
98//
99const SABLIB_DATA_PTR Sablib_PSpline(
100 const SABLIB_DATA_PTR y, const unsigned int knots_num,
101 const unsigned int degree, const unsigned int s, const double lambda
102)
103{
104 std::vector<double> yy;
105 yy.assign(y->data, y->data + y->size);
106
107 auto result = sablib::PSpline(yy, knots_num, degree, s, lambda);
108
109 SABLIB_DATA_PTR p = AllocSablibData(result.size());
110 std::copy(result.begin(), result.end(), p->data);
111
112 return p;
113}
114
115//
116// Implementation of Sablib_SavitzkyGolayCoefficients() function.
117//
119 const unsigned int n, const unsigned int polyorder, const unsigned derive, const double delta
120)
121{
122 auto result = sablib::SavitzkyGolayCoefficients(n, polyorder, derive, delta);
123
124 SABLIB_DATA_PTR p = AllocSablibData(result.size());
125 std::copy(result.begin(), result.end(), p->data);
126
127 return p;
128}
129
130//
131// Implementation of Sablib_SavitzkyGolay() function.
132//
133const SABLIB_DATA_PTR Sablib_SavitzkyGolay(
134 const SABLIB_DATA_PTR y,
135 const unsigned int n, const unsigned int polyorder, const unsigned derive, const double delta
136)
137{
138 std::vector<double> yy;
139 yy.assign(y->data, y->data + y->size);
140
141 auto result = sablib::SavitzkyGolay(yy, n, polyorder, derive, delta);
142
143 SABLIB_DATA_PTR p = AllocSablibData(result.size());
144 std::copy(result.begin(), result.end(), p->data);
145
146 return p;
147}
148
149//
150// Implementation of Sablib_WeightedWhittaker() function.
151//
152const SABLIB_DATA_PTR Sablib_WeightedWhittaker(
153 const SABLIB_DATA_PTR y, const SABLIB_DATA_PTR w,
154 const double lambda, const unsigned int s
155)
156{
157 std::vector<double> yy, ww;
158 yy.assign(y->data, y->data + y->size);
159 ww.assign(w->data, w->data + w->size);
160
161 auto result = sablib::Whittaker(yy, ww, lambda, s);
162
163 SABLIB_DATA_PTR p = AllocSablibData(result.size());
164 std::copy(result.begin(), result.end(), p->data);
165
166 return p;
167}
168
169//
170// Implementation of Sablib_Whittaker() function.
171//
172const SABLIB_DATA_PTR Sablib_Whittaker(
173 const SABLIB_DATA_PTR y, const double lambda, const unsigned int s
174)
175{
176 std::vector<double> yy;
177 yy.assign(y->data, y->data + y->size);
178
179 auto result = sablib::Whittaker(yy, lambda, s);
180
181 SABLIB_DATA_PTR p = AllocSablibData(result.size());
182 std::copy(result.begin(), result.end(), p->data);
183
184 return p;
185}
const SABLIB_DATA_PTR AllocSablibData(const size_t size)
Allocates a SABLIB_DATA structure.
Definition data.cpp:12
const std::vector< double > GaussianKernel(const unsigned int n, const double sigma)
Generates a Gaussian kernel.
const std::vector< double > MovingAverage(const std::vector< double > &y, const unsigned int n)
Calculates the simple moving average of the input signal (std::vector<double> version).
const std::vector< double > WeightedMovingAverage(const std::vector< double > &y, const std::vector< double > &w)
Calculates the weighted moving average of the input signal (std::vector<double> version).
Smoothing using simple/weighted moving average.
const std::vector< double > GaussianFilter(const std::vector< double > &y, const unsigned int n, const double sigma)
Performs Gaussian smoothing on the input signal.
const std::vector< double > MovingMedian(const std::vector< double > &y, const unsigned int n)
Performs moving median smoothing.
Smoothing using moving median.
const std::vector< double > PSpline(const std::vector< double > &y, const unsigned int knots_num, const unsigned int degree, const unsigned int s, const double lambda)
Smoothes the input data using P-Splines (Penalized B-Splines).
Definition pspline.cpp:17
Smoothing using penalized Spline(P-Spline).
const std::vector< double > SavitzkyGolay(const std::vector< double > &y, const unsigned int n, const unsigned int polyorder, const unsigned derive, const double delta)
Performs smoothing (and differentiation) using a Savitzky-Golay filter.
const std::vector< double > SavitzkyGolayCoefficients(const unsigned int n, const unsigned int polyorder, const unsigned derive, const double delta)
Calculates the coefficients for a Savitzky-Golay filter.
Smoothing using Savitzky-Golay filter.
const SABLIB_DATA_PTR Sablib_GaussianKernel(const unsigned int n, const double sigma)
Generates a Gaussian kernel.
Definition smoothing.cpp:54
const SABLIB_DATA_PTR Sablib_WeightedMovingAverage(const SABLIB_DATA_PTR y, const SABLIB_DATA_PTR w)
Calculates the weighted moving average of the input signal.
Definition smoothing.cpp:37
const SABLIB_DATA_PTR Sablib_MovingMedian(const SABLIB_DATA_PTR y, const unsigned int n)
Performs moving median smoothing.
Definition smoothing.cpp:83
const SABLIB_DATA_PTR Sablib_Whittaker(const SABLIB_DATA_PTR y, const double lambda, const unsigned int s)
Performs Whittaker smoothing (without weights).
const SABLIB_DATA_PTR Sablib_GaussianFilter(const SABLIB_DATA_PTR y, const unsigned int n, const double sigma)
Performs Gaussian smoothing on the input signal.
Definition smoothing.cpp:67
const SABLIB_DATA_PTR Sablib_MovingAverage(const SABLIB_DATA_PTR y, const unsigned int n)
Calculates the simple moving average of the input signal.
Definition smoothing.cpp:21
const SABLIB_DATA_PTR Sablib_SavitzkyGolayCoefficients(const unsigned int n, const unsigned int polyorder, const unsigned derive, const double delta)
Calculates the coefficients for a Savitzky-Golay filter.
const SABLIB_DATA_PTR Sablib_WeightedWhittaker(const SABLIB_DATA_PTR y, const SABLIB_DATA_PTR w, const double lambda, const unsigned int s)
Performs Whittaker smoothing (with weights).
const SABLIB_DATA_PTR Sablib_PSpline(const SABLIB_DATA_PTR y, const unsigned int knots_num, const unsigned int degree, const unsigned int s, const double lambda)
Smoothes the input data using P-Splines (Penalized B-Splines).
Definition smoothing.cpp:99
const SABLIB_DATA_PTR Sablib_SavitzkyGolay(const SABLIB_DATA_PTR y, const unsigned int n, const unsigned int polyorder, const unsigned derive, const double delta)
Performs smoothing (and differentiation) using a Savitzky-Golay filter.
The C interface of sablib smoothing functions.
size_t size
Definition data.h:21
double * data
Definition data.h:22
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
Smoothing using Whittaker smoother.