Hey guys,
I am quite new at Arduino but already having a lot of fun and advancing a bit.
Anyways... for my project I want to compare several enviroments in terms of light. For that I have a digital light sensor attached to my Arduino which returns serial data in lux.
Well, in order to compare different light enviroments I want to fit the light over time functions to a quadratic curve:
(light in lux) = ax²+bx+c
With x being from 0 to 20.
Why quadratic function? Well I have special light sources and prior experiments without Arduino showed that quadratic fit is quite suitable. Now I want to build a handheld device.
My problem: I found following code here in the forum.
// USAGE: double g = fit_G( sizeof(x)/sizeof(double), t, x);
// should be declared like that to optimize for the 8bits microcontroller core.
double fit_G(int N_points, double px[], double py[]) __attribute__((__optimize__("O2"))); //Remove this one if you're using DUE.
double fit_G( int N_points, double px[], double py[] ) {
int i;
double S00, S10, S20, S30, S40, S01, S11, S21;
double denom, x, y, a, b, c;
S00=S10=S20=S30=S40=S01=S11=S21=0;
for (i=0; i<N_points; i++) {
x = px[i];
y = py[i];
//S00 += 1; // x^0+y^0
S10 += x;
S20 += x * x;
S30 += x * x * x;
S40 += x * x * x * x;
S01 += y;
S11 += x * y;
S21 += x * x * y;
}
S00 = N_points;
denom = S00*(S20*S40 - S30*S30) - S10*(S10*S40 - S20*S30) + S20*(S10*S30 - S20*S20);
/* c = (S01*(S20*S40-S30*S30)-S11*(S10*S40-S20*S30)+S21*(S10*S30-S20*S20))/denom;
b = (S00*(S11*S40-S30*S21)-S10*(S01*S40-S21*S20)+S20*(S01*S30-S11*S20))/denom;*/
a = ( S00*(S20*S21 - S11*S30) - S10*(S10*S21 - S01*S30) + S20*(S10*S11 - S01*S20) )/denom;
double g = a*2;
return g;
}
And I have no idea how to implement that.
I even understand the mathematics behind it, but I have no clue how I should tell my Arduino to declare x as 0 to 20 and y as my serial data and then fit the curve.
Aim: I want to receive different a,b,c for different enviroments and compare them.
Thank you all already for the answers!
I'm excited to hear them.