I'm streaming data in from a weather station for use within a PD patch, and I can't wrap my head around how to take a fluctuating stream of numbers and then tell whether they are trending upwards or downwards.
Thanks for your help!
-
Identifying an upward or downward trend of a stream of numbers?
-
- bang register for comparison with N
- write N in register and compare N against N-1 (order does not matter)
-
@milestone You can use linear regression for this. I found a simple algorithm here: https://www.edureka.co/blog/linear-regression-in-python/.
And made a patch from it: linear-regression.pd
To see if the values are trending upwards or downwards you just have to see if the slope is positive or negative.
-
You will need something that calculates trend over various samples, as a single fluctuation downwards doesn't mean the entire trend is down. Consider this set: 2, 4, 5, 7, 6, 8, 9 - the trend is obviously rising but @Obineg 's raw solution will detect a falling trend from 7 to 6. I'd use a simple one pole low-pass filter on the data and measure inclination (like @Obineg suggests) on that instead. Illustrative example:
I take it your data is not an audio signal so you will need to implement the one pole low-pass algorithm in the control message domain. Using this formula:
Y[n] = Y[n-1] + (X[n] - Y[n-1]) * T
you can do something like this:msgLP.pd - You need to feed it a periodical bang/clock on the 3rd inlet for anything to happen
Mess around with the T input (don't use values > 1 ) and use @Obineg 's method to measure whether the output of the filter is rising or falling...
-
Here a quick demo of the above: linear_regression_demo.pd
-
aha, i misinterpreted "trend" because i didnt see "weather station"
yes, lowpassfilter would be good, onepole, slide, or averaging could all be set up for numbers in case you need to avoid signal.
then poll and compare with N-1
-
@Obineg This weather based music used to pleasant, but it is getting so extreme these days...