Falcon
Loading...
Searching...
No Matches
gram_savitzky_golay.hpp
1// ---------------------------------------------------------------------
2// BSD 2-Clause License
3//
4// Copyright (c) 2012-2019, CNRS-UM LIRMM, CNRS-AIST JRL
5// All rights reserved.
6// reference: https://github.com/arntanguy/gram_savitzky_golay
7//
8// Modified in 2021 by Neuro-Electronics Research Flanders
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are met:
12//
13// 1. Redistributions of source code must retain the above copyright notice,
14// this
15// list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright notice,
18// this list of conditions and the following disclaimer in the documentation
19// and/or other materials provided with the distribution.
20//
21// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31// POSSIBILITY OF SUCH DAMAGE.
32// ---------------------------------------------------------------------
33
34#pragma once
35
36#include <cassert>
37#include <cmath>
38#include <vector>
39
40namespace gram_sg {
41
42inline double compute_gram_polynomial(const int i, const int m, const int k, const int s) {
43 if (k > 0) {
44 return (4. * k - 2.) / (k * (2. * m - k + 1.)) *
45 (i * compute_gram_polynomial(i, m, k - 1, s) +
46 s * compute_gram_polynomial(i, m, k - 1, s - 1)) -
47 ((k - 1.) * (2. * m + k)) / (k * (2. * m - k + 1.)) *
48 compute_gram_polynomial(i, m, k - 2, s);
49 }
50
51 if (k == 0 && s == 0) {
52 return 1.;
53 } else {
54 return 0.;
55 }
56};
57
58inline double compute_generalized_factorial(const int a, const int b) {
59 double gf = 1.;
60
61 for (int j = (a - b) + 1; j <= a; j++) {
62 gf *= j;
63 }
64 return gf;
65};
66
67inline double compute_weight(const int i, const int t, const int m, const int n, const int s) {
68 double w = 0;
69 for (int k = 0; k <= n; ++k) {
70 w = w + (2 * k + 1) *
71 (compute_generalized_factorial(2 * m, k) /
72 compute_generalized_factorial(2 * m + k + 1, k + 1)) *
73 compute_gram_polynomial(i, m, k, 0) * compute_gram_polynomial(t, m, k, s);
74 }
75 return w;
76};
77
78inline std::vector<double> compute_weights(const int m, const int t, const int n, const int s) {
79 std::vector<double> weights(2 * static_cast<size_t>(m) + 1);
80
81 for (int i = 0; i < 2 * m + 1; ++i) {
82 weights[static_cast<size_t>(i)] = compute_weight(i - m, t, m, n, s);
83 }
84 return weights;
85};
86} // namespace gram_sg