# Array averaging

## Recommended Posts

I am trying to create a code section that will take a 1D array and create a moving average array.  Sorry if this is a bad description.  I want to take x elements of the input array, average them, and put that average in the first element of a new array.  Then take the next x elements, average them, and put them as the second element of the new array.  I want this done until the array is empty.

I have two possible ways to do it, but neither are running as fast as I wanted them to.  I want to see if anyone knows of a faster way to conduct this averaging.

Thanks

Joe

##### Share on other sites

That's not quite a moving average, rather a down-sampling - i.e. Filtered Array is shorter than Input Data.  Your first solution is pretty good - just speed it up with a Parallel For Loop.  An alternative is to reshape into a 2D array.  Both these come out roughly the same speed, about 5x faster on my machine than your solutions above.

If you want a true Moving Average (where the result is the same length as the original) I think this suggestion from the NI forums using an FIR filter is nice and simple, although you might look carefully at the first Num values if that's important.

Edited by GregSands
##### Share on other sites

You have to be careful with the FIR filter the way you're using it because it applies a shift to your data by the amount of "Num of Averages".

To fix that, you have to do some padding at both ends of your input data. In this example I just repeat the first and last value:

If you run it through a graph it becomes obvious:

##### Share on other sites

Hadn't thought of using a FIR filter.    I bench marked a mean method and your FIR method. The FIR method was ~8-9x faster on my system than the mean with standard for loop, and the FIR was ~2-3x faster with the mean method for loop set for parallelism.

##### Share on other sites

I don't have LabVIEW installed on this machine and I may be confusing LabVIEW and MATLAB primatives, but I've seen a pretty fast approach that relies on a cumulative summation.  Textually, the algorithm is:

B=CumulativeSum(A)

SlidingAverage(i)=(B(i+windowsize)-B(i))/windowsize

Adjust indices as necessary to center your window in the right place and be careful with the edge conditions.

##### Share on other sites
• 2 weeks later...

Thanks for your help.  I was able to parallelize the for loop with the mean in it and it helped speed it up.  I didn't try the FIR approach.  I'm still waiting on verification that the averaging will work for the customer.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
• ### Similar Content

• By hooovahh
View File Hooovahh Array VIMs
Here is the Hooovahh Array VIMs.  This initial release contains 14 VIMs for manipulating array data, which are intended to replace OpenG functionality, but with the added benefit of data type propagation, and increased performance using newer array manipulation techniques.  In later versions other Array manipulation functions were added moving all the OpenG stuff to their own palette.
Most of the OpenG functions are unchanged, but a few use the newer conditional and concatenating tunnels.  And a few functions have added performance based on other inputs.  For instance the Delete Array Elements can operate in a more efficient way if the input indexes are already sorted.  The Filter 1D array can also be more efficient if the input is known to not contain any duplicates.
Because these packages contain VIMs, they require LabVIEW 2017 or newer.  Having these functions be VIMs mean all functions work with various array data types.  Included functions are:
Conditional Auto-Indexing Tunnel Delete Elements from (1D or 2D) Array Filter 1D Array Index (1D or 2D) Array, Scalar, Row, Column Remove Duplicates from 1D Array Reorder (1D or 2D) Array Reverse 1D Array Slice 1D Array Sort (1D or 2D) Array Convert 1D to 2D Convert 2D to 1D Find Subarray Force Array Min/Max Size Foreign Key Sort Submitter hooovahh Submitted 10/11/2017 Category *Uncertified* LabVIEW Version 2018 License Type BSD (Most common)
• By hooovahh
Here is the Hooovahh Array VIMs.  This initial release contains VIMs for manipulating array data, which are intended to replace OpenG functionality, but with the added benefit of data type propagation, and increased performance using newer array manipulation techniques.  In later versions other Array manipulation functions were added moving all the OpenG stuff to their own palette.
Version 2.0 changed the suffix naming standard.  Updating may mean replacing calls to the new versions since the name on disk has changed.  This was for consistency and I'm sorry for breaking compatibility.  The added type defs in 2.0 may break compatibility too but these help avoid code breaking bugs since VIMs allowed any data type previously.
Most of the OpenG functions are unchanged, but a few use the newer conditional and concatenating tunnels.  And a few functions have added performance based on other inputs.  For instance the Delete Array Elements can operate in a more efficient way if the input indexes are already sorted.  The Filter 1D array can also be more efficient if the input is known to not contain any duplicates.
Because these packages contain VIMs, they require LabVIEW 2017 or newer.  Having these functions be VIMs mean all functions work with various array data types.  Included functions are:
Conditional Auto-Indexing Tunnel Delete Elements from (1D or 2D) Array Filter 1D Array Index (1D or 2D) Array, Scalar, Row, Column Remove Duplicates from 1D Array Reorder (1D or 2D) Array Reverse 1D Array Slice 1D Array Sort (1D or 2D) Array Convert 1D to 2D Convert 2D to 1D Find Subarray Force Array Min/Max Size Foreign Key Sort
• By WILSTi
Hello,

I'm developing an application to play "Battleship" with another opponent through serial communication. I currently have the following difficulty:
I have not been able to fill the Array with my opponent's ships, I can modify a position, but by changing the position, the result of the previous position returns to its original value.
VI operation:

For my turn, using the radio buttons, I select the coordinate and press the SEND button. My opponent answers me with a 1 if I hit or with a 0 if I failed, this data is what I couldn't keep in the array.
Best regards,
3Q.vi
• By Chen Li
The program obtains the number of messages in the hardware buffer through ZCAN_GetReceiveNum function, and then calls ZCAN Receive to complete the received data. When the amount of data reaches 1000 frames / s, the program will crash.

I think there is something wrong with creating the parameter array ZCAN Receive Data* pReceive. Has anyone ever encountered a similar problem?

• I have never gotten the performance that I desire out of the 2D picture control. I always think that it should be cheaper than using controls since they don't have to handle user inputs and click events etc. But they always seem to be slower.
I was wondering if any of the wizards out there had any 2d picture control performance tips that could help me out?
Some things that come to mind as far as questions go:
Is the conversion to from pixmap to/from picture costly?
Why does the picture control behave poorly when in a Shift register?
What do the Erase first settings cost performance wise?
Anything you can think of that are bad ideas with picture controls?
Anything you can think of that is generally a good idea with picture controls?
×