Home > RobotC Code > Weighted Average Class

Weighted Average Class

Continuing on my current fascination with math and code to implement it, I decided to write a class for weighted average monitoring.  Creating a running average of a stream of numbers can be complicated, and consume many variables (depending on how many samples make up the average).  It also means you have create some sort of queue so the values can shift such that the n most current samples are available for calculation.  That can be a lot of work.

Average = SUM(nSamples)/n 

I found a shortcut to all of that where an average value is maintained in the background, and a weighted percentage is applied to the newest input value and the previous average.  I am sure this method is not as accurate as averaging the last n values, but the goal is not so much precision, but reducing spikes and anomalies.  In other words, I don’t care what the actual average is; I care that the trend of values over time reduces the spikes and provides usable information to the robot with less error.

Weighted Average = New Sample * %wt + (1-%wt)*Previous Average

This class can help filter sensor data to a robot, making it react more slowly to large value changes.  Try it and let me know what you think…

You can download the code here: WeightedAverage.c

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: