sablib
Loading...
Searching...
No Matches
moving_average.h
Go to the documentation of this file.
1
6
7#ifndef __SABLIB_MOVING_AVERAGE_H__
8#define __SABLIB_MOVING_AVERAGE_H__
9
10#include <cmath>
11#include <stdexcept>
12#include <vector>
13
14#include "../misc/expand.h"
15#include "sablib_export.h"
16
17namespace sablib {
18
26SABLIB_EXPORT const std::vector<double> WeightedMovingAverage(const std::vector<double> & y, const std::vector<double> & w);
27
36template <typename Derived>
37const typename Derived::PlainObject WeightedMovingAverage(
38 const Eigen::MatrixBase<Derived> & y, const Eigen::MatrixBase<Derived> & w
39)
40{
41 // Although parameters are received as MatrixBase<Derived>, only vector classes are allowed.
42 // Others will be rejected at compile time.
43 static_assert(Derived::IsVectorAtCompileTime, "Error: y and w are not vector.");
44
45 if(y.size() == 0 || w.size() == 0) {
46 throw std::invalid_argument("WeightedMovingAverage(): the length of y or w is zero.");
47 }
48
49 using PlainObject = typename Derived::PlainObject;
50
51 int points = w.size();
52 int n = points / 2;
53 PlainObject yy = ExpandBoundaries(y, n);
54 PlainObject result = PlainObject::Zero(y.size());
55
56 if(y.cols() == 1) {
57 for(int i = 0; i < y.size(); i++) {
58 result(i) = (yy.block(i, 0, points, 1).array() * w.array()).matrix().sum();
59 }
60 }
61 else if(y.rows() == 1) {
62 for(int i = 0; i < y.size(); i++) {
63 result(i) = (yy.block(0, i, 1, points).array() * w.array()).matrix().sum();
64 }
65 }
66
67 return result;
68}
69
77SABLIB_EXPORT const std::vector<double> MovingAverage(const std::vector<double> & y, const unsigned int n);
78
87template <typename Derived>
88const typename Derived::PlainObject MovingAverage(const Eigen::MatrixBase<Derived> & y, const unsigned int n)
89{
90 using PlainObject = typename Derived::PlainObject;
91
92 if(n == 0) {
93 throw std::invalid_argument("MovingAverage(): n is zero.");
94 }
95
96 int points = 2 * n + 1;
97 PlainObject w = PlainObject::Ones(points) / points;
98
99 return WeightedMovingAverage(y, w);
100}
101
110SABLIB_EXPORT const std::vector<double> GaussianKernel(const unsigned int n, const double sigma);
111
123inline const std::vector<double> GaussianFilter(const std::vector<double> & y, const unsigned int n, const double sigma)
124{
125 return WeightedMovingAverage(y, GaussianKernel(n, sigma));
126}
127
128}; // namespace sablib
129
130#endif // __SABLIB_MOVING_AVERAGE_H__
Expands and Trim the boundaries of the data.
const Derived::PlainObject ExpandBoundaries(const Eigen::MatrixBase< Derived > &y, const unsigned int n)
Expands the boundaries of a vector by padding with the first and last elements.
Definition expand.h:23
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).
const std::vector< double > GaussianFilter(const std::vector< double > &y, const unsigned int n, const double sigma)
Performs Gaussian smoothing on the input signal.