Skip to content

Commit 2e46b6f

Browse files
committed
0.1.4 Correlation
1 parent 9d0e558 commit 2e46b6f

File tree

11 files changed

+122
-68
lines changed

11 files changed

+122
-68
lines changed

libraries/Correlation/.github/workflows/arduino_test_runner.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ name: Arduino CI
44
on: [push, pull_request]
55

66
jobs:
7-
arduino_ci:
7+
runTest:
88
runs-on: ubuntu-latest
99

1010
steps:
1111
- uses: actions/checkout@v2
12-
- uses: Arduino-CI/action@master
13-
# Arduino-CI/[email protected]
12+
- uses: ruby/setup-ruby@v1
13+
with:
14+
ruby-version: 2.6
15+
- run: |
16+
gem install arduino_ci
17+
arduino_ci.rb

libraries/Correlation/Correlation.cpp

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
//
22
// FILE: Correlation.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.3
4+
// VERSION: 0.1.4
55
// PURPOSE: Arduino Library to determine correlation between X and Y dataset
66
//
77
// HISTORY:
8+
// 0.1.4 2021-08-26 improve performance calculate
9+
//
810
// 0.1.3 2021-01-16 add size in constructor,
911
// add statistical + debug functions
1012
// 0.1.2 2020-12-17 add arduino-CI + unit tests
@@ -73,40 +75,52 @@ bool Correlation::calculate()
7375
if (!_needRecalculate) return true;
7476

7577
// CALC AVERAGE X, AVERAGE Y
76-
_avgX = 0;
77-
_avgY = 0;
78+
float avgx = 0;
79+
float avgy = 0;
7880
for (uint8_t i = 0; i < _count; i++)
7981
{
80-
_avgX += _x[i];
81-
_avgY += _y[i];
82+
avgx += _x[i];
83+
avgy += _y[i];
8284
}
83-
_avgX /= _count;
84-
_avgY /= _count;
85+
avgx /= _count;
86+
avgy /= _count;
87+
88+
_avgX = avgx;
89+
_avgY = avgy;
8590

8691
// CALC A and B ==> formula Y = A + B*X
87-
_sumXiYi = 0;
88-
_sumXi2 = 0;
89-
_sumYi2 = 0;
92+
float sumXiYi = 0;
93+
float sumXi2 = 0;
94+
float sumYi2 = 0;
9095
for (uint8_t i = 0; i < _count; i++)
9196
{
92-
float xi = _x[i] - _avgX;
93-
float yi = _y[i] - _avgY;
94-
_sumXiYi += (xi * yi);
95-
_sumXi2 += (xi * xi);
96-
_sumYi2 += (yi * yi);
97+
float xi = _x[i] - avgx;
98+
float yi = _y[i] - avgy;
99+
sumXiYi += (xi * yi);
100+
sumXi2 += (xi * xi);
101+
sumYi2 += (yi * yi);
97102
}
98-
_b = _sumXiYi / _sumXi2;
99-
_a = _avgY - _b * _avgX;
100-
_rSquare = _sumXiYi * _sumXiYi / (_sumXi2 * _sumYi2);
101-
103+
float b = sumXiYi / sumXi2;
104+
float a = avgy - b * avgx;
105+
// bool CORLIB_CALC_R_SQUARE
106+
_rSquare = sumXiYi * sumXiYi / (sumXi2 * sumYi2);
107+
108+
_a = a;
109+
_b = b;
110+
_sumXiYi = sumXiYi;
111+
_sumXi2 = sumXi2;
112+
_sumYi2 = sumYi2;
113+
114+
// bool CORLIB_CALC_E_SQUARE
102115
// CALC _sumErrorSquare
103-
_sumErrorSquare = 0;
116+
float sumErrorSquare = 0;
104117
for (uint8_t i = 0; i < _count; i++)
105118
{
106-
float EY = _a + _b * _x[i];
119+
float EY = a + b * _x[i];
107120
float ei = _y[i] - EY;
108-
_sumErrorSquare += (ei * ei);
121+
sumErrorSquare += (ei * ei);
109122
}
123+
_sumErrorSquare = sumErrorSquare;
110124
_needRecalculate = false;
111125
return true;
112126
}

libraries/Correlation/Correlation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: Correlation.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.3
5+
// VERSION: 0.1.4
66
// PURPOSE: Calculate Correlation from a small dataset.
77
// HISTORY: See Correlation.cpp
88
//
@@ -11,7 +11,7 @@
1111
#include "Arduino.h"
1212

1313

14-
#define CORRELATION_LIB_VERSION (F("0.1.3"))
14+
#define CORRELATION_LIB_VERSION (F("0.1.4"))
1515

1616

1717
class Correlation

libraries/Correlation/README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11

