22 inline double GramPoly(
const int i,
const int m,
const int k,
const int s)
26 return (4. * k - 2.) / (k * (2. * m - k + 1.)) * (i * GramPoly(i, m, k - 1, s) + s * GramPoly(i, m, k - 1, s - 1))
27 - ((k - 1.) * (2. * m + k)) / (k * (2. * m - k + 1.)) * GramPoly(i, m, k - 2, s);
41 inline double GenFact(
const int a,
const int b)
45 for (
int j = (a - b) + 1; j <= a; j++)
56 inline double Weight(
const int i,
const int t,
const int m,
const int n,
const int s)
59 for (
int k = 0; k <= n; ++k)
62 + (2 * k + 1) * (GenFact(2 * m, k) / GenFact(2 * m + k + 1, k + 1)) * GramPoly(i, m, k, 0)
63 * GramPoly(t, m, k, s);
72 inline std::vector<double> ComputeWeights(
const int m,
const int t,
const int n,
const int s)
74 std::vector<double> weights(2 *
static_cast<size_t>(m) + 1);
75 for (
int i = 0; i < 2 * m + 1; ++i)
77 weights[
static_cast<size_t>(i)] = Weight(i - m, t, m, n, s);
165 os <<
"m : " << conf.
m << std::endl
166 <<
"Window Size (2*m+1) : " << 2 * conf.
m + 1 << std::endl
167 <<
"n (Order) :" << conf.
n << std::endl
168 <<
"s (Differentiate) : " << conf.
s << std::endl
169 <<
"t: Filter point ([-m,m]): " << conf.
t << std::endl;
176 SavitzkyGolayFilter(
unsigned m,
int t,
unsigned n,
unsigned s,
double dt = 1.) : conf_(m, t, n, s, dt)
210 template<
typename ContainerT>
211 typename ContainerT::value_type
filter(
const ContainerT& v)
const
213 assert(v.size() == weights_.size() && v.size() > 0);
214 using T =
typename ContainerT::value_type;
215 T res = weights_[0] * v[0];
216 for (
size_t i = 1; i < v.size(); ++i)
218 res += weights_[i] * v[i];
223 std::vector<double> weights()
const
228 SavitzkyGolayFilterConfig config()
const
234 SavitzkyGolayFilterConfig conf_;
235 std::vector<double> weights_;
240 weights_ = ComputeWeights(
static_cast<int>(conf_.m), conf_.t,
static_cast<int>(conf_.n),
static_cast<int>(conf_.s));
241 dt_ = std::pow(conf_.time_step(), conf_.derivation_order());
unsigned m
Window size is 2*m+1.
Definition: savgol.h:85
int t
Time at which the filter is applied.
Definition: savgol.h:88
unsigned window_size() const
Full size of the filter's window 2*m+1
Definition: savgol.h:150
int data_point() const
Time at which the filter is evaluated.
Definition: savgol.h:126
unsigned order() const
Polynomial order.
Definition: savgol.h:142
unsigned n
Polynomial order.
Definition: savgol.h:90
unsigned s
Derivation order (0 for no derivation)
Definition: savgol.h:92
unsigned derivation_order() const
Derivation order.
Definition: savgol.h:134
SavitzkyGolayFilterConfig(unsigned m, int t, unsigned n, unsigned s, double dt=1.)
Construct a filter with the specified configuration.
Definition: savgol.h:119
SavitzkyGolayFilterConfig()
Construct a filter with default weights.
Definition: savgol.h:99
double time_step() const
Time step.
Definition: savgol.h:158
double dt
Time step.
Definition: savgol.h:94
ContainerT::value_type filter(const ContainerT &v) const
Apply Savitzky-Golay convolution to the data x should have size 2*m+1 As the function only applies a ...
Definition: savgol.h:211