LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

average value from analog input

Solved!
Go to solution

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

Mark R Rivet
0 Kudos
Message 1 of 6
(5,045 Views)

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.

  • Acquire 1 sample from 8 channels every 0.01 sec.  Plot the 8 channels.  Each sample will reflect the "constant" value of the channel plus the noise.
  • Acquire 10 samples from 8 channels every 0.001 sec.  Average these 10 samples -- you now have 8 averaged estimates of the 8 DC values sampled over 10 * 0.001 sec = 0.01, just like the previous example, but with a lower noise value.

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

 

 

0 Kudos
Message 2 of 6
(5,025 Views)
Solution
Accepted by topic author LeatherNeck

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.

 

 

0 Kudos
Message 3 of 6
(5,002 Views)

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.

Mark R Rivet
0 Kudos
Message 4 of 6
(4,988 Views)

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!

0 Kudos
Message 5 of 6
(4,983 Views)

@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 --

Moving Average.png

Bob Schor

0 Kudos
Message 6 of 6
(4,963 Views)