From d55ee89a7d680674467139ce86f68ab71be2c42e Mon Sep 17 00:00:00 2001 From: Vincent Samy Date: Mon, 17 Dec 2018 14:48:44 +0900 Subject: [PATCH] Compilation pass. --- include/BilinearTransform.h | 16 +++---- include/Butterworth.h | 15 +++--- include/Butterworth.tpp | 64 ++++++++++++++------------ include/DigitalFilter.h | 3 -- include/GenericFilter.h | 14 ++++-- include/GenericFilter.tpp | 11 ++++- include/MovingAverage.h | 14 ++++-- include/fratio | 35 ++++++++++++++ include/fratio.h | 32 ------------- include/polynome_functions.h | 12 ++--- include/typedefs.h | 2 + tests/ButterworthFilterTests.cpp | 74 +++++++++++++++--------------- tests/CMakeLists.txt | 8 ++-- tests/DigitalFilterTests.cpp | 7 ++- tests/GenericFilterTests.cpp | 3 +- tests/MovingAverageFilterTests.cpp | 4 +- tests/polynome_functions_tests.cpp | 46 ++++++++++--------- 17 files changed, 194 insertions(+), 166 deletions(-) create mode 100644 include/fratio delete mode 100644 include/fratio.h diff --git a/include/BilinearTransform.h b/include/BilinearTransform.h index 5ba8477..e7747cd 100644 --- a/include/BilinearTransform.h +++ b/include/BilinearTransform.h @@ -11,9 +11,9 @@ struct BilinearTransform { static_assert(std::is_floating_point::value, "This struct can only accept floating point types (real and complex)."); static void SToZ(SubType fs, const T& sPlanePole, T& zPlanePole); - static void SToZ(SubType fs, const Eigen::VectorX& sPlanePoles, Eigen::VectorX& zPlanePoles); // Can be optimized + static void SToZ(SubType fs, const Eigen::VectorX& sPlanePoles, Eigen::Ref>& zPlanePoles); // Can be optimized static void ZToS(SubType fs, const T& zPlanePole, T& sPlanePole); - static void ZToS(SubType fs, const Eigen::VectorX& zPlanePoles, Eigen::VectorX& sPlanePoles); // Can be optimized + static void ZToS(SubType fs, const Eigen::VectorX& zPlanePoles, Eigen::Ref>& sPlanePoles); // Can be optimized }; template @@ -24,11 +24,11 @@ void BilinearTransform::SToZ(SubType fs, const T& sPlanePole, T& zPlanePole) } template -void BilinearTransform::SToZ(SubType fs, const Eigen::VectorX& sPlanePoles, Eigen::VectorX& zPlanePoles) +void BilinearTransform::SToZ(SubType fs, const Eigen::VectorX& sPlanePoles, Eigen::Ref>& zPlanePoles) { assert(sPlanePoles.size() == zPlanePoles.size()); - for (size_t k = 0; k < sPlanePoles.size(); ++k) - SToZ(fs, sPlanePoles[k], zPlanePoles[k]); + for (Eigen::Index k = 0; k < sPlanePoles.size(); ++k) + SToZ(fs, sPlanePoles(k), zPlanePoles(k)); } template @@ -39,11 +39,11 @@ void BilinearTransform::ZToS(SubType fs, const T& zPlanePole, T& sPlanePole) } template -void BilinearTransform::ZToS(SubType fs, const Eigen::VectorX& zPlanePoles, Eigen::VectorX& sPlanePoles) +void BilinearTransform::ZToS(SubType fs, const Eigen::VectorX& zPlanePoles, Eigen::Ref>& sPlanePoles) { assert(zPlanePoles.size() == sPlanePoles.size()); - for (size_t k = 0; k < sPlanePoles.size(); ++k) - ZToS(fs, zPlanePoles[k], sPlanePoles[k]); + for (Eigen::Index k = 0; k < sPlanePoles.size(); ++k) + ZToS(fs, zPlanePoles(k), sPlanePoles(k)); } } // namespace fratio \ No newline at end of file diff --git a/include/Butterworth.h b/include/Butterworth.h index d7bedf9..04e50bd 100644 --- a/include/Butterworth.h +++ b/include/Butterworth.h @@ -1,6 +1,6 @@ #pragma once -#include "GenericFilter.h" +#include "DigitalFilter.h" #include "typedefs.h" #include #include @@ -23,21 +23,20 @@ public: // static double minimumRequiredFrequency(...); public: Butterworth(Type type = Type::LowPass); - Butterworth(size_t order, T fc, T fs, Type type = Type::LowPass); + Butterworth(int order, T fc, T fs, Type type = Type::LowPass); - void setFilterParameters(size_t order, T fc, T fs); + void setFilterParameters(int order, T fc, T fs); private: - void initialize(size_t order, T fc, T fs); + void initialize(int order, T fc, T fs); void computeDigitalRep(); - void updateCoeffSize(); - std::complex generateAnalogPole(T fpw, size_t k); + std::complex generateAnalogPole(T fpw, int k); Eigen::VectorX> generateAnalogZeros(); - void scaleAmplitude(); + void scaleAmplitude(Eigen::Ref> aCoeff, Eigen::Ref> bCoeff); private: Type m_type; - size_t m_order; + int m_order; T m_fc; T m_fs; Eigen::VectorX> m_poles; diff --git a/include/Butterworth.tpp b/include/Butterworth.tpp index c374189..f442d1f 100644 --- a/include/Butterworth.tpp +++ b/include/Butterworth.tpp @@ -10,21 +10,31 @@ Butterworth::Butterworth(Type type) } template -Butterworth::Butterworth(size_t order, T fc, T fs, Type type) +Butterworth::Butterworth(int order, T fc, T fs, Type type) : m_type(type) { initialize(order, fc, fs); } template -void Butterworth::setFilterParameters(size_t order, T fc, T fs) +void Butterworth::setFilterParameters(int order, T fc, T fs) { initialize(order, fc, fs); } template -void Butterworth::initialize(size_t order, T fc, T fs) +void Butterworth::initialize(int order, T fc, T fs) { + if (order <= 0) { + m_status = FilterStatus::BAD_ORDER_SIZE; + return; + } + + if (fc <= 0 || fs <= 0) { + m_status = FilterStatus::BAD_FREQUENCY_VALUE; + return; + } + if (m_fc > m_fs / 2.) { m_status = FilterStatus::BAD_CUTOFF_FREQUENCY; return; @@ -33,8 +43,8 @@ void Butterworth::initialize(size_t order, T fc, T fs) m_order = order; m_fc = fc; m_fs = fs; - updateCoeffSize(); computeDigitalRep(); + resetFilter(); } template @@ -46,37 +56,31 @@ void Butterworth::computeDigitalRep() // Compute poles std::complex analogPole; Eigen::VectorX> poles(m_order); - for (size_t k = 1; k <= m_order; ++k) { + for (int k = 1; k <= m_order; ++k) { analogPole = generateAnalogPole(fpw, k); - BilinearTransform>::SToZ(m_fs, analogPole, poles[k - 1]); + BilinearTransform>::SToZ(m_fs, analogPole, poles(k - 1)); } Eigen::VectorX> zeros = generateAnalogZeros(); Eigen::VectorX> a = VietaAlgo>::polyCoeffFromRoot(poles); Eigen::VectorX> b = VietaAlgo>::polyCoeffFromRoot(zeros); - for (size_t i = 0; i < m_order + 1; ++i) { - m_aCoeff[i] = a[i].real(); - m_bCoeff[i] = b[i].real(); + Eigen::VectorX aCoeff(m_order + 1); + Eigen::VectorX bCoeff(m_order + 1); + for (int i = 0; i < m_order + 1; ++i) { + aCoeff(i) = a(i).real(); + bCoeff(i) = b(i).real(); } - scaleAmplitude(); - checkCoeff(m_aCoeff, m_bCoeff); + scaleAmplitude(aCoeff, bCoeff); + setCoeffs(std::move(aCoeff), std::move(bCoeff)); } template -void Butterworth::updateCoeffSize() -{ - m_aCoeff.resize(m_order + 1); - m_bCoeff.resize(m_order + 1); - resetFilter(); -} - -template -std::complex Butterworth::generateAnalogPole(T fpw, size_t k) +std::complex Butterworth::generateAnalogPole(T fpw, int k) { T scaleFactor = 2 * PI * fpw; - auto thetaK = [pi = PI, order = m_order](size_t k) -> T { + auto thetaK = [pi = PI, order = m_order](int k) -> T { return (2 * k - 1) * pi / (2 * order); }; @@ -105,32 +109,32 @@ Eigen::VectorX> Butterworth::generateAnalogZeros() } template -void Butterworth::scaleAmplitude() +void Butterworth::scaleAmplitude(Eigen::Ref> aCoeff, Eigen::Ref> bCoeff) { T scale = 0; T sumB = 0; switch (m_type) { case Type::HighPass: - for (size_t i = 0; i < m_order + 1; ++i) { + for (int i = 0; i < m_order + 1; ++i) { if (i % 2 == 0) { - scale += m_aCoeff(i); - sumB += m_bCoeff(i); + scale += aCoeff(i); + sumB += bCoeff(i); } else { - scale -= m_aCoeff(i); - sumB -= m_bCoeff(i); + scale -= aCoeff(i); + sumB -= bCoeff(i); } } break; case Type::LowPass: default: - scale = m_aCoeff.sum(); - sumB = m_bCoeff.sum(); + scale = aCoeff.sum(); + sumB = bCoeff.sum(); break; } - m_bCoeff *= scale / sumB; + bCoeff *= scale / sumB; } } // namespace fratio \ No newline at end of file diff --git a/include/DigitalFilter.h b/include/DigitalFilter.h index 2c4606c..2a57517 100644 --- a/include/DigitalFilter.h +++ b/include/DigitalFilter.h @@ -13,9 +13,6 @@ public: : GenericFilter(aCoeff, bCoeff) { } - - size_t aOrder() const noexcept { return m_aCoeff.size(); } - size_t bOrder() const noexcept { return m_bCoeff.size(); } }; } // namespace fratio \ No newline at end of file diff --git a/include/GenericFilter.h b/include/GenericFilter.h index 90919a4..bcf6721 100644 --- a/include/GenericFilter.h +++ b/include/GenericFilter.h @@ -16,14 +16,18 @@ public: public: // Careful: Only an assert check for the filter status - T stepFilter(const T& data); + T stepFilter(const T& data); Eigen::VectorX filter(const Eigen::VectorX& data); bool getFilterResults(Eigen::Ref> results, const Eigen::VectorX& data); void resetFilter(); - bool setCoeffs(const Eigen::VectorX& aCoeff, const Eigen::VectorX& bCoeff); - void getCoeffs(Eigen::Ref> aCoeff, Eigen::Ref> bCoeff) const; + template + bool setCoeffs(T2&& aCoeff, T2&& bCoeff); + + void getCoeffs(Eigen::VectorX& aCoeff, Eigen::VectorX& bCoeff) const; FilterStatus status() const noexcept { return m_status; } + Eigen::Index aOrder() const noexcept { return m_aCoeff.size(); } + Eigen::Index bOrder() const noexcept { return m_bCoeff.size(); } protected: GenericFilter() = default; @@ -35,10 +39,10 @@ protected: protected: FilterStatus m_status; - Eigen::VectorX m_aCoeff; - Eigen::VectorX m_bCoeff; private: + Eigen::VectorX m_aCoeff; + Eigen::VectorX m_bCoeff; Eigen::VectorX m_filteredData; Eigen::VectorX m_rawData; }; diff --git a/include/GenericFilter.tpp b/include/GenericFilter.tpp index da10af7..1f827f4 100644 --- a/include/GenericFilter.tpp +++ b/include/GenericFilter.tpp @@ -11,12 +11,16 @@ std::string GenericFilter::filterStatus(FilterStatus status) return "Filter is uninitialized"; case FilterStatus::READY: return "Filter is ready to be used"; + case FilterStatus::BAD_ORDER_SIZE: + return "You try to initialize the filter with an order inferior or equal to 0 (window size for the moving average)"; case FilterStatus::ALL_COEFF_MISSING: return "Filter has none of its coefficient initialized"; case FilterStatus::A_COEFF_MISSING: return "Filter has its 'a' coefficients uninitialized"; case FilterStatus::A_COEFF_MISSING: return "Filter has its 'b' coefficients uninitialized"; + case FilterStatus::BAD_FREQUENCY_VALUE: + return "Filter has a received a frequency that is negative or equal to zero"; case FilterStatus::BAD_CUTOFF_FREQUENCY: return "Filter has a received a bad cut-off frequency. It must be inferior to the sampling frequency"; default: @@ -71,8 +75,11 @@ void GenericFilter::resetFilter() } template -bool GenericFilter::setCoeffs(const Eigen::VectorX& aCoeff, const Eigen::VectorX& bCoeff) +template +bool GenericFilter::setCoeffs(T2&& aCoeff, T2&& bCoeff) { + static_assert(std::is_same_v>, "The coefficents should be of type Eigen::VectorX"); + if (!checkCoeffs(aCoeff, bCoeff)) return false; @@ -84,7 +91,7 @@ bool GenericFilter::setCoeffs(const Eigen::VectorX& aCoeff, const Eigen::V } template -void GenericFilter::getCoeffs(Eigen::Ref> aCoeff, Eigen::Ref> bCoeff) const +void GenericFilter::getCoeffs(Eigen::VectorX& aCoeff, Eigen::VectorX& bCoeff) const { aCoeff = m_aCoeff; bCoeff = m_bCoeff; diff --git a/include/MovingAverage.h b/include/MovingAverage.h index b904bd7..58dc022 100644 --- a/include/MovingAverage.h +++ b/include/MovingAverage.h @@ -9,13 +9,21 @@ template class MovingAverage : public DigitalFilter { public: MovingAverage() = default; - MovingAverage(size_t windowSize) + MovingAverage(int windowSize) : DigitalFilter(Eigen::VectorX::Constant(1, T(1)), Eigen::VectorX::Constant(windowSize, T(1) / windowSize)) { } - void setWindowSize(size_t windowSize) { setCoeffs(Eigen::VectorX::Constant(1, T(1)), Eigen::VectorX::Constant(windowSize, T(1) / windowSize)); } - size_t windowSize() const noexcept { return bOrder(); } + void setWindowSize(int windowSize) + { + if (windowSize <= 0) { + m_status = FilterStatus::BAD_ORDER_SIZE; + return; + } + + setCoeffs(Eigen::VectorX::Constant(1, T(1)), Eigen::VectorX::Constant(windowSize, T(1) / windowSize)); + } + int windowSize() const noexcept { return bOrder(); } }; } // namespace fratio diff --git a/include/fratio b/include/fratio new file mode 100644 index 0000000..8d6d667 --- /dev/null +++ b/include/fratio @@ -0,0 +1,35 @@ +#pragma once + +// #include "BilinearTransform.h" +#include "Butterworth.h" +#include "DigitalFilter.h" +#include "GenericFilter.h" +#include "MovingAverage.h" +#include "polynome_functions.h" +#include "typedefs.h" + +namespace fratio { + +// Filters +using DigitalFilterf = DigitalFilter; +using DigitalFilterd = DigitalFilter; +using MovingAveragef = MovingAverage; +using MovingAveraged = MovingAverage; +using Butterworthf = Butterworth; +using Butterworthd = Butterworth; + +// Polynome helper functions +using VietaAlgof = VietaAlgo; +using VietaAlgod = VietaAlgo; +using VietaAlgoi = VietaAlgo; +using VietaAlgocf = VietaAlgo>; +using VietaAlgocd = VietaAlgo>; +using VietaAlgoci = VietaAlgo>; + +// Bilinear transformation functions +using BilinearTransformf = BilinearTransform; +using BilinearTransformd = BilinearTransform; +using BilinearTransformcf = BilinearTransform>; +using BilinearTransformcd = BilinearTransform>; + +} // namespace fratio \ No newline at end of file diff --git a/include/fratio.h b/include/fratio.h deleted file mode 100644 index 5e92649..0000000 --- a/include/fratio.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#include "Butterworth.h" -#include "DigitalFilter.h" -#include "MovingAverage.h" -#include "polynome_functions.h" - -namespace fratio { - -// Filters -using DigitalFilterf = DigitalFilter; -using DigitalFilterd = DigitalFilter; -using MovingAveragef = MovingAverage; -using MovingAveraged = MovingAverage; -using Butterworthf = Butterworth; -using Butterworthd = Butterworth; - -// // Polynome helper functions -// using VietaAlgof = VietaAlgo; -// using VietaAlgod = VietaAlgo; -// using VietaAlgoi = VietaAlgo; -// using VietaAlgocf = VietaAlgo>; -// using VietaAlgocd = VietaAlgo>; -// using VietaAlgoci = VietaAlgo>; - -// // Bilinear transformation functions -// using BilinearTransformf = BilinearTransform; -// using BilinearTransformd = BilinearTransform; -// using BilinearTransformcf = BilinearTransform>; -// using BilinearTransformcd = BilinearTransform>; - -} // namespace fratio \ No newline at end of file diff --git a/include/polynome_functions.h b/include/polynome_functions.h index b19b37c..e087e4c 100644 --- a/include/polynome_functions.h +++ b/include/polynome_functions.h @@ -19,22 +19,22 @@ Eigen::VectorX VietaAlgo::polyCoeffFromRoot(const Eigen::VectorX& poles { Eigen::VectorX coeffs = Eigen::VectorX::Zero(poles.size() + 1); coeffs(0) = T(1); - for (size_t i = 0; i < poles.size(); ++i) { - for (size_t k = i + 1; k > 0; --k) { + for (Eigen::Index i = 0; i < poles.size(); ++i) { + for (Eigen::Index k = i + 1; k > 0; --k) { coeffs(k) -= poles(i) * coeffs(k - 1); } } // Check for equation c(k) = sum(i=k-1, poles.size() : p(i)) * c(k-1), k>=1 - // size_t pSize = poles.size(); - // for (size_t k = 1; k < coeffs.size(); ++k) + // Eigen::Index pSize = poles.size(); + // for (Eigen::Index k = 1; k < coeffs.size(); ++k) // coeffs(k) -= poles.tail(pSize - (k - 1)).sum() * coeffs(k - 1); // Maybe better // T sum = poles.sum(); - // for (size_t k = 1; k < coeffs.size(); ++k) { + // for (Eigen::Index k = 1; k < coeffs.size(); ++k) { // coeffs(k) -= sum * coeffs(k - 1); // sum -= poles(k - 1); - // } + // } return coeffs; } diff --git a/include/typedefs.h b/include/typedefs.h index 4e3ddf5..0f5f56e 100644 --- a/include/typedefs.h +++ b/include/typedefs.h @@ -15,12 +15,14 @@ enum class FilterStatus { // Generic filter NONE, READY, + BAD_ORDER_SIZE, BAD_A_COEFF, A_COEFF_MISSING, B_COEFF_MISSING, ALL_COEFF_MISSING = A_COEFF_MISSING | B_COEFF_MISSING, // Butterworth filter + BAD_FREQUENCY_VALUE, BAD_CUTOFF_FREQUENCY }; diff --git a/tests/ButterworthFilterTests.cpp b/tests/ButterworthFilterTests.cpp index c141238..cf5f36a 100644 --- a/tests/ButterworthFilterTests.cpp +++ b/tests/ButterworthFilterTests.cpp @@ -1,6 +1,6 @@ #define BOOST_TEST_MODULE ButterworthFilterTests -#include "fratio.h" +#include "fratio" #include "warning_macro.h" #include @@ -8,13 +8,13 @@ DISABLE_CONVERSION_WARNING_BEGIN template struct System { - std::vector data = { 1, 2, 3, 4, 5, 6, 7, 8 }; - size_t order = 5; + Eigen::VectorX data = (Eigen::VectorX(8) << 1, 2, 3, 4, 5, 6, 7, 8).finished(); + int order = 5; T fc = 10; T fs = 100; - std::vector aCoeffRes = { 1.000000000000000, -2.975422109745684, 3.806018119320413, -2.545252868330468, 0.881130075437837, -0.125430622155356 }; - std::vector bCoeffRes = { 0.001282581078961, 0.006412905394803, 0.012825810789607, 0.012825810789607, 0.006412905394803, 0.001282581078961 }; - std::vector results = { 0.001282581078961, 0.012794287652606, 0.062686244350084, 0.203933712825708, 0.502244959135609, 1.010304217144175, 1.744652693589064, 2.678087381460197 }; + Eigen::VectorX aCoeffRes = (Eigen::VectorX(6) << 1.000000000000000, -2.975422109745684, 3.806018119320413, -2.545252868330468, 0.881130075437837, -0.125430622155356).finished(); + Eigen::VectorX bCoeffRes = (Eigen::VectorX(6) << 0.001282581078961, 0.006412905394803, 0.012825810789607, 0.012825810789607, 0.006412905394803, 0.001282581078961).finished(); + Eigen::VectorX results = (Eigen::VectorX(8) << 0.001282581078961, 0.012794287652606, 0.062686244350084, 0.203933712825708, 0.502244959135609, 1.010304217144175, 1.744652693589064, 2.678087381460197).finished(); }; DISABLE_CONVERSION_WARNING_END @@ -23,74 +23,76 @@ BOOST_FIXTURE_TEST_CASE(BUTTERWORTH_FILTER_FLOAT, System) { auto bf = fratio::Butterworthf(order, fc, fs); - std::vector aCoeff, bCoeff, filteredData; - bf.getCoeff(aCoeff, bCoeff); + std::vector filteredData; + Eigen::VectorX aCoeff, bCoeff; + bf.getCoeffs(aCoeff, bCoeff); BOOST_REQUIRE_EQUAL(aCoeff.size(), aCoeffRes.size()); BOOST_REQUIRE_EQUAL(bCoeff.size(), bCoeffRes.size()); BOOST_REQUIRE_EQUAL(aCoeff.size(), bCoeffRes.size()); - for (size_t i = 0; i < aCoeff.size(); ++i) { - BOOST_CHECK_SMALL(std::abs(aCoeff[i] - aCoeffRes[i]), 1e-4f); - BOOST_CHECK_SMALL(std::abs(bCoeff[i] - bCoeffRes[i]), 1e-4f); + for (Eigen::Index i = 0; i < aCoeff.size(); ++i) { + BOOST_CHECK_SMALL(std::abs(aCoeff(i) - aCoeffRes(i)), 1e-6f); + BOOST_CHECK_SMALL(std::abs(bCoeff(i) - bCoeffRes(i)), 1e-6f); } - for (float d : data) - filteredData.push_back(bf.stepFilter(d)); + for (Eigen::Index i = 0; i < data.size(); ++i) + filteredData.push_back(bf.stepFilter(data(i))); for (size_t i = 0; i < filteredData.size(); ++i) - BOOST_CHECK_SMALL(std::abs(filteredData[i] - results[i]), 1e-4f); + BOOST_CHECK_SMALL(std::abs(filteredData[i] - results(i)), 1e-6f); bf.resetFilter(); - filteredData = bf.filter(data); - for (size_t i = 0; i < filteredData.size(); ++i) - BOOST_CHECK_SMALL(std::abs(filteredData[i] - results[i]), 1e-4f); + Eigen::VectorXf fData = bf.filter(data); + for (Eigen::Index i = 0; i < fData.size(); ++i) + BOOST_CHECK_SMALL(std::abs(fData(i) - results(i)), 1e-6f); auto bf2 = fratio::Butterworthf(); bf2.setFilterParameters(order, fc, fs); - filteredData.resize(0); - for (float d : data) - filteredData.push_back(bf2.stepFilter(d)); + filteredData.clear(); + for (Eigen::Index i = 0; i < data.size(); ++i) + filteredData.push_back(bf2.stepFilter(data(i))); for (size_t i = 0; i < filteredData.size(); ++i) - BOOST_CHECK_SMALL(std::abs(filteredData[i] - results[i]), 1e-4f); + BOOST_CHECK_SMALL(std::abs(filteredData[i] - results(i)), 1e-6f); } BOOST_FIXTURE_TEST_CASE(BUTTERWORTH_FILTER_DOUBLE, System) { auto bf = fratio::Butterworthd(order, fc, fs); - std::vector aCoeff, bCoeff, filteredData; - bf.getCoeff(aCoeff, bCoeff); + std::vector filteredData; + Eigen::VectorX aCoeff, bCoeff; + bf.getCoeffs(aCoeff, bCoeff); BOOST_REQUIRE_EQUAL(aCoeff.size(), aCoeffRes.size()); BOOST_REQUIRE_EQUAL(bCoeff.size(), bCoeffRes.size()); BOOST_REQUIRE_EQUAL(aCoeff.size(), bCoeffRes.size()); - for (size_t i = 0; i < aCoeff.size(); ++i) { - BOOST_CHECK_SMALL(std::abs(aCoeff[i] - aCoeffRes[i]), 1e-14); - BOOST_CHECK_SMALL(std::abs(bCoeff[i] - bCoeffRes[i]), 1e-14); + for (Eigen::Index i = 0; i < aCoeff.size(); ++i) { + BOOST_CHECK_SMALL(std::abs(aCoeff(i) - aCoeffRes(i)), 1e-14); + BOOST_CHECK_SMALL(std::abs(bCoeff(i) - bCoeffRes(i)), 1e-14); } - for (double d : data) - filteredData.push_back(bf.stepFilter(d)); + for (Eigen::Index i = 0; i < data.size(); ++i) + filteredData.push_back(bf.stepFilter(data(i))); for (size_t i = 0; i < filteredData.size(); ++i) - BOOST_CHECK_SMALL(std::abs(filteredData[i] - results[i]), 1e-14); + BOOST_CHECK_SMALL(std::abs(filteredData[i] - results(i)), 1e-14); bf.resetFilter(); - filteredData = bf.filter(data); - for (size_t i = 0; i < filteredData.size(); ++i) - BOOST_CHECK_SMALL(std::abs(filteredData[i] - results[i]), 1e-14); + Eigen::VectorXd fData = bf.filter(data); + for (Eigen::Index i = 0; i < fData.size(); ++i) + BOOST_CHECK_SMALL(std::abs(fData(i) - results(i)), 1e-14); auto bf2 = fratio::Butterworthd(); bf2.setFilterParameters(order, fc, fs); - filteredData.resize(0); - for (double d : data) - filteredData.push_back(bf2.stepFilter(d)); + filteredData.clear(); + for (Eigen::Index i = 0; i < data.size(); ++i) + filteredData.push_back(bf2.stepFilter(data(i))); for (size_t i = 0; i < filteredData.size(); ++i) - BOOST_CHECK_SMALL(std::abs(filteredData[i] - results[i]), 1e-14); + BOOST_CHECK_SMALL(std::abs(filteredData[i] - results(i)), 1e-14); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c83b3ea..a0ee31c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -20,8 +20,8 @@ macro(addTest testName) GENERATE_MSVC_DOT_USER_FILE(${testName}) endmacro(addTest) -addTest(GenericFilterTests) +# addTest(GenericFilterTests) +# addTest(polynome_functions_tests) addTest(DigitalFilterTests) -addTest(MovingAverageFilterTests) -addTest(ButterWorthFilterTests) -# addTest(polynome_functions_tests) \ No newline at end of file +# addTest(MovingAverageFilterTests) +# addTest(ButterWorthFilterTests) \ No newline at end of file diff --git a/tests/DigitalFilterTests.cpp b/tests/DigitalFilterTests.cpp index 4ad8fc5..2e0226f 100644 --- a/tests/DigitalFilterTests.cpp +++ b/tests/DigitalFilterTests.cpp @@ -1,7 +1,6 @@ #define BOOST_TEST_MODULE DigitalFilterTests -#include "fratio.h" -#include "typedefs.h" +#include "fratio" #include "warning_macro.h" #include @@ -10,8 +9,8 @@ DISABLE_CONVERSION_WARNING_BEGIN template struct System { Eigen::VectorX data = (Eigen::VectorX(4) << 1, 2, 3, 4).finished(); - Eigen::VectorX aCoeff = (Eigen::VectorX(4) << 1, -0.99993717).finished(); - Eigen::VectorX bCoeff = (Eigen::VectorX(4) << 0.99996859, -0.99996859).finished(); + Eigen::VectorX aCoeff = (Eigen::VectorX(2) << 1, -0.99993717).finished(); + Eigen::VectorX bCoeff = (Eigen::VectorX(2) << 0.99996859, -0.99996859).finished(); Eigen::VectorX results = (Eigen::VectorX(4) << 0.99996859, 1.999874351973491, 2.999717289867956, 3.999497407630634).finished(); }; diff --git a/tests/GenericFilterTests.cpp b/tests/GenericFilterTests.cpp index 2aa6080..b6399dc 100644 --- a/tests/GenericFilterTests.cpp +++ b/tests/GenericFilterTests.cpp @@ -1,7 +1,6 @@ #define BOOST_TEST_MODULE GenericFilterTests -#include "fratio.h" -#include "typedefs.h" +#include "fratio" #include #include diff --git a/tests/MovingAverageFilterTests.cpp b/tests/MovingAverageFilterTests.cpp index d9a501e..e11a4bd 100644 --- a/tests/MovingAverageFilterTests.cpp +++ b/tests/MovingAverageFilterTests.cpp @@ -1,6 +1,6 @@ #define BOOST_TEST_MODULE MovingAverageFilterTests -#include "fratio.h" +#include "fratio" #include template @@ -18,7 +18,7 @@ BOOST_FIXTURE_TEST_CASE(MOVING_AVERAGE_FLOAT, System) filteredData.push_back(maf.stepFilter(data(i))); for (size_t i = 0; i < filteredData.size(); ++i) - BOOST_CHECK_SMALL(std::abs(filteredData[i] - results[i]), 1e-6f); + BOOST_CHECK_SMALL(std::abs(filteredData[i] - results(i)), 1e-6f); maf.resetFilter(); Eigen::VectorXf fData = maf.filter(data); diff --git a/tests/polynome_functions_tests.cpp b/tests/polynome_functions_tests.cpp index fe4ec3c..783798f 100644 --- a/tests/polynome_functions_tests.cpp +++ b/tests/polynome_functions_tests.cpp @@ -1,31 +1,35 @@ #define BOOST_TEST_MODULE polynome_functions_tests -#include "fratio.h" +#include "fratio" #include "warning_macro.h" #include +using c_int_t = std::complex; +template +using c_t = std::complex; + struct SystemInt { - std::vector data = { 1, 1, 1, 1 }; - std::vector results = { 1, -4, 6, -4, 1 }; + Eigen::VectorX data = (Eigen::VectorX(4) << 1, 1, 1, 1).finished(); + Eigen::VectorX results = (Eigen::VectorX(5) << 1, -4, 6, -4, 1).finished(); }; struct SystemCInt { - std::vector> data = { { 1, 1 }, { -1, 4 }, { 12, -3 }, { 5, 2 } }; - std::vector> results = { { 1, 0 }, { -17, -4 }, { 66, 97 }, { 127, -386 }, { -357, 153 } }; + Eigen::VectorX data = (Eigen::VectorX(4) << c_int_t{ 1, 1 }, c_int_t{ -1, 4 }, c_int_t{ 12, -3 }, c_int_t{ 5, 2 }).finished(); + Eigen::VectorX results = (Eigen::VectorX(5) << c_int_t{ 1, 0 }, c_int_t{ -17, -4 }, c_int_t{ 66, 97 }, c_int_t{ 127, -386 }, c_int_t{ -357, 153 }).finished(); }; DISABLE_CONVERSION_WARNING_BEGIN template struct SystemFloat { - std::vector data = { 0.32, -0.0518, 41.4, 0.89 }; - std::vector results = { 1, -42.558199999999999, 48.171601999999993, -9.181098159999999, -0.610759296 }; + Eigen::VectorX data = (Eigen::VectorX(4) << 0.32, -0.0518, 41.4, 0.89).finished(); + Eigen::VectorX results = (Eigen::VectorX(5) << 1, -42.558199999999999, 48.171601999999993, -9.181098159999999, -0.610759296).finished(); }; template struct SystemCFloat { - std::vector> data = { { 1.35, 0.2 }, { -1.5, 4.45 }, { 12.8, -3.36 }, { 5.156, 2.12 } }; - std::vector> results = { { 1, 0 }, { -17.806, -3.41 }, { 73.2776, 99.20074 }, { 101.857496, -444.634694 }, { -269.1458768, 388.7308864 } }; + Eigen::VectorX> data = (Eigen::VectorX>(4) << c_t{ 1.35, 0.2 }, c_t{ -1.5, 4.45 }, c_t{ 12.8, -3.36 }, c_t{ 5.156, 2.12 }).finished(); + Eigen::VectorX> results = (Eigen::VectorX>(5) << c_t{ 1, 0 }, c_t{ -17.806, -3.41 }, c_t{ 73.2776, 99.20074 }, c_t{ 101.857496, -444.634694 }, c_t{ -269.1458768, 388.7308864 }).finished(); }; DISABLE_CONVERSION_WARNING_END @@ -34,46 +38,46 @@ BOOST_FIXTURE_TEST_CASE(POLYNOME_FUNCTION_INT, SystemInt) { auto res = fratio::VietaAlgoi::polyCoeffFromRoot(data); - for (size_t i = 0; i < res.size(); ++i) - BOOST_CHECK_EQUAL(res[i], results[i]); + for (Eigen::Index i = 0; i < res.size(); ++i) + BOOST_CHECK_EQUAL(res(i), results(i)); } BOOST_FIXTURE_TEST_CASE(POLYNOME_FUNCTION_FLOAT, SystemFloat) { auto res = fratio::VietaAlgof::polyCoeffFromRoot(data); - for (size_t i = 0; i < res.size(); ++i) - BOOST_CHECK_SMALL(std::abs(res[i] - results[i]), 1e-6f); + for (Eigen::Index i = 0; i < res.size(); ++i) + BOOST_CHECK_SMALL(std::abs(res(i) - results(i)), 1e-6f); } BOOST_FIXTURE_TEST_CASE(POLYNOME_FUNCTION_DOUBLE, SystemFloat) { auto res = fratio::VietaAlgod::polyCoeffFromRoot(data); - for (size_t i = 0; i < res.size(); ++i) - BOOST_CHECK_SMALL(std::abs(res[i] - results[i]), 1e-14); + for (Eigen::Index i = 0; i < res.size(); ++i) + BOOST_CHECK_SMALL(std::abs(res(i) - results(i)), 1e-14); } BOOST_FIXTURE_TEST_CASE(POLYNOME_FUNCTION_CINT, SystemCInt) { auto res = fratio::VietaAlgoci::polyCoeffFromRoot(data); - for (size_t i = 0; i < res.size(); ++i) - BOOST_CHECK_EQUAL(res[i], results[i]); + for (Eigen::Index i = 0; i < res.size(); ++i) + BOOST_CHECK_EQUAL(res(i), results(i)); } BOOST_FIXTURE_TEST_CASE(POLYNOME_FUNCTION_CFLOAT, SystemCFloat) { auto res = fratio::VietaAlgocf::polyCoeffFromRoot(data); - for (size_t i = 0; i < res.size(); ++i) - BOOST_CHECK_SMALL(std::abs(res[i] - results[i]), 1e-4f); + for (Eigen::Index i = 0; i < res.size(); ++i) + BOOST_CHECK_SMALL(std::abs(res(i) - results(i)), 1e-4f); } BOOST_FIXTURE_TEST_CASE(POLYNOME_FUNCTION_CDOUBLE, SystemCFloat) { auto res = fratio::VietaAlgocd::polyCoeffFromRoot(data); - for (size_t i = 0; i < res.size(); ++i) - BOOST_CHECK_SMALL(std::abs(res[i] - results[i]), 1e-12); + for (Eigen::Index i = 0; i < res.size(); ++i) + BOOST_CHECK_SMALL(std::abs(res(i) - results(i)), 1e-12); }