DiFipp/include/GenericFilter.h

113 wiersze
4.1 KiB
C
Czysty Zwykły widok Historia

2018-10-23 01:16:10 +00:00
#pragma once
#include "type_checks.h"
2018-12-14 10:04:41 +00:00
#include "typedefs.h"
2018-10-23 01:16:10 +00:00
#include <stddef.h>
2018-12-14 10:04:41 +00:00
#include <string>
2018-10-23 01:16:10 +00:00
2018-10-23 10:22:57 +00:00
namespace fratio {
2019-01-04 08:15:03 +00:00
/*! \brief Low-level filter.
*
* It creates the basic and common functions of all linear filter that can written as a digital filter.
* This class can not be instantiated directly.
*
* \warning In Debug mode, all functions may throw if a filter is badly initialized.
* This not the case in Realese mode.
*
* \tparam T Floating type.
*/
template <typename T>
class GenericFilter {
static_assert(std::is_floating_point<T>::value && !std::is_const<T>::value, "Only accept non-complex floating point types.");
2018-10-23 10:22:57 +00:00
public:
2019-01-04 08:15:03 +00:00
/*! \brief Get the meaning of the filter status.
* \param status Filter status to get the meaning from.
* \return The meaning.
*/
2018-12-14 10:04:41 +00:00
static std::string filterStatus(FilterStatus status);
public:
2019-01-04 08:15:03 +00:00
/*! \brief Filter a new data.
*
* This function is practical for online application that does not know the whole signal in advance.
* \param data New data to filter.
* \return Filtered data.
*/
2018-12-17 05:48:44 +00:00
T stepFilter(const T& data);
2019-01-04 08:15:03 +00:00
/*! \brief Filter a signal.
*
* Filter all data given by the signal.
* \param data Signal.
* \return Filtered signal.
*/
2018-12-17 08:16:01 +00:00
vectX_t<T> filter(const vectX_t<T>& data);
2019-01-04 08:15:03 +00:00
/*! \brief Filter a signal and store in a user-defined Eigen vector.
*
* Useful if the length of the signal is known in advance.
* \param[out] results Filtered signal.
* \param data Signal.
* \return False if vector's lengths do not match.
*/
2018-12-17 08:16:01 +00:00
bool getFilterResults(Eigen::Ref<vectX_t<T>> results, const vectX_t<T>& data);
2019-01-04 08:15:03 +00:00
/*! \brief Reset the data and filtered data. */
2018-10-23 10:22:57 +00:00
void resetFilter();
2019-01-04 08:15:03 +00:00
/*! \brief Get digital filter coefficients.
*
* It will automatically resize the given vectors.
* \param[out] aCoeff Denominator coefficients of the filter in decreasing order.
* \param[out] bCoeff Numerator coefficients of the filter in decreasing order.
*/
2018-12-17 08:16:01 +00:00
void getCoeffs(vectX_t<T>& aCoeff, vectX_t<T>& bCoeff) const;
2019-01-04 08:15:03 +00:00
/*! \brief Return the current filter status. */
2018-12-14 10:04:41 +00:00
FilterStatus status() const noexcept { return m_status; }
2019-01-04 08:15:03 +00:00
/*! \brief Return the order the denominator polynome order of the filter. */
2018-12-17 05:48:44 +00:00
Eigen::Index aOrder() const noexcept { return m_aCoeff.size(); }
2019-01-04 08:15:03 +00:00
/*! \brief Return the order the numerator polynome order of the filter. */
2018-12-17 05:48:44 +00:00
Eigen::Index bOrder() const noexcept { return m_bCoeff.size(); }
2018-10-23 10:22:57 +00:00
protected:
2019-01-04 08:15:03 +00:00
/*! \brief Default uninitialized constructor. */
GenericFilter() = default;
2019-01-04 08:15:03 +00:00
/*! \brief Constructor.
* \param aCoeff Denominator coefficients of the filter in decreasing order.
* \param bCoeff Numerator coefficients of the filter in decreasing order.
*/
2018-12-17 08:16:01 +00:00
GenericFilter(const vectX_t<T>& aCoeff, const vectX_t<T>& bCoeff);
2019-01-04 08:15:03 +00:00
/*! \brief Default destructor. */
virtual ~GenericFilter() = default;
2019-01-04 08:15:03 +00:00
/*! \brief Set the new coefficients of the filters.
*
* It awaits a universal reference.
* \param aCoeff Denominator coefficients of the filter in decreasing order.
* \param bCoeff Numerator coefficients of the filter in decreasing order.
*/
template <typename T2>
void setCoeffs(T2&& aCoeff, T2&& bCoeff);
/*! \brief Normalized the filter coefficients such that aCoeff(0) = 1. */
2018-12-14 10:04:41 +00:00
void normalizeCoeffs();
2019-01-04 08:15:03 +00:00
/*! \brief Check for bad coefficients.
*
* Set the filter status to ready is everything is fine.
* \param aCoeff Denominator coefficients of the filter.
* \param bCoeff Numerator coefficients of the filter.
* \return True if the filter status is set on READY.
*/
2018-12-17 08:16:01 +00:00
bool checkCoeffs(const vectX_t<T>& aCoeff, const vectX_t<T>& bCoeff);
2018-10-23 10:22:57 +00:00
protected:
2019-01-04 08:15:03 +00:00
FilterStatus m_status; /*!< Filter status */
2018-10-23 10:22:57 +00:00
private:
2019-01-04 08:15:03 +00:00
vectX_t<T> m_aCoeff; /*!< Denominator coefficients of the filter */
vectX_t<T> m_bCoeff; /*!< Numerator coefficients of the filter */
vectX_t<T> m_filteredData; /*!< Last set of filtered data */
vectX_t<T> m_rawData; /*!< Last set of non-filtered data */
2018-10-23 10:22:57 +00:00
};
} // namespace fratio
2018-12-14 10:04:41 +00:00
#include "GenericFilter.tpp"