19 const unsigned int n,
const unsigned int polyorder,
const unsigned derive,
const double delta
22 unsigned int window = 2 * n + 1;
26 throw std::invalid_argument(
"SavitzkyGolayCoefficients(): n is zero.");
30 throw std::invalid_argument(
"SavitzkyGolayCoefficients(): polyorder is zero.");
33 if(window <= polyorder) {
34 throw std::invalid_argument(
"SavitzkyGolayCoefficients(): window size (2 * n + 1) must be greater than polyorder.");
37 if(window <= derive) {
38 throw std::invalid_argument(
"SavitzkyGolayCoefficients(): window size (2 * n + 1) must be greater than derive.");
41 if(polyorder <= derive) {
42 throw std::invalid_argument(
"SavitzkyGolayCoefficients(): polyorder must be greater than derive.");
45 Eigen::VectorXd v = Eigen::VectorXd::LinSpaced(window, (
int)-n, n);
46 Eigen::MatrixXd x = Eigen::MatrixXd::Ones(window, polyorder + 1);
48 for(
unsigned int i = 1; i <= polyorder; i++){
49 x.col(i) = (x.col(i - 1).array() * v.array()).matrix();
52 Eigen::MatrixXd coeff_mat = (x.transpose() * x).inverse() * x.transpose();
55 for(
unsigned int i = 1; i <= derive; i++){
58 p /= std::pow(delta, derive);
61 Eigen::VectorXd coeff = coeff_mat.row(derive) * p;
62 std::vector<double> result(coeff.size());
64 Eigen::VectorXd::Map(result.data(), result.size()) = coeff;
73 const std::vector<double> & y,
74 const unsigned int n,
const unsigned int polyorder,
const unsigned derive,
const double delta
78 throw std::invalid_argument(
"SavitzkyGolay(): the length of y is zero.");
82 Eigen::VectorXd w = Eigen::VectorXd::Map(coeff.data(), coeff.size());
83 Eigen::VectorXd yy = Eigen::VectorXd::Map(y.data(), y.size());
87 std::vector<double> result(z.size());
89 Eigen::VectorXd::Map(result.data(), result.size()) = z;
Python's NumPy-like convolve() function.
const Derived1::PlainObject Convolve(const Eigen::MatrixBase< Derived1 > &v1, const Eigen::MatrixBase< Derived2 > &v2, const ConvolveMode mode=ConvolveMode::Full)
Returns the discrete, linear convolution of two one-dimensional sequences.
const std::vector< double > SavitzkyGolay(const std::vector< double > &y, const unsigned int n, const unsigned int polyorder, const unsigned derive, const double delta)
Performs smoothing (and differentiation) using a Savitzky-Golay filter.
const std::vector< double > SavitzkyGolayCoefficients(const unsigned int n, const unsigned int polyorder, const unsigned derive, const double delta)
Calculates the coefficients for a Savitzky-Golay filter.
Smoothing using Savitzky-Golay filter.