08-08-2018 07:22 PM
I am reading the analog input port, all 8 bits. The voltage jumps around, and I would like to take the average of ten readings. I can't seem to figure this out. I thought I could decimate the array and then do something, but I cannot figure it out. help...
LeatherNeck
Solved! Go to Solution.
08-08-2018 08:32 PM
I don't understand what you are doing. First, you seem to be using some third-party hardware, as there are a number of ulx functions that are not DAQmx.
I don't understand what you mean by "8-bits", nor do I understand why you are taking samples one at a time, and appear to be averaging across channels (instead of across time). [There is nothing "wrong" about averaging across channels, but when trying to "average out noise", it is much more common to average N sequential samples on a single channel together].
Here are some ideas. Assume there's some "noise" in your system, and that you have 8 channels of data whose value you want to sample at 100 Hz. Let's also assume that the 8 channels are measuring 8 unchanging voltages, but that there is a fair amount of high frequency noise on these channels.
Another advantage of setting up the A/D to sample multiple samples (instead of single samples) is that you can take advantage of the (typically more accurate) timing of the DAQ hardware, compared to the timing of PC "loops". If paired with a Producer/Consumer Design, you can simultaneously acquire, average, and plot without losing any data and maintain sampling timing accuracy.
Bob Schor
08-09-2018 04:38 AM
If I understand correctly, you simply want to make a moving average for each of your channels? That's not that difficult. There are two options: 1) True moving average, or 2) Exponential Moving Average.
For the first you simply store the last 10 values in an array and output the average value. For N channels you can store it in a 2D array of size Nx10. It gives you the true average, but at the cost of having to rebuild the array upon each iteration.
The Exponential Moving Average simply works by adding the newest sample with a weight coefficient: newAverage = (1 - a) * oldAverage + a * newSample. You can set the "length" by tuning the alpha-parameter (see the link above).
Advantage is that it's fast, it only stores one value per channel. The disadvantage is that it is not a true average and large disturbances might be preserved for longer than 10 samples.
08-09-2018 07:28 AM
Thank you for your support. I think my post was a little confusing in that I used the term "bits". I meant the analog channels as you correctly assumed. Ok, good info. I'll follow that line to see what I can learn.
Regards,
LeatherNeck.
08-09-2018 07:41 AM
Good to hear. I think the EMA is very elegant, though not perfect. I often use it to assess if a process, like a temperature controller, has settled to its setpoint value.
Let us know if you have questions on implementing either of the options!
08-10-2018 05:01 AM
@irPaul wrote:
If I understand correctly, you simply want to make a moving average for each of your channels? That's not that difficult. There are two options: 1) True moving average, or 2) Exponential Moving Average.
For the first you simply store the last 10 values in an array and output the average value. For N channels you can store it in a 2D array of size Nx10. It gives you the true average, but at the cost of having to rebuild the array upon each iteration.
I'm not sure what you mean by the "cost of having to rebuild the array upon each iteration". Build a "ring buffer", an array of length 10, pre-initialized to 0. As the i-th point comes in, it is stored in array position (i mod 10) (use the Remainder on Quotient/Remainder) and you return the mean of the Array. Here's a Snippet demonstrating its effect on a random number generator --
Bob Schor