AD averaging

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
Roy Johnston
Flowcode V4 User
Posts: 220
Joined: Mon Aug 24, 2009 8:38 am
Has thanked: 2 times
Been thanked: 34 times
Contact:

AD averaging

Post by Roy Johnston »

I need to take samples of an analogue input and then average them over a fixed time. i use an interupt to call a macro . in this macro i need to sample a number of samples and then add them up, wherupon i will divide it by the number of samples. is there an easier way of doing it

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: AD averaging

Post by Jan Lichtenbelt »

What about this? Pay attention: I do not know the convertion time of the ADC. This must be shorter as the time between two interrupts.

Good luck

Jan Lichtenbelt
Attachments
Average of analoge input over 1 second.fcf
(6 KiB) Downloaded 504 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: AD averaging

Post by medelec35 »

Hiya Roy.
I had a problem in which I had to add up a number of samples then divide by the amount of samples taken.
See attachment. I know its not the way you intended, but it shows one simple way of solving the problem.
The only thing by adding up samples over a given time is the total cannot go over 32767 which is the amount assigned by a temporary ram area for calculation (I believe that is correct?) If the total goes over that then you will get roll over to -32768. this means when divided by the amount of samples the total will be wrong.
If you state the total sampling time you are interested in I will also created a flowchart to achieve the desired results. I know Jan has kindly created a flowchart, but the more options posted, the more that can be learnt.
Attachments
Average Value.fcf
(5 KiB) Downloaded 511 times
Martin

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Moving average

Post by Jan Lichtenbelt »

Sometime a moving average is wanted. Please find a method without using an array of elements and without the possibility of integer overflow.

Good Luck

Jan Lichtenbelt
Attachments
Moving Average.fcf
(9.5 KiB) Downloaded 550 times

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Moving average

Post by Jan Lichtenbelt »

Please find some more details of this simple program for the moving average of a variable.
The advantages are:
- no additional memory used (EEPROM or array)
- no overflow due to lack of summing data


See the Flowcode attached to the previous message.


Kind regards

Jan Lichtenbelt

Remark november 10, 2011: The attachements are tested to be free from virus
Attachments
Moving average.xls
(23 KiB) Downloaded 418 times
Moving average.doc
(36.5 KiB) Downloaded 454 times
Last edited by Jan Lichtenbelt on Thu Nov 10, 2011 1:47 pm, edited 1 time in total.

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: AD averaging

Post by JonnyW »

Nice method Jan. I've always used arrays for this. One word of warning - the moving average will produce inaccurate results for the first N elements added (where N is the array size).

For example:
* MA initialised to 0 (no samples)
* Sample 96 is added
* MA = MA - (MA/8) + (96/8) = 12

But the average of one element is the value of that element. This can be rectified using a counter and accumulating this up to the limit N.

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

moving averaging

Post by Jan Lichtenbelt »

Dear Jonny

The example you gave is correct, and as expected. If the moving average MA =0 and the next value added is 96, using only 1/8 to change, then the new value is:
MA -MA/8+New/8= 0-0+12=12. It will take 18 steps to become at 90% of the newvalue. Or MA 0,9*96=86 after 18 steps. See details the Word document.

Dividing by a factor 8 makes the moving average 'slow'. Try also smaller values like 2 and 4.

Kind regards,

Jan Lichtenbelt

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: AD averaging

Post by medelec35 »

Thanks Jan.
This will come in handy. I did not know this method existed.

Martin
Martin

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: AD averaging

Post by JonnyW »

Yes, it is nice - I have a method I use at home using arrays, it might well get updated to use this system!

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: AD averaging

Post by Benj »

Hello,

I'm not sure about how the compiler optimises the code but if the divide is a power of two then it might be more code efficient to use right shifts rather then the divide.

eg. MA -(MA>>3)+(New>>3) = 0-0+12=12

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: AD averaging

Post by Jan Lichtenbelt »

In the Flowcode, as described in the Word document and given in the Flowcode example, only shifts have been used.

That is the reason in Flowcode only 1/n (with n=2,4,8) parts of the moving average will be changed. Mathemactically all 1/n parts with n=1,2,3,4 can be used.

Kind regards

Jan

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Moving average

Post by Jan Lichtenbelt »

Please find a 2 pages Word document attached, which describes the moving average in general and including the Flowcode to be used.

Kind regards,

Jan Lichtenbelt
Attachments
Filtering by means of moving average.doc
(55 KiB) Downloaded 405 times

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Moving averaging

Post by Jan Lichtenbelt »

I'm now using this moving average method again in an other program. And with this experience I learn more about the advantages and disadvantages of this method. I will try to explain as simple as possible the usual method (sum of values divided by the number of values) compared to the method I proposed. I call them here respectively method I and method II.
1) The advantage of method II is in the first place that it takes less memory. Method II use only one parameter, the moving average (MA) itself, while in method I you need to remember older values and scroll them after each step.
2) Method II is very simple. Part of the old value is replaced by (the same) part of the new value. E.g.part is 1/8, then
MA(new) = MA(old)- MA(old)/8+New_Value/8. That's all. (Remark: Use >>3 in stead of /8 in your program)
3) Additional, but not necessary, the accuracy of MA can be improved by up-scaling and down-scaling. Dividing by 8 is equal to roll right 3 times. That means 3 bits are lost by this division. If it is possible to up-scale a factor 8, both MA and new value, no bits will be lost by the division of 8. But if you start the first time with MA, forget this scaling.
4) A more serious matter is that MA-II there will be always a kind of memory, in contrast with MA-I. Let we start with method I. E.g. we take the moving average of 5 values. Each new value will be lost after 5 steps. That means there is no memory of older values. In contrast with MA-II, where all values will in principal disappear only after infinite steps. The reason is simple, a part of the old MA value will be present in the new MA value. In the example above:
MA(new) = MA(old)x 7/8 + New_Value/8. The part of the old value after N steps will be (7/8)power N. And this will become zero for N is infinite.
5) The consequence is that even the choice of the start value for MA-II is important. MA-II(start) is 0, a very high value or just a 'reasonable' value?
6) My advise for using the MA-II method depends on the accuracy you want and the number of iterations.
In the example above, with a division of 8, the after 22 iterations an accuracy of 95% will be reached, 32 steps for 99% accuracy.
Replacing a quart (division with 4) after 10 steps an accuracy of 95% will be reached, 15 steps for 99% accuracy. If even less iterations are required you can try to replace the half of the MA. There are 4 steps needed for 95% accuracy and 7 steps for 99%.
With accuracy I mean that you start MA=0 and in each iteration a constant value will replace a part of the MA. The end value after infinite iterations, will be exact the new value. But in general we accept that the end value is reached within an accuracy of 95% or 99% (for other values see word document)
Using this method the first time replace, the MA with a quart of the new value.

Conclusion: This moving average method, by replacing just a part of a new value, has the advantage of simple programming with less memory needed. But care has to be taken with the fact that older values disappear only relatively slowly with each new value. Proper choices of the part which will replaced, in combination with the number of steps, will give you the wanted accuracy of this moving average method.

Succes

Jan Lichtenbelt

Post Reply