Android Open Source - bearing-example Average Angle






From Project

Back to project page bearing-example.

License

The source code is released under:

MIT License

If you think the Android project bearing-example listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ymc.ch.bearingexample;
/*from w  ww .  j a  va 2 s.com*/
/**
 * Created by nkuebler on 14/07/14.
 */
public class AverageAngle
{
    private double[] mValues;
    private int mCurrentIndex;
    private int mNumberOfFrames;
    private boolean mIsFull;
    private double mAverageValue = Double.NaN;

    public AverageAngle(int frames)
    {
        this.mNumberOfFrames = frames;
        this.mCurrentIndex = 0;
        this.mValues = new double[frames];
    }

    public void putValue(double d)
    {
        mValues[mCurrentIndex] = d;
        if (mCurrentIndex == mNumberOfFrames - 1) {
            mCurrentIndex = 0;
            mIsFull = true;
        } else {
            mCurrentIndex++;
        }
        updateAverageValue();
    }

    public double getAverage()
    {
        return this.mAverageValue;
    }

    private void updateAverageValue()
    {
        int numberOfElementsToConsider = mNumberOfFrames;
        if (!mIsFull) {
            numberOfElementsToConsider = mCurrentIndex + 1;
        }

        if (numberOfElementsToConsider == 1) {
            this.mAverageValue = mValues[0];
            return;
        }

        // Formula: http://en.wikipedia.org/wiki/Circular_mean
        double sumSin = 0.0;
        double sumCos = 0.0;
        for (int i = 0; i < numberOfElementsToConsider; i++) {
            double v = mValues[i];
            sumSin += Math.sin(v);
            sumCos += Math.cos(v);
        }
        this.mAverageValue = Math.atan2(sumSin, sumCos);
    }
}




Java Source Code List

ymc.ch.bearingexample.ApplicationTest.java
ymc.ch.bearingexample.AverageAngle.java
ymc.ch.bearingexample.BearingDisplay.java
ymc.ch.bearingexample.BearingToNorthProvider.java