Skip to content
This repository has been archived by the owner on Jun 26, 2024. It is now read-only.

Commit

Permalink
Throw an exception if buffer size and convolution weigths do not match
Browse files Browse the repository at this point in the history
  • Loading branch information
arntanguy committed Jun 26, 2024
1 parent 3962c60 commit 00cb1ca
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion include/gram_savitzky_golay/gram_savitzky_golay.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ struct GRAM_SAVITZKY_GOLAY_DLLAPI SavitzkyGolayFilter
template<typename ContainerT>
typename ContainerT::value_type filter(const ContainerT & v) const
{
assert(v.size() == weights_.size() && v.size() > 0);
if(v.size() != weights_.size() || v.size() < 1)
{
throw std::runtime_error("The size of v (" + std::to_string(v.size()) + ") and the convolution weights ("
+ std::to_string(weights_.size())
+ ") must be the same. Make sure that 2*m+1 = v.size()");
}
using T = typename ContainerT::value_type;
T res = weights_[0] * v[0];
for(size_t i = 1; i < v.size(); ++i)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_gram_savitzky_golay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,25 @@ BOOST_AUTO_TEST_CASE(TestPolynomialDerivative)
BOOST_REQUIRE_CLOSE(result_order1, expected_result_order1, 10e-8);
BOOST_REQUIRE_CLOSE(result_order2, expected_result_order2, 10e-8);
}

// Test derivation on a known polynomial function
BOOST_AUTO_TEST_CASE(TestWrongWindowSize)
{
auto vec = std::vector<double>(300, 0);
BOOST_REQUIRE_EQUAL(vec.size(), 300);

// Window size is 2*m+1
const size_t m = 151;
// Polynomial Order
const size_t n = 6;
// Initial Point Smoothing (ie evaluate polynomial at first point in the window)
// Points are defined in range [-m;m]
const size_t t = m;
// Derivation order? 0: no derivation, 1: first derivative, 2: second derivative...
const int d = 0;

// Real-time filter (filtering at latest data point)
gram_sg::SavitzkyGolayFilter filter(m, t, n, d);
// Filter some data
BOOST_REQUIRE_THROW(filter.filter(vec), std::runtime_error);
}

0 comments on commit 00cb1ca

Please sign in to comment.