LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How Moving average is calculated

When I use the smoothing filter express VI in Labview and select a moving average and half width of 10, how does LV treat the first 10 data points. Does it simply start at data element 11?

 

I have a client saying they are getting different results in Excel compared to the output from LV, and I would like to explain how LV treats the first data elements

Here are my settings

filter.png

Message 1 of 7
(4,851 Views)

Hi CastleWorks,

 

as you show the config dialog of an ExpressVI: why don't you configure this VI as needed and then look into its block diagram by opening it with a right-click?

 

Btw. Why don't you use the much simpler PtByPt-Mean function?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 7
(4,838 Views)

I suspect that the array of data is initialized a preset size and then filled in as points are added.  Until the array is full, the average value will be affected by whatever value the array was initialized with.  Probably zero.  A way around that is to use a lossy queue.  Initialize the queue to a preset size and then use a lossy enqueue to add data.  The queue size will grow up to its max size and then elements will drop off as new ones are added.  To get the average, simpy use the Get Queue Status with Return Elements set to true to get all of the values.  Add them up and divide by the current size of the queue.  I have an example in LabVIEW 2016 if you want to look at it.

CLA
0 Kudos
Message 3 of 7
(4,807 Views)

In the end, the Express VI just uses the IIR filter, which is a dll call to handle the full calculations.  So I would say the best way to find out is to make up some data (like all 10s) and see what the output looks like.

 

I agree with GerdW, the Mean PtByPt would be a lot simpler to use, does not depend on the evil Dynamic Data Type, and is likely to match what is being done in Excel.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 4 of 7
(4,801 Views)

The difference is how you treat border data that are undefined. It will affect transition in the end and beginning

Filter expressVI window = specified half width +1  = 21 in your case. It is not centered, array initialized with zeros (or data from previous run without reset input!). So first point is an average of array of all zeros except first element. In crossrulz's example

Out1 = sum([10, 0, 0...0])/21 = 10/21 = 0.4761.

Mean PtbyPt averages first points with increasing window, that is why it starts from 10 with constant signal.

Out1 = [10] / 1 = 10. Out2 = [10, 10] / 2.

It will cause higher noise while window is accumulating.

 

Disclaimer: This information is an attempt to explain the operation of particular expressVI and in no way encourages or discourages the use of express VIs in general. 😃

Message 5 of 7
(4,785 Views)

On the topic of running average filters...  here is what I use.  It is an iterated running average filter that doesn't "drop off" at the boundary conditions.  By running the filter both directions you prevent phase shift from being introduced into your signal by the filter.  Running both directions will also smooth the data around the boundaries.  While it won't be quite as good as the other data it is much better than just starting with the growing array like PtByPt does.  Using a smaller window and multiple iterations will help preserve waveform shape around peaks while still reducing noise.

 

It is unfortunately more computationally intensive than a simple RA filter.

IRA Filter.png

0 Kudos
Message 6 of 7
(4,779 Views)

If you think about what you are doing from a perspective of Signal Analysis, you are basically creating a Low Pass Filter.  Signal Analysis tends to deal with either "infinitely-long signals" (where you can effectively "ignore" the ends) or "strictly periodic signals" where there is continuity of the signal and a few of its derivatives at the end/beginning junction (i.e. it would continue "smoothly" if you added another copy at the end).

 

It is an interesting exercise to work out the filter characteristics of the various smoothing algorithms, which I did once (quite a few years ago) and remember some vague details.  One way to understand the phase shift introduced by an n-point moving average is to say "If I'm averaging over points 0 .. 10, I'm getting a symmetric estimate about point 5", leading to a phase shift (a lag or lead of 5, depending on which end you reference).  I vaguely recall that the gain component was a cosine bell (this might be wrong -- exercise left for the reader).  When you do two-way (up and back) averaging, the phase differences cancel out, and the gain goes as a squared (whatever is the correct function for the one-way smooth).  

 

Bob Schor

0 Kudos
Message 7 of 7
(4,755 Views)