use median instead of average

This commit is contained in:
Joerg Lehmann 2020-01-13 17:29:40 +01:00
parent 730904e6ba
commit 4c021ff34a
2 changed files with 51 additions and 23 deletions

View File

@ -536,13 +536,52 @@ void DoDeepSleep(uint32_t sleep_time)
}
}
//Following functions are based on "https://github.com/dndubins/QuickStats", by David Dubins
long median(long samples[], int m) //calculate the median
{
//First bubble sort the values: https://en.wikipedia.org/wiki/Bubble_sort
long sorted[m]; // Define and initialize sorted array.
long temp = 0; // Temporary float for swapping elements
for (int i = 0; i < m; i++) {
sorted[i] = samples[i];
}
bubbleSort(sorted, m); // Sort the values
if (bitRead(m, 0) == 1) { //If the last bit of a number is 1, it's odd. This is equivalent to "TRUE". Also use if m%2!=0.
return sorted[m / 2]; //If the number of data points is odd, return middle number.
} else {
return (sorted[(m / 2) - 1] + sorted[m / 2]) / 2; //If the number of data points is even, return avg of the middle two numbers.
}
}
void bubbleSort(long A[], int len) {
unsigned long newn;
unsigned long n = len;
long temp = 0;
do {
newn = 1;
for (int p = 1; p < len; p++) {
if (A[p - 1] > A[p]) {
temp = A[p]; //swap places in array
A[p] = A[p - 1];
A[p - 1] = temp;
newn = p;
} //end if
} //end for
n = newn;
} while (n > 1);
}
long my_read_average(byte gain, byte times) {
// highest and lowest value will be ignored
long sum = 0;
long v = 0;
long L = 2147483647;
long H = -2147483648;
long res;
int const num_scale_readings = 100; //number of instantaneous scale readings to calculate the median
// we use the median, not the average, see https://community.particle.io/t/boron-gpio-provides-less-current-than-electrons-gpio/46647/13
long readings[num_scale_readings]; // create arry to hold readings
// highest and lowest value will be ignored
if (config_data.debug_level > 0) {
gCatena.SafePrintf("%010d - my_read_average, measurements: ", millis());
}
@ -551,22 +590,14 @@ long my_read_average(byte gain, byte times) {
// we wait 400ms (settling time according HX711 datasheet @ 10 SPS
delay(400);
for (byte i = 0; i < times; i++) {
// we wait 400ms (settling time according HX711 datasheet @ 10 SPS)
v = LoadCell.read();
if (L > v) L = v; // find lowest value
if (H < v) H = v; // find highest value
sum += v;
if (config_data.debug_level > 0) {
gCatena.SafePrintf("%d ", v);
}
gCatena.poll();
delay(WAITTIMELOADSAMPLES);
for (int i = 0; i < num_scale_readings; i++) {
readings[i] = LoadCell.read(); // fill the array with instantaneous readings from the scale
}
res = (sum - L - H) / (times - 2);
res = median(readings, num_scale_readings); //calculate median
if (config_data.debug_level > 0) {
gCatena.SafePrintf("; average (without highest [%d] and lowest [%d] value): %d\n", H, L, res);
gCatena.SafePrintf("; median of %d samples: %d\n", num_scale_readings, res);
}
return res;

View File

@ -55,10 +55,7 @@ enum {
|
\****************************************************************************/
static const int32_t fwVersion = 20200109;
// wait between samples in milliseconds
const int WAITTIMELOADSAMPLES = 100;
static const int32_t fwVersion = 20200113;
static const byte INIT_PACKAGE_INTERVAL = 100; // send an init package every 100 packages;
static const byte MAX_VALUES_TO_SEND = 8;