Kstars

polynomialfit.cpp
1#include "polynomialfit.h"
2
3#include "ekos/ekos.h"
4#include <gsl/gsl_fit.h>
5#include <gsl/gsl_vector.h>
6#include <gsl/gsl_min.h>
7#include <ekos_focus_debug.h>
8
9namespace Ekos
10{
11
12PolynomialFit::PolynomialFit(int degree_, uint8_t maxCount, const QVector<double> &x_, const QVector<double> &y_)
13 : degree(degree_), x(x_), y(y_)
14{
15 Q_ASSERT(x_.size() == y_.size());
16 QVector<QPointF> values;
17 for (int i = 0; i < x.count(); i++)
18 values << QPointF(x[i], y[i]);
19
20 std::sort(values.begin(), values.end(), [](const QPointF & a, const QPointF & b)
21 {
22 return a.y() < b.y();
23 });
24
25 while(values.count() > maxCount)
26 values.takeLast();
27
28 x.clear();
29 y.clear();
30
31 for (const auto &onePoint : values)
32 {
33 x << onePoint.x();
34 y << onePoint.y();
35 }
36
37 solve(x, y);
38}
39
40PolynomialFit::PolynomialFit(int degree_, const QVector<int> &x_, const QVector<double> &y_)
41 : degree(degree_), y(y_)
42{
43 Q_ASSERT(x_.size() == y_.size());
44 for (int i = 0; i < x_.size(); ++i)
45 {
46 x.push_back(static_cast<double>(x_[i]));
47 }
48 solve(x, y);
49}
50
51void PolynomialFit::solve(const QVector<double> &x, const QVector<double> &y)
52{
53 double chisq = 0;
54 coefficients = gsl_polynomial_fit(x.data(), y.data(), x.count(), degree, chisq);
55}
56
57double PolynomialFit::polynomialFunction(double x, void *params)
58{
59 PolynomialFit *instance = static_cast<PolynomialFit *>(params);
60
61 if (instance && !instance->coefficients.empty())
62 return instance->f(x);
63 else
64 return -1;
65}
66
67double PolynomialFit::f(double x)
68{
69 const int order = coefficients.size() - 1;
70 double sum = 0;
71 for (int i = 0; i <= order; ++i)
72 sum += coefficients[i] * pow(x, i);
73 return sum;
74}
75
76bool PolynomialFit::findMinimum(double expected, double minPosition, double maxPosition, double *position, double *value)
77{
78 int status;
79 int iter = 0, max_iter = 100;
82 double m = expected;
83
85 F.function = &PolynomialFit::polynomialFunction;
86 F.params = this;
87
88 // Must turn off error handler or it aborts on error
90
94
95 if (status != GSL_SUCCESS)
96 {
97 qCWarning(KSTARS_EKOS_FOCUS) << "Focus GSL error:" << gsl_strerror(status);
98 return false;
99 }
100
101 do
102 {
103 iter++;
105
109
111
112 if (status == GSL_SUCCESS)
113 {
114 *position = m;
115 *value = polynomialFunction(m, this);
116 }
117 }
118 while (status == GSL_CONTINUE && iter < max_iter);
119
121 return (status == GSL_SUCCESS);
122}
123} // namespace
124
Ekos is an advanced Astrophotography tool for Linux.
Definition align.cpp:78
QAction * solve(const QObject *recvr, const char *slot, QObject *parent)
NETWORKMANAGERQT_EXPORT NetworkManager::Status status()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:19:02 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.