7#ifndef __SABLIB_MOVING_AVERAGE_H__
8#define __SABLIB_MOVING_AVERAGE_H__
25const std::vector<double>
WeightedMovingAverage(
const std::vector<double> & y,
const std::vector<double> & w);
35template <
typename Derived>
37 const Eigen::MatrixBase<Derived> & y,
const Eigen::MatrixBase<Derived> & w
42 static_assert(Derived::IsVectorAtCompileTime,
"Error: y and w are not vector.");
44 if(y.size() == 0 || w.size() == 0) {
45 throw std::invalid_argument(
"WeightedMovingAverage(): the length of y or w is zero.");
48 using PlainObject =
typename Derived::PlainObject;
50 int points = w.size();
53 PlainObject result = PlainObject::Zero(y.size());
56 for(
int i = 0; i < y.size(); i++) {
57 result(i) = (yy.block(i, 0, points, 1).array() * w.array()).matrix().sum();
60 else if(y.rows() == 1) {
61 for(
int i = 0; i < y.size(); i++) {
62 result(i) = (yy.block(0, i, 1, points).array() * w.array()).matrix().sum();
76const std::vector<double> MovingAverage(
const std::vector<double> & y,
const unsigned int n);
86template <
typename Derived>
87const typename Derived::PlainObject
MovingAverage(
const Eigen::MatrixBase<Derived> & y,
const unsigned int n)
89 using PlainObject =
typename Derived::PlainObject;
92 throw std::invalid_argument(
"MovingAverage(): n is zero.");
95 int points = 2 * n + 1;
96 PlainObject w = PlainObject::Ones(points) / points;
109const std::vector<double> GaussianKernel(
const unsigned int n,
const double sigma);
122inline const std::vector<double>
GaussianFilter(
const std::vector<double> & y,
const unsigned int n,
const double sigma)
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.
const std::vector< double > GaussianKernel(const unsigned int n, const double sigma)
Generates a Gaussian kernel.
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 > 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 > GaussianFilter(const std::vector< double > &y, const unsigned int n, const double sigma)
Performs Gaussian smoothing on the input signal.