From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Simple Moving Average VI

Solved!
Go to solution

@altenbach wrote:

Your code still makes no sense. SInce you are calling the subVI one scalar at a time, you are not getting what you want because the shift register only rememebers the last N scalars, no matter what channel it is from. Your code is still very inefficient and convoluted. (e.g. why are you still using insert into array to append (both in the mani nad in the sub). (You could use a reenetrant subVI and a parallel innermost FOR loop, but that seems overly complicated too)

 

If you want to do a running average on each channel, the subVI needs to keep a 2D array in the subVI. All this has been done before.


I can understand your frustration - more so when the context of LV coding is different in both our lives. For me as a mechanical engineer its a add on and that which started as a time pass. So the code may not be the most efficient or one that extracts the last drop of juice from the CPU. In fact in machine control applications the whole of the CPU does nothing more most times than run the control code. And with processors like i5 or i7,  it makes it even more easy. I am not saying efficient code is a waste of time - i am just saying the context is different.

 

Just before i wind down here is another one quick and dirty one which does work provided the first few values are ignored - not a big deal in a data logging application that runs for hours on end.

 

Simple N Channel Version.png

Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
Message 11 of 16
(6,868 Views)

Raghunathan,

 

     I'm sorry you have such a low regard for elegance and correctness, and that you feel that "Good Enough is Good Enough".  Maybe, in the future, you will consult someone who knows and understands LabVIEW and simply tell him (or her) what you want to do and let them do it for you.

 

     However, if your goal is to (a) collect data from N channels at frequency F (in Hz), but to display the average of M points (so for every M points you generate you plot 1 point, the average of the M points), here's a good way to do it:

  • Create a "Producer Loop" that generates the data by setting your Sampling Device to give you M Points from N Channels, sampled at frequency F.  This should be a 2D array (M rows of N columns).  Put this in a Queue and repeat, over and over, until the Stop button is pushed.
  • After exiting the Producer Loop, send one more empty 2D array (you'll see why in a moment).
  • Create a parallel "Consumer Loop" and feed it the Queue used by the Producer Loop.  Dequeue the Data Array -- if it is empty, we're done, so release the Queue and stop the Consumer Loop.
  • Otherwise, we need to "Consume" (= Average and Plot) the data.  We need to transpose the M Points by N Channels array to a N Channel by M Points array.  Feed this Array to a For Loop (which will loop over the Channels), compute the Average o the M Points, and output this Average.  We want to plot this (single) point from all of the N Channels, so pass it through Build Array to make a 1 by N Array, and plot this.

Here is a Snippet in LabVIEW 2012 that lets you simulate all of this (it generates points at the frequency you specify, max 1KHz).  I also scale the Plot so that Time is in seconds.  [This didn't "translate" well for the Snippet -- I actually used an Averaged Data Property Node there].

Average Plot.png

This, I think, does what I understand you really wanted, namely to have a display that shows the "average" (at a slower plot rate than the data).  You'll notice that each Channel has a different offset, so when you look at the plot, you should see 16 lines, looking more and more smooth as you increase the Sample Size.  You need to specify the parameters before you run the code, since the principles of Data Flow (on which LabVIEW is based) will ignore changes you make to the Controls once the program is running.

 

Bob Schor

 

Message 12 of 16
(6,846 Views)
Solution
Accepted by topic author MogaRaghu

Here's a simple subVI that does it all in-place (unless the number of signals or the number of averages changes).

See if it makes sense to you....

Message 13 of 16
(6,828 Views)

Hi Bob,

 

First let me thank you for the detailed response and the code snippet. I apprecaite your intention to help.

 

My earlier response possibly sounded a bit gross but  i will be the last person to palm off something that is half baked or unstable. Let me explain :

 

My line of activity oil hydraulics and i have my own company established in 1995 and as a value addition proposition i supply integrated systems with instrumenation and software. We operate in two major vertcials - WInd Turbines and Test Systems. And to date we have supplied many complete systems which are used widely in the automotive industry here in India as well as in Europe.

 

But my company is not a huge organization and is a mid sized one and here in there is a catch - it is next to impossible to find expert LabVIEW programmers. India has a huge pool of software engineers but all are into desktop computing and the % of engineers in Physical Computing is almost nil or negligible. SO when clients come to me with fresh requirements ( trust me - all are different ) i take up the challenge personally but to deliver i entirely depend on support from on-line community. Todate i have successfully developed LV code to get data via CanOpen, Modbus RTU, Profinet and EtherCAT. WIth such variety and short time to market demands i simply dont have the resources to deploy a huge team to deliver. So it boils down to developing basic or obvious code that does what it is supposed to and if the client is OK with it - leave it there. Its a difference between Functional code and Smart code and i was hinting at the earlier one.. and not developing crappy code Smiley Wink

 

Coming to your sample code - this producer consumer model is a regular one that i use for all my projects where I have Main as the producer and the Sub VIs as the consumer. With data flow via Queue.  Will study it more in detail.

 

Cheers

Raghu

Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
0 Kudos
Message 14 of 16
(6,801 Views)

@altenbach wrote:

Here's a simple subVI that does it all in-place (unless the number of signals or the number of averages changes).

See if it makes sense to you....


Yes it does. Very compact and exactly does what is required. Thanks for sending the Sub VI as well as an example using it. Its getting to be clear as to what compact code is all about !!

Raghu

Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
Message 15 of 16
(6,793 Views)

Thank you!

You are always a legend!

 

0 Kudos
Message 16 of 16
(3,667 Views)