hi guys

i got a problem with my hc12 and a calculation...

what i wanna to do: do a correlation of 2 signals

i found and codesnippet in an book for c# and hooked it over to my controller:

int i = 0;float sumx = 0.0;float sumy = 0.0;float sumxx = 0.0;float sumyy = 0.0;float sumxy = 0.0;float rho = 0.0;for(i = 0; i < length; i++){ sumx += x[i]; sumy += y[i]; sumxx += x[i] * x[i]; sumyy += y[i] * y[i]; sumxy += x[i] * y[i]; }rho = ((length * sumxy) - (sumx * sumy)) / (sqrtf(((length * sumxx) - (sumx * sumx)) * ((length * sumyy) - (sumy * sumy))));

the problem starts right at the beginning by calculating sumxx...

my x-array contains: { 1, 4, 2105,.....}

stepping through the function gives the sumxx values { 1, 17, -25406, ...}

the first two values are correct, but why does the third cracks up? should be something like 4431042... or something like 4.43102E06

because of the values the correlation becomes inverted, sometimes completely wrong and sometimes greater 1 :/

i got another realization of a correlation implemented which calculates correctly with same values but is far more runtime unfriendly. there the values go up toe 8,xyzE07 without a problem...

whats the problem with summing up floats that way?

First of all, if you want to be helped next time, please provide at least all necessary declarations. Why should I guess what type is your x array? Compileable small snippet could be even better. It would dramatically ease helping you.

I guess x[] array is array of int's or short's. If so, then no wonder integer 2105 * integer 2105 overflows. Integer 2105 * 2105 is chopped to 0x9CB1 (-25423). Next -25423 is converted to float and added to sumxx.

To fix it change your expressions like this

sumxx += (long)x[i] * x[i];

or this

sumxx += (float)x[i] * x[i];