How to find the average angle

Premise

The target is the angle in the two-dimensional coordinate system I am writing a program with an angle of -180 ° to 180 °, but I think that it can be 0 to 360 ° with a little rewriting.

Background (You can skip it)

In my master's research, I am making an indoor positioning application using Android (Java), and I am using a particle filter in it. Among them, it was necessary to take the operation of averaging the state vectors of each particle. There are three state vectors (x, y, θ), of which (x, y) can be simply divided by the number, but the angle does not work due to the problem of periodicity (described later). So I made it myself

Do not simply find the average angle

It is explained in detail with figures on the following site http://d.hatena.ne.jp/ootanAW/20111030/1319981509 Simply put, the average of 350 ° and 10 ° (at the angle defined by [0 ° ~ 360 °]) is 180 °, right? But considering the periodicity, I want the average to be 0 °. This time I am thinking about [-180 ° ~ 180 °], but the same problem occurs.

Then what should we do

According to the site of ↓, there seems to be a method of once converting the angle to a vector on the unit circle, synthesizing it, averaging the vectors, and then performing the inverse conversion. https://staff.aist.go.jp/toru-nakata/averageangle.html

The procedure is as follows

  1. Generate a vector ** Vn ** of (cos θn, sin θn) from each angle θn
  2. Find the average vector ** V ** of ** Vn **
  3. Obtain the average angle θ from ** V **

Code created (Java)

That's why I wrote it It is also uploaded to gist, so feel free to use it (I would appreciate it if you could point out any deficiencies).

Angle_average.java


import java.lang.Math;
public class Angle_average{
    public static void main(String[] args){

        final class vector{
            double x;
            double y;
        }

        System.out.println("Average angle");

        //Variable declaration / initialization
        double ang_avg;

        // -----Enter the angle you want to average here-----
        double[] angles = {10.0, 30.0, -10.0, -30.0};

        int len = angles.length;

        double[] angles_rad = new double[len];

        vector[] vectors = new vector[len];

        vector vector_avg = new vector();

        for (int i = 0; i < vectors.length; i++) {
            vectors[i] = new vector();
        }

        for (int i = 0; i < angles.length; i++) {
            angles_rad[i] = angles[i] * (Math.PI / 180.0);

            vectors[i].x = Math.cos(angles_rad[i]);
            vectors[i].y = Math.sin(angles_rad[i]);
        }

        //Find the average
        vector_avg.x = 0.0;
        vector_avg.y = 0.0;

        for (int i = 0; i < vectors.length; i++) {
            vector_avg.x += vectors[i].x;
            vector_avg.y += vectors[i].y;
        }

        vector_avg.x = vector_avg.x / (double)vectors.length;
        vector_avg.y = vector_avg.y / (double)vectors.length;
        
        ang_avg = Math.atan2(vector_avg.y, vector_avg.x);

        //Correction of calculation error
        if(Math.abs(ang_avg) < Math.pow(10, -8)){
            ang_avg = 0.0;
        }

        System.out.println(Double.toString(Math.toDegrees(ang_avg)) + "\n");
    }
}

Recommended Posts

How to find the average angle
How to find the total score and average score
Difference between Java and JavaScript (how to find the average)
How to find the tens and ones
How to find the cause of the Ruby error
[Ruby] How to find the sum of each digit
How to find the distance and angle between two points on a plane
How to use the link_to method
How to find May'n in XPath
How to find the total value, average value, etc. of a two-dimensional array (multidimensional array)-java
How to use the include? method
How to use the form_with method
How to use the wrapper class
Find the angle between two vectors
How to add the delete function
How to find Java prime numbers
[Java] How to use the File class
How to delete the wrong migration file
[Java] How to use the hasNext function
How to put out the error bundling
[Java] How to use the HashMap class
How to delete the migration file NO FILE
[Rails] How to use the map method
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to determine the number of parallels
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
[Swift] How to implement the countdown function
How to change the timezone on Ubuntu
Find the average age from List <Person>.
Ransack sort_link How to change the color!
[Processing × Java] How to use the class
How to sort the List of SelectItem
How to install the legacy version [Java]
How to pass the value to another screen
How to get the date in java
[Processing × Java] How to use the function
[Java] How to use the Calendar class
Summarized how to climb the programming stairs
How to deploy
How to find out the Java version of a compiled class file
How to find the total number of pages when paging in Java
[Rails] How to decide the destination by "rails routes"
How to use the camera module OV7725 (ESP32-WROVER-B)
Scraping Twitter to find the oldest Pien Tweet
How to check the logs in the Docker container
[Java] How to use Thread.sleep to pause the program
Customize how to divide the contents of Recyclerview
[Swift] How to implement the LINE login function
[swift5] How to implement the Twitter share function
How to add sound in the app (swift)
How to implement the breadcrumb function using gretel
[Swift] How to link the app with Firebase
How to get today's day of the week
[For beginners] How to implement the delete function
How to delete the database when recreating the application
Output of how to use the slice method
[Java] (for MacOS) How to set the classpath
How to use the replace () method (Java Silver)