How to find the average wind direction

Every minute we get readings from the wind sensor. It is necessary to calculate the average wind direction for the last 10 minutes. First of all, I applied the AVG function to all 10 records, but I quickly realized that this averaging method does not work with the direction.

At the moment, there is such a function:

def get_avg_direction (directions):
      sinSum = 0
      cosSum = 0
      for value in directions:
        sinSum += math.sin(math.radians(value))
        cosSum += math.cos(math.radians(value))

      return ((math.degrees(math.atan2(sinSum, cosSum)) + 360) % 360)

How true is it?

Author: 0xdb, 2019-09-01

2 answers

Judging by the function, your language is Python.

For the answer, you will need not only an array of directions, but also an array of velocities (that is, the directions of the wind vectors and the lengths of these vectors). These arrays are assumed to be of the same length.

Here is the corrected code based on your function with an example of using the function:

import math

def get_avg_direction (directions, speeds):
    sinSum = 0.0
    cosSum = 0.0
    for value, speed in zip(directions, speeds):
        sinSum += speed * math.sin(math.radians(value))
        cosSum += speed * math.cos(math.radians(value))
    sinSum = sinSum / len(directions)
    cosSum = cosSum / len(directions)
    return ((math.degrees(math.atan2(sinSum, cosSum)) + 360) % 360), math.sqrt(cosSum*cosSum + sinSum*sinSum)

d, sp = get_avg_direction([90, 90, 270], [1, 1, 4])

print d, sp
 2
Author: zcorvid, 2019-09-03 14:14:30

If we talk in terms of signal processing, then you have 10 complex samples in exponential form, that is, the amplitude + phase. Translate them from exponential to algebraic, sum them up, and the phase of this sum will give you the desired result.

I think python has everything you need for complex arithmetic

 0
Author: Pavel Gridin, 2019-09-01 19:50:36