GY-63_MS5611/libraries/Correlation/Correlation.h

137 lines
4.2 KiB
C
Raw Normal View History

#pragma once
//
// FILE: Correlation.h
2021-01-29 06:31:58 -05:00
// AUTHOR: Rob Tillaart
2022-10-30 13:12:10 -04:00
// VERSION: 0.2.3
// PURPOSE: Calculate Correlation from a small dataset.
// HISTORY: See Correlation.cpp
//
2021-01-29 06:31:58 -05:00
#include "Arduino.h"
2022-10-30 13:12:10 -04:00
#define CORRELATION_LIB_VERSION (F("0.2.3"))
2021-01-29 06:31:58 -05:00
class Correlation
{
public:
2022-10-30 13:12:10 -04:00
Correlation(uint8_t size = 20); // WARNING calculate memory usage !!
2021-01-29 06:31:58 -05:00
~Correlation();
2022-10-30 13:12:10 -04:00
// returns true if the pair of values is added to internal array.
// returns false when internal array is full.
bool add(float x, float y);
2022-10-30 13:12:10 -04:00
// administrative functions
uint8_t count() { return _count; };
2021-01-29 06:31:58 -05:00
uint8_t size() { return _size; };
void clear();
2021-08-27 10:16:35 -04:00
2022-10-30 13:12:10 -04:00
// in running mode, adding new pair of values will replace old ones
// this constantly adapts the regression parameters A and B (iff calculate is called)
void setRunningCorrelation(bool rc) { _runningMode = rc; };
2021-01-29 06:31:58 -05:00
bool getRunningCorrelation() { return _runningMode; };
2021-08-27 10:16:35 -04:00
2022-10-30 13:12:10 -04:00
// worker, to calculate the correlation parameters.
// MUST be called before retrieving the parameters
2021-12-14 10:39:48 -05:00
// A, B, R, Rsquared, Esquared, avgX and avgY
2021-08-27 10:16:35 -04:00
//
2022-10-30 13:12:10 -04:00
// parameter forced overrules the _needRecalculate flag.
// forced is default false to maintain backwards compatibility
2021-08-27 10:16:35 -04:00
//
2022-10-30 13:12:10 -04:00
// returns false if contains no elements ==> count() == 0
2021-08-27 10:16:35 -04:00
bool calculate(bool forced = false);
2022-10-30 13:12:10 -04:00
// enables / disables R, Rsquared and Esquared calculation
// This can be used to speed up the calculate function if
// these values are not used in your project.
2021-08-27 10:16:35 -04:00
void setR2Calculation(bool doR2) { _doR2 = doR2; };
bool getR2Calculation() { return _doR2; };
void setE2Calculation(bool doE2) { _doE2 = doE2; };
bool getE2Calculation() { return _doE2; };
2022-10-30 13:12:10 -04:00
// Y = A + B * X
// note if no elements are added or calculate is not called
2021-08-27 10:16:35 -04:00
// the values for A and B are 0
float getA() { return _a; };
float getB() { return _b; };
2021-08-27 10:16:35 -04:00
2022-10-30 13:12:10 -04:00
// getR() returns correlation coefficient (0.2.0 fixed sign)
2021-08-27 10:16:35 -04:00
float getR() { return _r; };
float getRsquare() { return _r * _r; };
2022-10-30 13:12:10 -04:00
// returns sum of the errors squared == indication of 'spread'
// the smaller this value the more the points are on/near one line.
float getEsquare() { return _sumErrorSquare; };
2021-08-27 10:16:35 -04:00
2022-10-30 13:12:10 -04:00
// get the average values of the datasets (if count > 0)
2022-06-21 02:22:05 -04:00
float getAverageX(){ return _avgX; }; // will replace getAvgX() in time
float getAverageY(){ return _avgY; }; // will replace getAvgY() in time
float getAvgX() { return _avgX; }; // will be obsolete in future
float getAvgY() { return _avgY; }; // will be obsolete in future
2021-08-27 10:16:35 -04:00
2022-10-30 13:12:10 -04:00
// based on the dataset get the estimated values for X and Y
// it uses the last calculated A and B
// library does not return a confidence interval for these values.
float getEstimateY(float x);
float getEstimateX(float y);
2021-01-29 06:31:58 -05:00
2022-10-30 13:12:10 -04:00
// STATISTICAL
2021-01-29 06:31:58 -05:00
float getMinX(); // idem
float getMaxX(); // idem
float getMinY(); // idem
float getMaxY(); // idem
2022-10-30 13:12:10 -04:00
// DEBUGGING - access to internal arrays.
bool setXY(uint8_t index, float x, float y); // returns true if succeeded
bool setX(uint8_t index, float x); // returns true if succeeded
bool setY(uint8_t index, float y); // ss returns true if succeeded
float getX(uint8_t index); // idem
float getY(uint8_t index); // idem
2021-08-27 10:16:35 -04:00
2022-06-21 02:22:05 -04:00
float getSumXY() { return _sumXiYi; }; // replaces getSumXiYi()
float getSumX2() { return _sumXi2; }; // replaces getSumXi2()
float getSumY2() { return _sumYi2; }; // replaces getSumYi2()
float getSumXiYi() { return _sumXiYi; }; // obsolete in version 0.3.0
float getSumXi2() { return _sumXi2; }; // obsolete in version 0.3.0
float getSumYi2() { return _sumYi2; }; // obsolete in version 0.3.0
2021-01-29 06:31:58 -05:00
private:
2021-01-29 06:31:58 -05:00
uint8_t _size = 0;
2021-12-14 10:39:48 -05:00
uint8_t _index = 0;
uint8_t _count = 0;
bool _runningMode = false;
bool _needRecalculate = true;
2021-08-27 10:16:35 -04:00
bool _doE2 = true;
bool _doR2 = true;
2021-01-29 06:31:58 -05:00
float * _x;
float * _y;
float _avgX;
float _avgY;
float _a;
float _b;
2022-06-21 02:22:05 -04:00
float _div_b;
2021-08-27 10:16:35 -04:00
float _r;
float _sumErrorSquare;
2021-01-29 06:31:58 -05:00
float _sumXiYi;
float _sumXi2;
float _sumYi2;
};
2021-12-14 10:39:48 -05:00
// -- END OF FILE --
2021-12-14 10:39:48 -05:00