Hello!
Long time seeker of knowledge from the forums, the first time I have had to reach out for help. I am trying to work out the standard deviation of an array s_val
and I am following this blog post as upon finding it, I thought I could follow the code, but I am having strange results.
I know I should be posting my whole code, and I will at the bottom of the post but im fairly confident im just not declaring my values as the correct type of numbers, here's the bit im falling over on:
//variables:
float s_val[4] = {1100,1100,1100,1100};
float meanSample;
float sqDevSum = 0.0;
int SAMPLES = 4;
//there is some more code here//
for(int i = 0; i < SAMPLES; i++) {
// pow(x, 2) is x squared.
sqDevSum += pow((meanSample - float(s_val[i])), 2);
Serial.print("s_val - mean squared - ");
Serial.println(sqDevSum);
}
you see I have been printing out the values for sqDevSum
as that's where the system loses the plot, I am expecting very low values for sqDevSum
, the data set is very condensed and the values all very similar while I write the programme, (I plan to ultimately sample data once an hour) so lets say s_val[0]
is 1015.23 and the meanSample
is 1017.76 (I have verified meansample
is correct from an earlier point of the code) that's 1017.76 - 1015.23 = 2.53 and 2.532is 6.4009 so on the first go through the for loop, I would expect to see 6.4009 and three returns of 0 as the s_val
array only has info in s_val[0]
right at the start, however, this is the Serial readout i get:
13:16:52.334 -> 1028.21
13:16:52.709 -> END OF LOOP
13:16:52.709 -> s_val - mean squared - 594684.68
13:16:52.746 -> s_val - mean squared - 660760.75
13:16:52.746 -> s_val - mean squared - 726836.81
13:16:52.783 -> s_val - mean squared - 792912.87
that figure of 1028.21 is the data point that gets shifted into s_val[0]
Im in well over my head, but im pretty sure im just not handling the numbers as they go through the maths as the correct "type" float, int, etc . . . can anyone see why I get such high values in my readout? here's the whole code, it's in a state of being written, and there's a whole bunch of superfluous i2c stuff going on, but my hunch could well be wrong:
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <ds3231.h>
Adafruit_BME280 bme;
ts t; //ts is a struct findable in ds3231.h
float pressureVals[33];
float s_val[4] = {1100,1100,1100,1100};
float var;
float meanSample;
float stDev;
float multiplierCheck;
float pressureSum;
float avgPressure;
float sampleSum = 0;
float sqDevSum = 0.0;
int multiplier;
int SAMPLES = 4;
void setup() {
// -------RTC-------
Wire.begin(); //start i2c (required for connection)
DS3231_init(DS3231_INTCN); //register the ds3231 (DS3231_INTCN is the default address of ds3231, this is set by macro for no performance loss)
// --------BME--------
bme.begin(0x76);
// -------Other-------
Serial.begin(9600);
}
void loop() {
pressureVals[0] = (bme.readPressure() / 100.0F);
for (int i = 32; i >= 0; i--) {
pressureVals[i+1] = pressureVals[i];
Serial.print(pressureVals[i]);
Serial.println();
delay(350);
}
Serial.print("END OF LOOP");
Serial.println();
float sampleSum = 0;
s_val[0] = pressureVals[1];
s_val[1] = pressureVals[3];
s_val[2] = pressureVals[5];
s_val[3] = pressureVals[9];
for(int i = 0; i < SAMPLES; i++) {
sampleSum += s_val[i];
}
meanSample = sampleSum/float(SAMPLES);
for(int i = 0; i < SAMPLES; i++) {
// pow(x, 2) is x squared.
sqDevSum += pow((float(s_val[i]) - meanSample), 2);
Serial.print("s_val - mean squared - ");
Serial.println(sqDevSum);
}
stDev = sqrt(sqDevSum/float(SAMPLES));
Serial.print("sample sum - ");
Serial.println(sampleSum);
Serial.print("Mean of the Samples - ");
Serial.println(meanSample);
Serial.print("sum the squares of the differences from the mean - ");
Serial.println(sqDevSum);
Serial.print("hopefully, a standard deviation? - ");
Serial.println(stDev);
}