Page 1 of 1

How to sample a signal based on a clock?

Posted: Thu Oct 22, 2020 6:03 pm
by skydivertricky
So I have combinators set up so I have a counter running.
I have another clock (At different frequency) and I want to sample the counter value on this 2nd clock.
How can I do this with combinators? Is it possible?

What Im trying to do is work out if a number is increasing or decreasing over time. to do this I need to sample a signal at a given point in time so that I can compare it to the previous.

Re: How to sample a signal based on a clock?

Posted: Thu Oct 22, 2020 7:30 pm
by DaveMcW
Multiply the counter by -1. Output this as the same signal on the opposite color wire as the counter.

Connect both wires to a decider combinator. If the value is positive, the signal is increasing. If the value is negative, the signal is decreasing.

Re: How to sample a signal based on a clock?

Posted: Thu Oct 22, 2020 9:39 pm
by skydivertricky
This works if the value changes rapidly. But Im trying to monitor accumulators. The values are pretty static, so I need to know whether its been increasing/decreasing over a period of several ticks.

Re: How to sample a signal based on a clock?

Posted: Thu Oct 22, 2020 9:49 pm
by eradicator
Build two rolling-average contraptions with different time spans (i.e. 1sec and 10sec). Then compare those. If the 1sec is larger than the 10sec the value is increasing. Depending on the fluctuation strength of the signal you might be able to use the origin directly instead of the 1sec average.

Re: How to sample a signal based on a clock?

Posted: Fri Oct 23, 2020 6:33 pm
by Yoyobuae
skydivertricky wrote:
Thu Oct 22, 2020 9:39 pm
This works if the value changes rapidly. But Im trying to monitor accumulators. The values are pretty static, so I need to know whether its been increasing/decreasing over a period of several ticks.
As mentioned by DaveMcW the easiest way to convert a varying signal into a signal which represents the rate of change is doing something like this:



The arithmetic combinator (and the way it is wired) calculates the difference between the value of the signal in the previous tick with the value of the signal in the current tick. If the result is positive the signal is increasing, and negative if it is decreasing. In other words it calculates the first difference of the signal (if the signal was continuous, it would be the first derivative).

Now the signal from accumulator changes rather slowly, remaining constant for many ticks before changing. In the above example I used a storage tank instead because the signal from a storage tank changes more quickly, usually once per tick (which makes it easier to demonstrate the "rate of change").

Luckily we can further process the rate of change signal of an accumulator to detect charging/discharging:





In this example I added a SR latch to hold last state of the accumulator, either charging/discharging. It doesn't tell you by how much it is charging/discharging though (or if the charge is constant).

If you actually want a more detailed signal then just "charging/discharging" then you use a filter on the output of the arithmetic combinator using a rolling average circuit to get a pretty accurate rate of change signal (the number shows the charge/discharge rate of the accumulator in kW):