7#ifndef __SABLIB_MOVING_AVERAGE_H__
8#define __SABLIB_MOVING_AVERAGE_H__
15#include "sablib_export.h"
26SABLIB_EXPORT
const std::vector<double>
WeightedMovingAverage(
const std::vector<double> & y,
const std::vector<double> & w);
36template <
typename Derived>
38 const Eigen::MatrixBase<Derived> & y,
const Eigen::MatrixBase<Derived> & w
43 static_assert(Derived::IsVectorAtCompileTime,
"Error: y and w are not vector.");
45 if(y.size() == 0 || w.size() == 0) {
46 throw std::invalid_argument(
"WeightedMovingAverage(): the length of y or w is zero.");
49 using PlainObject =
typename Derived::PlainObject;
51 int points = w.size();
54 PlainObject result = PlainObject::Zero(y.size());
57 for(
int i = 0; i < y.size(); i++) {
58 result(i) = (yy.block(i, 0, points, 1).array() * w.array()).matrix().sum();
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();
77SABLIB_EXPORT
const std::vector<double> MovingAverage(
const std::vector<double> & y,
const unsigned int n);
87template <
typename Derived>
88const typename Derived::PlainObject
MovingAverage(
const Eigen::MatrixBase<Derived> & y,
const unsigned int n)
90 using PlainObject =
typename Derived::PlainObject;
93 throw std::invalid_argument(
"MovingAverage(): n is zero.");
96 int points = 2 * n + 1;
97 PlainObject w = PlainObject::Ones(points) / points;
110SABLIB_EXPORT
const std::vector<double> GaussianKernel(
const unsigned int n,
const double sigma);
123inline 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 > 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.