20#include <nlohmann/json.hpp>
21using json = nlohmann::json;
38template<
typename Scalar =
double>
77 _data.resize(windowSize);
104 void push_back(
const std::pair<Scalar, Scalar>& dataPoint)
106 push_back(dataPoint.first, dataPoint.second);
114 if (!
_data.empty() &&
_data.back().first == x)
121 _data.back().second = y;
133 _data.push_back(std::make_pair(x, y));
148 auto prepareDataVectors = [&]() {
149 auto n =
static_cast<int>(
_data.size());
150 Eigen::VectorX<Scalar> x = Eigen::VectorX<Scalar>(n);
151 Eigen::VectorX<Scalar> y = Eigen::VectorX<Scalar>(n);
153 for (
size_t i = 0; i <
_data.size(); i++)
155 x(
static_cast<int>(i)) =
_data.at(i).first;
156 y(
static_cast<int>(i)) =
_data.at(i).second;
159 return std::make_pair(x, y);
168 auto [x, y] = prepareDataVectors();
173 auto [x, y] = prepareDataVectors();
178 auto [x, y] = prepareDataVectors();
183 auto [x, y] = prepareDataVectors();
189 return { Eigen::VectorX<Scalar>() };
219 if (
_data.empty()) {
return; }
221 auto [x, y] =
_data.front();
239template<
typename Scalar =
double>
252template<
typename Scalar =
double>
255 if (j.contains(
"strategy"))
259 if (j.contains(
"polyDegree"))
264 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.
std::optional< Polynomial< Scalar > > calcPolynomial() const
Calculates the polynomial.
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.
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.