# LabVIEW

cancel
Showing results for
Did you mean:

Solved!
Go to solution

## Background loop of camera by calculating Moving Average

Hi,

Let me tell you my application for  Image capture by Ximea Camera.

The background captured should be the moving average of the intensities  and change the value of the next background depending on the intensities of the following images .

Let me problem statement in the word doc named camera.docx.

I am unable to proceed for a moving array or shift register .

Thanks  a Lot

Message 1 of 10
(2,298 Views)

## Re: Background loop of camera by calculating Moving Average

The problem you pose involves what some call a "moving average", except you impose additional criteria for deciding which points take part in the average.

One way to do a "conventional" moving average is to have a queue-like storage area for the points being averaged.  For example, if you are doing a 5-point average, you could have an array of 5 elements and add new elements in a "circular" manner, i.e. in positions 0, 1, 2, 3, 4, 0, 1, 2, 3, ...  You could also use a fixed-length Queue of 5 elements and use a "Lossy Enqueue" (where the "oldest" element "falls off the end of the Queue".

However you maintain the elements being averaged, when a new number comes in, you add it to the Store-of-5 (losing the oldest) and compute a new Average.

In your case, you'd proceed as follows:

• Use the first 5 elements to build the Store-of-5.
• For each succeeding element (i.e. from the 6th element):
• Determine if it meets your "inclusion criterion".
• If so, add it to the Store and re-compute the Average.

You should be able to do this by splitting the input Array into 5 and N-5 sub-arrays, initialize the Store (to a Shift Register), then do the rest in a single For Loop with a suitable Case Statement checking the inclusion criterion and updating the Store and Average as needed.

Bob Schor

P.S. -- I'm deliberately not attaching a VI.  You will learn a lot more if you "do it yourself".  If you are unfamiliar with Queues and don't need to learn about them now, you should certainly be able to handle the "circular Array" (also called a "Ring Buffer") -- pay attention to when and how you update the Array index (hint -- the number 5 plays a role in the update process).

Message 2 of 10
(2,246 Views)

## Re: Background loop of camera by calculating Moving Average

Thank you so much for your suggestions.

I have tried implementing what I could understand.

However, I am still perplexed with the circular array and hence unable to calculate the new average when in  inclusion criteria.(inside the case structure in the  Vi)

I have tried on 2 solutions .

It would be really helpful if you let me verify my code.

Thanking you immensely

Message 3 of 10
(2,238 Views)
Solution
Accepted by topic author Sabitri@22

## Re: Background loop of camera by calculating Moving Average

Sorry, I was travelling yesterday.

When trying to implement "something new" like a Moving Average Filter, it is sometime useful (and instructive!) to write a little Demo program where you make up data that are simple and understandable and test your algorithm on that.  With a simple system, you can play and tweak and figure out how things work.

I'm also thinking that you might not be so familiar with Digital Filters, where "time" is quantized and can lead to "unexpected" situations, particularly at the beginning and end of the signal.  You can often see these effects (and think about how to handle them) with your Demo.

So, here is a Demo Simple Moving Average.  It's pretty much all in the Snippet, but I've also attached the VI (unless I forget).  The Snippet and code are in LabVIEW 2016, but it should be simple enough that you can create it "from scratch" from the Snippet image.

The two Functions on the left are from the Waveform Palette and are Function Generators.  The top one generates 2 cycles of a 10 Hz Triangle Wave sampled at 1KHz, while the bottom one generates Gaussian Noise, which are added together to make a Noisy Triangle Signal (I chose the Triangle because it is easy to compare the effects of the Filter.

We initialize the 5-point Moving Average by creating a "Filter Memory" having 5 zeros.  The Filter takes the Waveform Data Points (Y) and replaces each point with the average of that point and the 4 preceding data points.  Do you see how we use the Remainder function to convert the Point Index, "i", from 0..199 to 0..4 (so we save the latest point in positions 0, 1, 2, 3, 4, 0, 1, 2, ...)?  Finally, we plot both the original ("noisy") data and the filtered ("smoothed") data on the same graph.

Try running this.  You should see the effect of the filter quite readily.  Do you notice any difference between the input (white) and filtered (red) signals?  Look at the positions of the peaks, and also examine the first few points.  This may be clearer if you re-run this after setting Noise to 0.  You can also change the filter size to a larger number, which will make the difference more obvious.

For a finite data sample, digital filtering means that you essentially "lose some points" and some information (but you want to lose some "information", namely the noise ...).  Why don't Analog filters "lose data"?  They do, it's just that we rarely look at them during the first milliseconds that they are on.

This is not exactly the code I described earlier -- there, I "pre-initialized" the filter by giving it the first 5 numbers, and started filtering from the fifth data point.  But this simpler example, I think, demonstrates Moving Average Filters in a simpler manner and shows some of the important properties of such filters.  At some point, you will learn about Low Pass Filters (this is an example of such a filter), and will learn that they introduce a "Phase Lag" in the signal.  "Phase Lag" is to Frequency as "Delay" is to Time.

Bob Schor

Message 4 of 10
(2,222 Views)

## Re: Background loop of camera by calculating Moving Average

Dear Bob,

I am unable to view the Vi due to my labView being an order version (2015).

I have solved the issue according to my understanding( I think there is some  logical error)

Thanking immensely,

Message 5 of 10
(2,211 Views)

## Moving Average using constraints

Hi,
I am trying to calculate the moving average of 5 numbers based on a condition as attached in the word document.
I have created the Vi for the same.
However , I am facing some logical errors in sending back the average value to the condition check via the shift register.
Can anybody please verify my program and help me solve it .

Thanking you immensely,

Message 6 of 10
(2,229 Views)

## Re: Moving Average using constraints

Your inner while loop never receives any new data while it is running.

(Most of the code makes no sense at all.)

Message 7 of 10
(2,225 Views)

## Re: Moving Average using constraints

Hi,

I  am aware of the  problem but could not find the way out  to solve this issue, hence seeking help.

(I have just started to learn LabView and thus such silly doubts and issues.)

Thanking you again.

Message 8 of 10
(2,216 Views)

## Re: Background loop of camera by calculating Moving Average

Hi Bob ,

I have created the VI as per the suggestion
However , I am facing some logical errors in sending back the average value to the condition check via the shift register.

I am very new to this programming language and so facing lot of issue even in the basics hence seeking for suggestions.
Best regards,

Message 9 of 10
(2,182 Views)

## Re: Moving Average using constraints

Have a look at "mean ptbypt" check the new value using "in range and coerce" and skip (case structure) the "mean ptbypt" if the new value does not meet the condition. Virtually no code needed (at least compare to what you currently have).

Lear about the principles of dataflow. It is central for understanding LabVIEW.

Message 10 of 10
(2,188 Views)