19#include <nlohmann/json.hpp>
20using json = nlohmann::json;
37template<
typename Scalar =
double>
76 _data.resize(windowSize);
103 void push_back(
const std::pair<Scalar, Scalar>& dataPoint)
105 push_back(dataPoint.first, dataPoint.second);
113 if (!
_data.empty() &&
_data.back().first == x)
120 _data.back().second = y;
132 _data.push_back(std::make_pair(x, y));
145 auto prepareDataVectors = [&]() {
146 auto n =
static_cast<int>(
_data.size());
147 Eigen::VectorX<Scalar> x = Eigen::VectorX<Scalar>(n);
148 Eigen::VectorX<Scalar> y = Eigen::VectorX<Scalar>(n);
150 for (
size_t i = 0; i <
_data.size(); i++)
152 x(
static_cast<int>(i)) =
_data.at(i).first;
153 y(
static_cast<int>(i)) =
_data.at(i).second;
156 return std::make_pair(x, y);
165 auto [x, y] = prepareDataVectors();
170 auto [x, y] = prepareDataVectors();
175 auto [x, y] = prepareDataVectors();
180 auto [x, y] = prepareDataVectors();
186 return { Eigen::VectorX<Scalar>() };
216 if (
_data.empty()) {
return; }
218 auto [x, y] =
_data.front();
236template<
typename Scalar =
double>
249template<
typename Scalar =
double>
252 if (j.contains(
"strategy"))
256 if (j.contains(
"polyDegree"))
261 if (j.contains(
"windowSize"))
#define INS_ASSERT_USER_ERROR(_EXP, _MSG)
Assert function with message.
Bidiagonal Divide and Conquer SVD Curve Fit.
Complete Orthogonal Decomposition Curve Fit.
nlohmann::json json
json namespace
Incremental Least Squares Curve Fit.
static Eigen::VectorX< Scalar > calcCoefficients(const Eigen::MatrixBase< DerivedX > &x, const Eigen::MatrixBase< DerivedY > &y, size_t polynomialDegree=2)
Calculates the polynomial coefficients in order a0 + a1 * x + a2 * x^2 + ...
static Eigen::VectorX< Scalar > calcCoefficients(const Eigen::MatrixBase< DerivedX > &x, const Eigen::MatrixBase< DerivedY > &y, size_t polynomialDegree=2)
Calculates the polynomial coefficients in order a0 + a1 * x + a2 * x^2 + ...
static Eigen::VectorX< Scalar > calcCoefficients(const Eigen::MatrixBase< DerivedX > &x, const Eigen::MatrixBase< DerivedY > &y, size_t polynomialDegree=2)
Calculates the polynomial coefficients in order a0 + a1 * x + a2 * x^2 + ...
Incremental Least Squares Curve Fitting.
static Eigen::VectorX< Scalar > calcCoefficients(const Eigen::MatrixBase< DerivedX > &x, const Eigen::MatrixBase< DerivedY > &y, size_t polynomialDegree=2)
Calculates the polynomial coefficients in order a0 + a1 * x + a2 * x^2 + ...
Polynomial Curve Fitting.
IncrementalLeastSquares< double > _incrementalLSQ
PolynomialRegressor(size_t polynomialDegree, size_t windowSize, Strategy strategy=Strategy::HouseholderQR)
Constructor.
const ScrollingBuffer< std::pair< Scalar, Scalar > > & data() const
Gets the underlying buffer.
bool windowSizeReached() const
Checks if the amount of data points equals the window size.
bool empty() const
Checks if the container has no elements.
void push_back(const Scalar &x, const Scalar &y)
Add a data point to the polynomial.
void setStrategy(Strategy strategy)
Set the strategy for the fit and resets the data.
Strategy
Possible Fit strategies.
@ BDCSVD
Bidiagonal Divide and Conquer SVD.
@ IncrementalLeastSquares
Incremental Least Squares (only polynomials of order <= 2)
@ COUNT
Amount of items in the enum.
@ HouseholderQR
Householder QR decomposition.
@ COD
Complete Orthogonal Decomposition.
@ LeastSquares
Least Squares (bas if even mildly ill-conditioned)
void setWindowSize(size_t windowSize)
Sets the amount of points used for the fit (sliding window)
void setPolynomialDegree(size_t polynomialDegree)
Set the Polynomial Degree and resets the data.
void reset()
Reset the polynomial coefficients and saved data.
Polynomial< Scalar > calcPolynomial() const
Calculates the polynomial.
void push_back(const std::pair< Scalar, Scalar > &dataPoint)
Add a data point to the polynomial.
void pop_front()
Removes the first data point from the polynomial fit (sliding window)
ScrollingBuffer< std::pair< double, double > > _data
void to_json(json &j, const Node &node)
Converts the provided node into a json object.
const char * to_string(gui::widgets::PositionWithFrame::ReferenceFrame refFrame)
Converts the enum to a string.
void from_json(const json &j, Node &node)
Converts the provided json object into a node object.