I’m building a patch that calculate the average of multiple sensor readings to reduce input noise. I started with a pulse-on-change node and I thought that if I convert it to boolean I can hook up an if-else and proceed with the calculations. However I couldn’t find any way to do that. Thanks in advance
Have you considered using the flip-flop node?
You may find it helpful to take a look at this discussion on calculating a moving average:
Converting pulse to a boolean really depends on what you are actually doing. Pulse is an instantaneous event. Boolean is a steady-state value.
If you only need to know if there has ever been a pulse, then flip-flop will do the trick. Probably in most cases, you will need some way to reset the flip-flop to restart the process and wait for the next pulse.
Are you just waiting until a minimum number of values have been read for your running average? You could pulse a counter & get a boolean by comparing count output to the minimum # of values you want. (If it can run for a long time, you may have issues with count over-flowing the max value and either restarting from zero or going negative. You could block pulse to count once your output has switched to true.)
I suspect there are plenty of other instances where the correct answer is “Don’t start with a pulse…you are doing it wrong.”
In your case, if you are trying to reduce noise, pulsing-on-change doesn’t really help since it will pulse for every change, so it doesn’t eliminate any noise. To get a running average, the simplest solution is probably to add a buffer for each value you want to average. When a new value comes in (pulse-on-change), update the last buffer with value from previous buffer, then update prev with value from the one before that…etc. until all have been updated. I would run pulse-on-change to last buffer, then go through defer node to next, then another defer node to next, on down the line to make sure buffers update correctly. Output from buffers can now be added together & divided by # buffers. The gweimer/utils/running-avg does all this for you, but it is not currently working in XOD 0.31.1.
Thanks for the detailed answer, I have tried to implement your second solution but XOD reported an error:
Loops detected
The program has a cycle
Use xod/core/defer node to break the cycle
I understand that I’ve created a loop, although from my past experiences with programing loops are fine when they are breakable (mine can in theory) and this defer node suppose to help me do that? I don’t really understand how this node works from the brief explanation of the docs and it seems important for this problem.
hi, use defer node for loop
205-loops
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.