Page 1 of 1

Moving average

Posted: Tue Aug 01, 2017 3:49 pm
by vanatteveldt
This is nog an original creation, but the post that it is based on (viewtopic.php?t=29679#p190249 and https://www.reddit.com/r/factorio/comme ... _included/) did not contain a detailed explanation or a .15 blueprint string, so I thought some people might find it useful.

Goal: give a moving average of a measurement like belt throughput, i.e. smooth or dampen the input signal so it doesn't fluctuate too much

Theory
theory
Overview:

This can be implemented in 3 combinators only, but I use slightly more to make it more understandable:

Image

This displays the throughput of the belt as items/minute, counter courtesy of DaveMcW (viewtopic.php?f=193&t=19825&start=60#p282391)

All red wires are control, green wires are content

Implementation:

The main work is done by the central combinator, which embodies the main formula explained above: V_new = V_old - V_old/S + Input = V_old - V_old*N + Input

The addition is implemented by putting all components together on the input side.
- V_old is its own output (of the previous tick)
- V_old * N is the output of the bottom combinator (memory decay)
- Input is the V output of the top combinator (input)

Thus, every tick calculates a new version of V to which input is added and a fraction of the previous output is removed. The resulting variable V can be seen as the exponential average times the smoothing factor.

The combinator settings itself are the simple 'memory cell' / counter, where the output is copied to the input unless the red reset signal is nonzero:

Image

Input: The topmost combinator scales up the input value (everything) and outputs it as V (for value):

Image

Memory decay

The bottom combinator divides the current value of V by N, which is the negative value of the smoothing factor (so -100 if the smoothing factor is 100):

Image

N is calculated by multiplying the constant value S by -1 in the constant and arithmetic combinators on the left hand side:

Image
Image

(the red signal is the reset signal: set to 1 to reset the counter, set back to zero to resume counting)

The final combinator scales down the value by dividing by the smoothing constant S:

Image

In this case it ouputs to A because the counter wants its input on A, but of course you can put it on whatever you want.
0eNrtXdtu2lgU/RdL8zKCmXO/oM5DfqAvleZlVCEHnMYSNxlTTVXx72NDk8AxOz6LQKdHQZUaBcL28dqXtfdZtvme3c82xaoqF3U2+p6Vk+VinY3++Z6tyy+LfNa+Vn9bFdkoK+ting2yRT5vf8ursn6cF3U5GU6W8/tykdfLKtsOsnIxLf7NRnw76LWxnuez2XCWz1cHHxTbz4OsWNRlXRb7lex++TZebOb3RdVYfv78Q76uh3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mjrp1wElENTTJsrOMRUAgC14yBu8vGgOxmQfACdYGJ04iMeM2gcJcNChmNgEEQsMkQtNPRaWERddKAd6YFi6AuKHYOWQTEiqQi6jyiB6bDirqzs031Ip4hCCTEimFf4Km+wHNED0yIFeMREAgC6bCi590sCF6gKqKXiECYDisaHo2AQhBIhxW9jE4LDQiPBryJLFgD1Zt4A6wB4kRS6/QWkPrSoUQTNiKeakQ8cidfOozobWe/hmwKkHv30mFEE4/A7vuIoiFIiBJ9NwtCTOivQOKAAJgQJ3oAAgFAkA4ptpJnfGpIQFs04F1f4SrIx7EzBazCYmCQzxNnGhD00uFGKzu4k1/fwQwAQTrs2EqYIQbk12EwC4h66fCjRSBwAATp8GMrY3ZTIXxCK10aPSD1pcOQ1sZDwBkAQUoM6Ynk+DzYfw3z6OCbnwfZ16Ja778JTClhtGrqi9lu/wNjgFv0
Compact version

Here is a more compact version for 'copy paste' use. Input is anything on the green signal to the topleft; output is set on the bottom right, here A on the red signal:

Image

(note that instead of a red reset signal this has a green control signal so you can reset by simply switching off the combinator; also, I manually calculate N=S*-1 to get rid of one combinator)
blueprint

As said above, none of this is original, but I hope the explanation and blueprint string might be of use. If there are any mistakes or inefficiencies please let me know!

Re: Moving average

Posted: Tue Aug 22, 2017 5:26 am
by regular_human
Excellent post, very informative. Thank you!

Re: Moving average

Posted: Wed Feb 21, 2018 12:44 pm
by AndrewIRL
This is exactly what I needed and explained in detail such that I was able to modify it to get it to do what I wanted (smoother).

Very nice. Thanks for the current version blueprints.

Re: Moving average

Posted: Wed Feb 21, 2018 4:48 pm
by vanatteveldt
Thanks both, glad you found it helpful! I learn a lot from all the posts on this board, so it's good to contribute something :)