22
[![Arduino CI](https://github.com/RobTillaart/Correlation/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
3+
[![Arduino-lint](https://github.com/RobTillaart/Correlation/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/Correlation/actions/workflows/arduino-lint.yml)
4+
[![JSON check](https://github.com/RobTillaart/Correlation/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/Correlation/actions/workflows/jsoncheck.yml)
35
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/Correlation/blob/master/LICENSE)
46
[![GitHub release](https://img.shields.io/github/release/RobTillaart/Correlation.svg?maxAge=3600)](https://github.com/RobTillaart/Correlation/releases)
57

@@ -13,7 +15,7 @@ Arduino Library to determine linear correlation between X and Y datasets.
1315

1416
This library calculates the coefficients of the linear correlation
1517
between two (relative small) datasets. The size of these datasets is
16-
20 by default, but this can be set in the constructor.
18+
20 by default. The size can be set in the constructor.
1719

1820
The formula of the correlation is expressed as **Y = A + B \* X**,
1921

@@ -35,7 +37,7 @@ Use with care.
3537

3638
### Base functions
3739

38-
- **bool add(x, y)** adds a pair of **floats** to the internal storage.
40+
- **bool add(float x, float y)** adds a pair of **floats** to the internal storage.
3941
Returns true if the value is added, returns false when internal array is full.
4042
When running correlation is set, it will replace the oldest element and return true.
4143
- **uint8_t count()** returns the amount of items in the internal arrays.
@@ -56,19 +58,20 @@ Most often the R squared **sqr(R)** is used.
5658
quality of the relation.
5759
- **float getAvgX()** returns the average of all elements in the X dataset.
5860
- **float getAvgY()** returns the average of all elements in the Y dataset.
59-
- **float getEstimateX(y)** use to calculate the estimated X for a certain Y.
60-
- **float getEstimateY(x)** use to calculate the estimated Y for a certain X.
61+
- **float getEstimateX(float y)** use to calculate the estimated X for a given Y.
62+
- **float getEstimateY(float x)** use to calculate the estimated Y for a given X.
6163

6264

6365
### Running correlation
6466

65-
- **setRunningCorrelation(true | false)** sets the internal variable
67+
- **void setRunningCorrelation(bool rc)** sets the internal variable
6668
runningMode which allows **add()** to overwrite old elements in the
6769
internal arrays.
70+
- **bool getRunningCorrelation()** returns the runningMOde flag.
6871

6972
The running correlation will be calculated over the last **count** elements.
7073
This allows for more adaptive formula finding e.g. find the relation between
71-
temperature and humidity for per hour.
74+
temperature and humidity per hour.
7275

7376

7477
### Statistical

libraries/Correlation/TODO.txt

Lines changed: 0 additions & 31 deletions
This file was deleted.

libraries/Correlation/examples/correlation_performance/correlation_performance.ino

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// FILE: correlation_performance.ino
33
// AUTHOR: Rob Tillaart
44
// DATE: 2020-05-18
5-
// VERSION: 0.1.0
5+
// VERSION: 0.1.1
66
// PUPROSE: demo of the Correlation Library
77

88
// performance test: only ADD and CALCULATE as these are the most used
@@ -11,7 +11,7 @@
1111

1212
#include "Correlation.h"
1313

14-
Correlation C;
14+
Correlation C(100);
1515

1616
uint32_t start, stop, sum = 0;
1717

@@ -21,6 +21,8 @@ void setup()
2121
{
2222
Serial.begin(115200);
2323
Serial.println(__FILE__);
24+
Serial.print("CORRELATION_LIB_VERSION: ");
25+
Serial.println(CORRELATION_LIB_VERSION);
2426

2527
Serial.println("ADD");
2628
delay(10);
@@ -67,6 +69,13 @@ void setup()
6769
Serial.println(stop - start);
6870

6971

72+
Serial.println("\ngetMaxX");
73+
delay(10);
74+
start = micros();
75+
f = C.getMaxX();
76+
stop = micros();
77+
Serial.println(stop - start);
78+
7079
Serial.println("\nDone...");
7180
}
7281

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
ADD
3+
10.60
4+
5+
CALCULATE - needed
6+
2780
7+
8+
CALCULATE - no new values added
9+
12
10+
11+
getEstimateX
12+
44
13+
14+
getEstimateY
15+
20
16+
17+
Done...
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
CORRELATION_LIB_VERSION: 0.1.4
3+
ADD
4+
10.60
5+
6+
CALCULATE - needed
7+
2720
8+
9+
CALCULATE - no new values added
10+
12
11+
12+
getEstimateX
13+
44
14+
15+
getEstimateY
16+
20
17+
18+
getMaxX
19+
92
20+
21+
Done...
22+

libraries/Correlation/keywords.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ size KEYWORD2
1010
clear KEYWORD2
1111

1212
setRunningCorrelation KEYWORD2
13+
getRunningCorrelation KEYWORD2
1314
calculate KEYWORD2
1415

1516
getA KEYWORD2
@@ -23,6 +24,21 @@ getAvgY KEYWORD2
2324
getEstimateY KEYWORD2
2425
getEstimateX KEYWORD2
2526

27+
getMinX KEYWORD2
28+
getMaxX KEYWORD2
29+
getMinY KEYWORD2
30+
getMaxY KEYWORD2
31+
32+
setXY KEYWORD2
33+
setX KEYWORD2
34+
setY KEYWORD2
35+
getX KEYWORD2
36+
getY KEYWORD2
37+
38+
getSumXiYi KEYWORD2
39+
getSumXi2 KEYWORD2
40+
getSumYi2 KEYWORD2
41+
2642

2743
# Constants (LITERAL1)
2844
CORRELATION_LIB_VERSION LITERAL1

libraries/Correlation/library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/Correlation.git"
1717
},
18-
"version": "0.1.3",
18+
"version": "0.1.4",
1919
"license": "MIT",
2020
"frameworks": "arduino",
2121
"platforms": "*"

0 commit comments

Comments
 (0)