Aug 26 2009

Where am I headed? Calculating the Average of Azimuth Values

File under Software & Media. Popularity: 17%

This entry is part of a series, Azimuths and Rose Diagrams»

Recently at work, I needed to build averages of angles for a project. During the past couple days, I stumbled on some things that other people might be able to use, so I thought I’d write them up. It involves some interesting (if simple to the math/physics crowd) findings on angle averages, and the development of a small utility to create rose diagrams using Google Chart.

Averaging Angles

Here’s a question that would have been really easy to answer if I’d have paid any attention in sophomore year of high school: How do you average angles?

At work, we have a bunch of datapoints, each with a long list of  azimuths “compass readings” (e.g. [270°, 238°, 250°,281°, 275°]). We needed to generate the average  azimuth of the data.  This, I learned, is not as simple as I’d originally thought. It turns out that simply averaging the data is not (always) sufficient. Think of the example above. If we average those azimuths, we get 262.8°.

That’s fine. But what if we need the average of [355°, 358°, 0°, 5°, 351°]? Well, an arithmetic average of these values yields 213.8°. Our data shows that all the values are pretty much straight north, yet our average tells us that we’re pointing south-southeast. Obviously, that’s screwed up, so I needed to figure out what the hell I was doing.

What I found is that, basically, we need to look at the unit vectors of these angles and average those, then get the angle of this average. This involves just a bit of high school trigonometry.

gif.latex

The formula is provided here (assuming I haven’t forgotten all my high school maths)  It tells us to take the sines of all the angles i and add them together and divide that by the sum of the cosines of all the angles (The upside-down A is basically the notation “for all,” hence, “for all angles, i“). This value is the angle at which the arc-tangent is the average of all the angles.

Here’s a bit of Python code, using the analogous atan2() function:

#########################################
# Calculate the average azimuth
 
# A sample list of azimuth readings
lst = [355, 358, 0, 5, 351]
#Convert our list of values to radians
rads = [radians(i) for i in lst]
# Convenience function for averaging
ave = lambda x: sum(x)/len(x)
# Convenience function to bring negative angles back to positive angle values
rotate = lambda x: x+(2*pi) if x<0 else x 
 
# Calculate the averages
sa = ave([sin(i) for i in rads]) # Average of the sines
ca = ave([cos(i) for i in rads]) # Average of the cosines
# Take the arctan of the averages, rotate to positive values, and then
# convert back to degrees.
mean = degrees(rotate(atan2(sa,ca)))
########################################

The result of this calculation is “mean=357.8°,” virtually due north, which makes much more intuitive sense than 213.8°.

Maths is so interesting. Here we have a calculation that, basically, we can’t do in the current coordinate system, so we project it to a new geometric space (polar space), solve the problem, and then re-project it back into our current space. Crazy.

Here There Be Monsters

So, now I have the average of a list of azimuths and I can say with confidence that the “general direction” of all those datapoints is due north, right?

Right?

Uh, well, this is where things get tricky. As it turns out, I’m not too sure of that. I mean,  who’s to say my average isn’t really 213.8°?

Think of it this way: What if I have wind measurements with two datapoints, at 355° and 5°. What’s my average azimuth? 0°, due north, right? Well, what if those two datapoints were two outliers, and those two outliers happened to be all that I collected? What if, just when I collected them, a helicopter flew overhead?

Furthermore, what if I have datapoints spread uniformly through all directions? What if it’s just really windy and the wind is dominated by eddys. Is an average really meaningful when the wind can blow any direction? More than that, how could I be sure that my average was good, at least intuitively believable?

So, as it turns out, the maths is easy, but calculating an average is still no simple task. What I ended up with is a program that will generate an average of angular data without telling me whether that average was at all meaningful. What I needed was a way to visualize the data. Enter Rose diagrams.

Cambot, give me rocket number nine!
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • email
  • FriendFeed
  • HackerNews
  • Identi.ca
  • LinkedIn
  • Netvibes
  • NewsVine
  • Posterous
  • Reddit
  • Slashdot
  • Suggest to Techmeme via Twitter
  • Technorati
  • Tumblr
  • Twitter



Comments are closed at this time.

  • This week last year...