LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

moving average question

I have a 500 data point 1-D array. I passed the array to a loop with Mean PtByPt.vi inside to get a moving average of 10 data points.  

Moving average.PNG

Would someone please explain how the loop handles initial and final data. If the first 10 data are used to get the first moving average, and the last 10 data are used for last moving average, the output of the loop would have less than 500 data points, correct? 490 data points to be exact?

 

0 Kudos
Message 1 of 13
(785 Views)

The for loop does not care about the SubVIs inside it. You have doubly constrained it with the auto indexing tunnel though. It will run either 500 times, or the length of the array, whichever is less. 

 

As far as how Mean PtbyPt behaves, you can just double click it and look inside. The first iteration it will have 1 sample, x0. The mean will be x0/1. It will continue to add samples until you have 10 samples. On the 11th run, it will get rid of the oldest sample. On the 500th run, it will use samples 490 through 499, if it is not stopped earlier by the auto indexing tunnel.

Message 2 of 13
(783 Views)

@Gregory wrote:

The for loop does not care about the SubVIs inside it. You have doubly constrained it with the auto indexing tunnel though. It will run either 500 times, or the length of the array, whichever is less. 

 

As far as how Mean PtbyPt behaves, you can just double click it and look inside. The first iteration it will have 1 sample, x0. The mean will be x0/1. It will continue to add samples until you have 10 samples. On the 11th run, it will get rid of the oldest sample. On the 500th run, it will use samples 490 through 499, if it is not stopped earlier by the auto indexing tunnel.


1. Doubly constraining auto indexing tunnel: Would it be better if I remove constant 500? If so, will the loop be completed when elements are exhausted?

 

2. How Mean PtByPt behaves: Thank you for the explanation. Now I understand.

 

Thank you.

 

2. 

0 Kudos
Message 3 of 13
(775 Views)

@knowlittle wrote:

1. Doubly constraining auto indexing tunnel: Would it be better if I remove constant 500? If so, will the loop be completed when elements are exhausted?


Yes and Yes


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
0 Kudos
Message 4 of 13
(768 Views)

 

In addition to what has been said, since you know that the array has 500 points and you probably want to average the entire array, irrespective of size, delete the constant wired to N. The autoindexing tunnel will automatically know how many times to loop based on the array size.

 

If this is part of code that runs repeatedly, you need to decide if you should initialize the ptbypt function on the first iteration of the FOR loop. If you don't, it will continue averaging from the last data on later runs of the FOR loop, why may or may not be desirable.

 

(If you want to stick with dynamic data, there is also an express VI that can do about the same [smoothing ... moving average ... rectangular ... half-width=5]. Personally, I rarely use express VIs)


LabVIEW Champion. It all comes together in GCentral GCentral
0 Kudos
Message 5 of 13
(767 Views)

I have a followup question. The averaged 500 data array was connected to Waveform Chart. And I got this. What I cannot explain is the initial quick fall. Let me explain what I did. 

 

Input: -1.5 V to 1.5V DC (1 alkaline battery and a variable resistor with center tap)

USB board: NI USB 6210

 

Adjusted variable resistor to -390 mV. Hit RUN. Adjusted variable resistor to -460 mV. Hit Run. The chart seems to start at the previous voltage and quickly changes to the new. Would someone kindly explain this strange behavior? Thank you.

moving average waveform chart 2.PNGmoving average waveform chart.PNG

0 Kudos
Message 6 of 13
(746 Views)

@altenbach wrote:

 

If this is part of code that runs repeatedly, you need to decide if you should initialize the ptbypt function on the first iteration of the FOR loop. If you don't, it will continue averaging from the last data on later runs of the FOR loop, why may or may not be desirable.

 


See altenbach's explanation above

0 Kudos
Message 7 of 13
(738 Views)

If you intialize it on the first iteration then you should stop seeing the data from the previous call.

 

Capture.PNG

 

Although if you are seeing this just from using the run arrow then it might be samples buffered from your DAQ instrument?

0 Kudos
Message 8 of 13
(736 Views)

@altenbach wrote:

 

In addition to what has been said, since you know that the array has 500 points and you probably want to average the entire array, irrespective of size, delete the constant wired to N. The autoindexing tunnel will automatically know how many times to loop based on the array size.

 

If this is part of code that runs repeatedly, you need to decide if you should initialize the ptbypt function on the first iteration of the FOR loop. If you don't, it will continue averaging from the last data on later runs of the FOR loop, why may or may not be desirable.

 

(If you want to stick with dynamic data, there is also an express VI that can do about the same [smoothing ... moving average ... rectangular ... half-width=5]. Personally, I rarely use express VIs)


I posted too hastily. As you advised, I initialized at each loop entry. I am about to connect this program to a real test setup. Thank you all so much.

moving average initilized.PNG

0 Kudos
Message 9 of 13
(733 Views)

Putting a constant outside the loop doesn't mean that it only takes effect on loop entry, it means the same value will be used every iteration of the loop. Right now, you are initializing every loop and taking the average of the most recent (single) element, so it's the same as if you weren't doing any averaging at all 🙂

0 Kudos
Message 10 of 13
(724 Views)