stevieb has asked for the wisdom of the Perl Monks concerning the following question:

Hello there fellow Monks!

I'm here once again to ask for a review of some mathematical code that I'm working on. I want to see if this is a reasonable approach to the problem I'm facing.

I'm building a grow environment controller which is working great, but I periodically get a spurious reading from the hygrometers.

To help combat this, I've implemented an exponential weighted moving average routine on the sensor's readings, but now I want to proceed further, and before running the EWMA, I want to literally remove any erroneous readings that are +/- 15% of the average of the last 10 readings.

Although this code will be translated into C++ code, I prototype in Perl first, and below is my quickly written testing code. It seems to work quite well, but I'm hoping our mathematicians can have a look at whether there are better or more efficient/reliable ways to do what I'm hoping to do.

use warnings; use strict; use feature 'say'; my $limit_percent = 0.15; my @nums = ((75) x 5, 99, (75) x 4); # 10 numbers filtered(\@nums); for (qw(75 74 75 90 74 75 7)) { # shift off oldest number, push the next new one shift @nums; push @nums, $_; say "Added: $_"; filtered(\@nums); } sub filtered { my ($nums) = @_; my $sum; $sum += $_ for @$nums; my $avg = $sum / scalar @$nums; my $range_limit = $avg * $limit_percent; my @filtered_nums; for (@nums) { if ($_ > $avg + $range_limit || $_ < $avg - $range_limit) { say "Removing: $_"; next; } push @filtered_nums, $_; } my $filtered_avg; $filtered_avg += $_ for @filtered_nums; return if ! scalar @filtered_nums; $filtered_avg = $filtered_avg / scalar @filtered_nums; printf( "Filtered %.2f, Avg %.2f, Limit: %.2f\n\n", $filtered_avg, $avg, $range_limit ); }

Output:

Removing: 99 Filtered 75.00, Avg 77.40, Limit: 11.61 Added: 75 Removing: 99 Filtered 75.00, Avg 77.40, Limit: 11.61 Added: 74 Removing: 99 Filtered 74.89, Avg 77.30, Limit: 11.59 Added: 75 Removing: 99 Filtered 74.89, Avg 77.30, Limit: 11.59 Added: 90 Removing: 99 Filtered 76.56, Avg 78.80, Limit: 11.82 Added: 74 Removing: 99 Filtered 76.44, Avg 78.70, Limit: 11.80 Added: 75 Removing: 90 Filtered 74.78, Avg 76.30, Limit: 11.44 Added: 7 Removing: 90 Removing: 7 Filtered 74.75, Avg 69.50, Limit: 10.42

For those interested, I've got five indoor grow areas/environments. One for clone cuttings, one for plants in vegetative state, one for flowering/blooming state and I just started my hand at growing mushrooms, so I've got a cabinet for that as well. Each area is managed by either a Raspberry Pi, Arduino Pro Mini, or ESP8266 NodeMCU-12e.

You can see the dashboard of where I'm at right here.

Replies are listed 'Best First'.
Re: Combining Exponential Moving Avg with Avg Filtering
by bliako (Abbot) on Mar 15, 2020 at 17:09 UTC

    I have one observation: you do not need to keep calculating the sum every time a reading is added or removed:

    $mean = (1+2+3+4) / 4; $newreading = 5; $remove = 1; $newmean = $mean + ($newreading - $remove) / 4;

    In this way you can have subs add_reading() and remove_reading() which also recalculate the mean without worrying about redandant summations.

    Secondly, since you are dealing with infinite data, perhaps you can benefit from B.P.Welford's method of "running statistics". This is a simple method to keep a mean and lots of other descriptive statistics (e.g. standard deviation) without ever storing any readings at all! Ingenious yet simple. And you are lucky because last time I looked CPAN had 3 such modules Statistics::Welford, Statistics::Running and Statistics::Running::Tiny (disclaimer last 2 modules by myself). If you want that in C (via C++) have a look at John Cook's blog

    You may find this approach useful especially if you will need higher-order statistics like standard deviation etc. because you will find that the moving average is not good enough for detecting outliers and you need to add more flexibility to your model.

    1 minute EDIT: however, if your running mean varies during the day or the seasons, then keeping a "running" mean since the beginning of time is not right! In which case a model with more input parameters (like time of day) can assess whether a reading is an outlier (over this very recent time). If that's the case, at least you have the first observation, you don't need to re-do the sums for a moving average.

Re: Combining Exponential Moving Avg with Avg Filtering
by karlgoethebier (Abbot) on Mar 15, 2020 at 18:14 UTC
    ...growing mushrooms...

    A(nother) kind of creative engineering. Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      The wife wants to try micro dosing. Figured adding a small box to the operation to learn how to grow something new was a good idea :)

Re: Combining Exponential Moving Avg with Avg Filtering
by Ea (Chaplain) on Mar 17, 2020 at 15:40 UTC
    While not what you asked, astronomers deal with spurious readings by using the Median which is less susceptible to a few wild readings. Statisticians say that this is a "noisier" statistic, but it's probably acceptable for most of us.

    Ea

    Sometimes I can think of 6 impossible LDAP attributes before breakfast.

    Mojoconf was great!