Using an Arduino UNO and a relative humidity sensor... Got all that working fine, but am in need of some help analyzing the data that is now in my array... I am looking to get the mode of the 60 samples I'm currently storing... The sensor is a HTF3223 which provide a frequency output based on its measurement... I am storing those measured frequencies into my array. Finding the mode of the array will help me filter out the outlying samples that were either to low or two high (for some reason)...
I have tried using the Average library, but I cannot get it to compile on Arduino 1.8.1 to even use their example sketch... So the question is, is there a trick to get the Average Library to work, or is there a better way to derive the mode of my data set...
Found the Average library here, but I only can find the .h file:
I'd be tempted to use the mean for that, ie compute mean, screen outliers and compute mean
again. The mode can be wildly out for small datasheets due to random fluctuations, and 60 is
perhaps a small dataset?
Copy the array to a scrap area.
Sort it.
Iterate through it. Keep a count of how many identical values you see so far. If you see more identical values than your previous best count, that is your new mode.
int[60] sortedArray; // this array must be sorted!!
int modeCt = 0;
int modeV = -1;
int ct = 0;
int v = -1;
for(int i = 0; i< 60; i++) {
if(sortedArray[i] != v) {
v = sortedArray[i];
ct = 0;
}
ct ++;
if(ct > modeCt) {
modeCt = ct;
modeV = v;
}
}