34#ifndef SBL_CONTROL_CALIBRATION_HPP_
35#define SBL_CONTROL_CALIBRATION_HPP_
70 float sum_x = 0.0f, sum_y = 0.0f;
71 for (uint8_t i = 0; i < n; ++i) {
75 const float inv_n = 1.0f /
static_cast<float>(n);
76 const float mean_x = sum_x * inv_n;
77 const float mean_y = sum_y * inv_n;
79 float cov = 0.0f, var = 0.0f;
80 for (uint8_t i = 0; i < n; ++i) {
81 const float dx = measured[i] - mean_x;
82 cov += dx * (expected[i] - mean_y);
89 out.
scale = cov / var;
131template<u
int8_t N,
typename Standard = VOctStandard>
133 static_assert(N >= 2,
"a line needs two points");
138 for (uint8_t i = 0; i < N; ++i) {
139 references_[i] = references[i];
159 measured_[point_] = measured;
166 for (uint8_t i = 0; i < N; ++i) {
167 expected[i] = Standard::expected(references_[i]);
171 if (
fit_points(measured_, expected, N, fitted)) {
199 float apply(
float measured)
const {
return fit_.
apply(measured); }
207 uint8_t
point()
const {
return point_; }
209 static constexpr uint8_t
points() {
return N; }
212 float reference()
const {
return references_[point_ < N ? point_ : N - 1]; }
224 float references_[N]{};
225 float measured_[N]{};
Capture N reference points, then fit.
PointCalibration(const float(&references)[N])
void set_fit(const LinearFit &fit)
Load a stored fit (boot) or install one by hand.
CalState state() const
Where the routine is.
bool capture(float measured)
Record the source's reading for the current reference.
const LinearFit & fit() const
The current fit.
float reference() const
The reference the app should patch for the current point.
uint8_t point() const
Points captured so far: the index of the reference to patch next, for the UI.
float apply(float measured) const
Correct a reading; identity until a fit is computed or loaded.
static constexpr uint8_t points()
Points the routine captures.
bool active() const
True while capturing points.
The control stack: scheduler, event queue, merge, CC mapping, calibration.
bool fit_points(const float *measured, const float *expected, uint8_t n, LinearFit &out)
Least-squares line through captured points.
constexpr float apply(float measured) const
Correct a measured value.
1 V per octave: the library's pitch is octaves (CvMapping::VOct)
static constexpr float OCTAVES_PER_VOLT
static constexpr float expected(float volts)
The value a reference should read once corrected.