altosdroid: Fix offline map messages to match new meanings
[fw/altos] / altoslib / AltosTimeSeries.java
index db33fafa56a088c1e05910229fb7029802da13c5..75225e1558d236ae45bdd34e72509fcce48905ab 100644 (file)
  * General Public License for more details.
  */
 
-package org.altusmetrum.altoslib_11;
+package org.altusmetrum.altoslib_13;
 
 import java.util.*;
 
-public class AltosTimeSeries implements Iterable<AltosTimeValue> {
+public class AltosTimeSeries implements Iterable<AltosTimeValue>, Comparable<AltosTimeSeries> {
        public String                   label;
        public AltosUnits               units;
        ArrayList<AltosTimeValue>       values;
+       boolean                         data_changed;
+
+       public int compareTo(AltosTimeSeries other) {
+               return label.compareTo(other.label);
+       }
 
        public void add(AltosTimeValue tv) {
+               data_changed = true;
                values.add(tv);
        }
 
+       public void erase_values() {
+               data_changed = true;
+               this.values = new ArrayList<AltosTimeValue>();
+       }
+
+       public void clear_changed() {
+               data_changed = false;
+       }
+
+//     public boolean changed() {
+//             return data_changed;
+//     }
+
        public void add(double time, double value) {
                add(new AltosTimeValue(time, value));
        }
@@ -146,30 +165,51 @@ public class AltosTimeSeries implements Iterable<AltosTimeValue> {
                return min;
        }
 
+       public AltosTimeValue first() {
+               if (values.size() > 0)
+                       return values.get(0);
+               return null;
+       }
+
+       public AltosTimeValue last() {
+               if (values.size() > 0)
+                       return values.get(values.size() - 1);
+               return null;
+       }
+
        public double average() {
-               double total = 0;
-               int count = 0;
+               double total_value = 0;
+               double total_time = 0;
+               AltosTimeValue prev = null;
                for (AltosTimeValue tv : values) {
-                       total += tv.value;
-                       count++;
+                       if (prev != null) {
+                               total_value += (tv.value + prev.value) / 2 * (tv.time - prev.time);
+                               total_time += (tv.time - prev.time);
+                       }
+                       prev = tv;
                }
-               if (count == 0)
+               if (total_time == 0)
                        return AltosLib.MISSING;
-               return total / count;
+               return total_value / total_time;
        }
 
        public double average(double start_time, double end_time) {
-               double total = 0;
-               int count = 0;
+               double total_value = 0;
+               double total_time = 0;
+               AltosTimeValue prev = null;
                for (AltosTimeValue tv : values) {
                        if (start_time <= tv.time && tv.time <= end_time) {
-                               total += tv.value;
-                               count++;
+                               if (prev != null) {
+                                       total_value += (tv.value + prev.value) / 2 * (tv.time - start_time);
+                                       total_time += (tv.time - start_time);
+                               }
+                               start_time = tv.time;
                        }
+                       prev = tv;
                }
-               if (count == 0)
+               if (total_time == 0)
                        return AltosLib.MISSING;
-               return total / count;
+               return total_value / total_time;
        }
 
        public AltosTimeSeries integrate(AltosTimeSeries integral) {
@@ -239,14 +279,35 @@ public class AltosTimeSeries implements Iterable<AltosTimeValue> {
 
        }
 
-       private double filter_coeff(double dist, double width) {
-               double ratio = dist / (width / 2);
+       private static double i0(double x) {
+               double  ds = 1, d = 0, s = 0;
 
-               return Math.cos(ratio * Math.PI / 2);
+               do {
+                       d += 2;
+                       ds = ds * (x * x) / (d * d);
+                       s += ds;
+               } while (ds - 0.2e-8 * s > 0);
+               return s;
+       }
+
+       private static double kaiser(double n, double m, double beta) {
+               double alpha = m / 2;
+               double t = (n - alpha) / alpha;
+
+               if (t > 1 || t < -1)
+                       t = 1;
+               double k = i0 (beta * Math.sqrt (1 - t*t)) / i0(beta);
+               return k;
+       }
+
+       private double filter_coeff(double dist, double width) {
+               return kaiser(dist + width/2.0, width, 2 * Math.PI);
        }
 
        public AltosTimeSeries filter(AltosTimeSeries f, double width) {
+
                double  half_width = width/2;
+               int half_point = values.size() / 2;
                for (int i = 0; i < values.size(); i++) {
                        double  center_time = values.get(i).time;
                        double  left_time = center_time - half_width;