Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by NCartist · Feb 10, 2018 at 01:02 AM · runningaverage

How to find a running average from an int that changes every frame

I have an int (ie. external sensor value) that changes every frame. I would like to find a running average over every 10 frames or so, because I am trying to smooth out the occasional erratic values.

What is the simplest way to do this in Unity and how do you store the changing values to calculate an average?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by pako · Feb 10, 2018 at 02:58 PM

To calculate a Moving Average per the "Cumulative moving average" formula in your link, use the following code:

 using UnityEngine;
 
 public class AverageValues : MonoBehaviour
 {
     public int NewValue; //this is an example for the value to be averaged
     public int MovingAverageLength = 10; //made public in case you want to change it in the Inspector, if not, could be declared Constant
 
     private int count;
     private float movingAverage;
 
     
     // Update is called once per frame
     void Update ()
     {
         count++;
 
         //This will calculate the MovingAverage AFTER the very first value of the MovingAverage
         if (count > MovingAverageLength)
         {
             movingAverage = movingAverage + (NewValue - movingAverage) / (MovingAverageLength + 1);
 
             //Debug.Log("Moving Average: " + movingAverage); //for testing purposes
 
         }
         else
         {
             //NOTE: The MovingAverage will not have a value until at least "MovingAverageLength" values are known (10 values per your requirement)
             movingAverage += NewValue;
 
             //This will calculate ONLY the very first value of the MovingAverage,
             if (count == MovingAverageLength)
             {
                 movingAverage = movingAverage / count;

                 //Debug.Log("Moving Average: " + movingAverage); //for testing purposes
             }
         }
         
     }
 }
 

I include the necessary code in a separate class AverageValues for demonstration purposes. Of course you can use the necessary code inside your own class.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NCartist · Feb 10, 2018 at 05:24 PM 1
Share

Works perfect! Thank you!

avatar image pako NCartist · Feb 10, 2018 at 05:41 PM 0
Share

You are welcome!

avatar image Dr1u · Nov 24, 2020 at 04:44 PM 1
Share

Isn't there a typo line 34? I think it's movingAverage = movingAverage / count;

avatar image pako Dr1u · Nov 24, 2020 at 05:56 PM 0
Share

Good catch @Dr1u I corrected it.

avatar image
1

Answer by Commoble · Feb 10, 2018 at 01:34 AM

I would store the changing values in a Queue and calculate the average every frame.


The average of 10 values is trivial to calculate performance-wise, but if you really wanted to optimize it, once you hit 10 values and start taking things out of the queue, instead of taking the average of each value, you could subtract 1/10 of the outgoing value (from ten frames ago) from your running total and add 1/10 of the new incoming value (I can't guarantee this won't cause weird skewing problems from rounding errors if you do this for a long time, though).

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MaxGuernseyIII · Feb 10, 2018 at 01:51 AM 0
Share
  • for being the right answer.

If you're using integer math, you can get a rounding error after the first frame if you are doing the add or subtract a fraction thing. You could do something weird like store the result of division in one queue and the remainder in another queue then use the result to influence the floating average and use the modulus values to wiggle an error variable that converts over to a plus or $$anonymous$$us one on the running average whenever it gets too large or too small.

...you'll probably never recoup the time it would take to write a "fast", accurate averaging algorithm in terms of execution time, though. ;)

avatar image Commoble MaxGuernseyIII · Feb 10, 2018 at 02:19 AM 1
Share

Oh, right, he's working with ints. Yeah, you'd want to convert to floats if you're adding/subtracting from the running total

avatar image MaxGuernseyIII Commoble · Feb 10, 2018 at 03:11 AM 0
Share

I tried to type the "plus" in "plus one" as a symbol and it didn't work out very well...

avatar image NCartist · Feb 10, 2018 at 01:16 PM 0
Share

I'm a very beginner programmer. Could you give a short example of what this would look like in C# code? Thank you!

PS. I'm not concerned with small rounding errors. I'm dealing with integers from 0-1024 and simply want to smooth out the occasional extreme outlier number. It is not a super-precise application.

avatar image pako · Feb 10, 2018 at 01:42 PM 0
Share

Wouldn't using a Queue be an overkill in this case? I mean why not use just a simple int variable to accumulate all the values to be averaged?

Am I missing something?

Also, @NCartist do you want to average only the last 10 values, or every 10 values average all the available values?

avatar image NCartist · Feb 10, 2018 at 01:57 PM 0
Share

@pako I would like a running / moving average https://en.wikipedia.org/wiki/$$anonymous$$oving_average

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

79 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how do I make my character jump while running without losing all your speed 1 Answer

[Right Click to Necromance] How I do the run system, for only one character? (FOR 2D RPG GAME) 2 Answers

unity not popening mac suddenly 0 Answers

if the user account does not have a Korean name goes? 0 Answers

Exception: IL2CPP Linkage failed 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges