Moving average

This board is to show, discuss and archive useful combinator- and logic-creations.
Smart triggering, counters and sensors, useful circuitry, switching as an art , computers.
vanatteveldt
Filter Inserter
Posts: 924
Joined: Wed Nov 25, 2015 11:44 am
Contact:

Moving average

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:

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:

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

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):

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

(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:

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.
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:

(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!

regular_human
Manual Inserter
Posts: 4
Joined: Wed Apr 05, 2017 12:50 am
Contact:

Re: Moving average

Excellent post, very informative. Thank you!

AndrewIRL
Fast Inserter
Posts: 225
Joined: Fri Mar 24, 2017 2:17 pm
Contact:

Re: Moving average

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.

vanatteveldt
Filter Inserter
Posts: 924
Joined: Wed Nov 25, 2015 11:44 am
Contact:

Re: Moving average

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

Who is online

Users browsing this forum: No registered users