altoslib: Keep time series in AltosFlightSeries array sorted
[fw/altos] / altoslib / AltosTimeSeries.java
1 /*
2  * Copyright © 2017 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 package org.altusmetrum.altoslib_11;
16
17 import java.util.*;
18
19 public class AltosTimeSeries implements Iterable<AltosTimeValue>, Comparable<AltosTimeSeries> {
20         public String                   label;
21         public AltosUnits               units;
22         ArrayList<AltosTimeValue>       values;
23
24         public int compareTo(AltosTimeSeries other) {
25                 return label.compareTo(other.label);
26         }
27
28         public void add(AltosTimeValue tv) {
29                 values.add(tv);
30         }
31
32         public void add(double time, double value) {
33                 add(new AltosTimeValue(time, value));
34         }
35
36         public AltosTimeValue get(int i) {
37                 return values.get(i);
38         }
39
40         private double lerp(AltosTimeValue v0, AltosTimeValue v1, double t) {
41                 /* degenerate case */
42                 if (v0.time == v1.time)
43                         return (v0.value + v1.value) / 2;
44
45                 return (v0.value * (v1.time - t) + v1.value * (t - v0.time)) / (v1.time - v0.time);
46         }
47
48         private int after_index(double time) {
49                 int     lo = 0;
50                 int     hi = values.size() - 1;
51
52                 while (lo <= hi) {
53                         int mid = (lo + hi) / 2;
54
55                         if (values.get(mid).time < time)
56                                 lo = mid + 1;
57                         else
58                                 hi = mid - 1;
59                 }
60                 return lo;
61         }
62
63         /* Compute a value for an arbitrary time */
64         public double value(double time) {
65                 int after = after_index(time);
66                 double ret;
67
68                 if (after == 0)
69                         ret = values.get(0).value;
70                 else if (after == values.size())
71                         ret = values.get(after - 1).value;
72                 else {
73                         AltosTimeValue b = values.get(after-1);
74                         AltosTimeValue a = values.get(after);
75                         ret = lerp(b, a, time);
76                 }
77                 return ret;
78         }
79
80         /* Find the value just before an arbitrary time */
81         public double value_before(double time) {
82                 int after = after_index(time);
83
84                 if (after == 0)
85                         return values.get(0).value;
86                 return values.get(after-1).value;
87         }
88
89         /* Find the value just after an arbitrary time */
90         public double value_after(double time) {
91                 int after = after_index(time);
92
93                 if (after == values.size())
94                         return values.get(after-1).value;
95                 return values.get(after).value;
96         }
97
98         public double time_of(double value) {
99                 double  last = AltosLib.MISSING;
100                 for (AltosTimeValue v : values) {
101                         if (v.value >= value)
102                                 return v.time;
103                         last = v.time;
104                 }
105                 return last;
106         }
107
108         public int size() {
109                 return values.size();
110         }
111
112         public Iterator<AltosTimeValue> iterator() {
113                 return values.iterator();
114         }
115
116         public AltosTimeValue max() {
117                 AltosTimeValue max = null;
118                 for (AltosTimeValue tv : values)
119                         if (max == null || tv.value > max.value)
120                                 max = tv;
121                 return max;
122         }
123
124         public AltosTimeValue max(double start_time, double end_time) {
125                 AltosTimeValue max = null;
126                 for (AltosTimeValue tv : values) {
127                         if (start_time <= tv.time && tv.time <= end_time)
128                                 if (max == null || tv.value > max.value)
129                                         max = tv;
130                 }
131                 return max;
132         }
133
134         public AltosTimeValue min() {
135                 AltosTimeValue min = null;
136                 for (AltosTimeValue tv : values) {
137                         if (min == null || tv.value < min.value)
138                                 min = tv;
139                 }
140                 return min;
141         }
142
143         public AltosTimeValue min(double start_time, double end_time) {
144                 AltosTimeValue min = null;
145                 for (AltosTimeValue tv : values) {
146                         if (start_time <= tv.time && tv.time <= end_time)
147                                 if (min == null || tv.value < min.value)
148                                         min = tv;
149                 }
150                 return min;
151         }
152
153         public double average() {
154                 double total = 0;
155                 int count = 0;
156                 for (AltosTimeValue tv : values) {
157                         total += tv.value;
158                         count++;
159                 }
160                 if (count == 0)
161                         return AltosLib.MISSING;
162                 return total / count;
163         }
164
165         public double average(double start_time, double end_time) {
166                 double total = 0;
167                 int count = 0;
168                 for (AltosTimeValue tv : values) {
169                         if (start_time <= tv.time && tv.time <= end_time) {
170                                 total += tv.value;
171                                 count++;
172                         }
173                 }
174                 if (count == 0)
175                         return AltosLib.MISSING;
176                 return total / count;
177         }
178
179         public AltosTimeSeries integrate(AltosTimeSeries integral) {
180                 double  value = 0.0;
181                 double  pvalue = 0.0;
182                 double  time = 0.0;
183                 boolean start = true;
184
185                 for (AltosTimeValue v : values) {
186                         if (start) {
187                                 value = 0.0;
188                                 start = false;
189                         } else {
190                                 value += (pvalue + v.value) / 2.0 * (v.time - time);
191                         }
192                         pvalue = v.value;
193                         time = v.time;
194                         integral.add(time, value);
195
196                 }
197                 return integral;
198         }
199
200         public AltosTimeSeries differentiate(AltosTimeSeries diff) {
201                 double value = 0.0;
202                 double time = 0.0;
203                 boolean start = true;
204
205                 for (AltosTimeValue v: values) {
206                         if (start) {
207                                 value = v.value;
208                                 time = v.time;
209                                 start = false;
210                         } else {
211                                 double  dx = v.time - time;
212                                 double  dy = v.value - value;
213
214                                 if (dx != 0)
215                                         diff.add(time, dy/dx);
216
217                                 time = v.time;
218                                 value = v.value;
219                         }
220                 }
221                 return diff;
222         }
223
224         private int find_left(int i, double dt) {
225                 int j;
226                 double t = values.get(i).time - dt;
227                 for (j = i; j >= 0; j--)        {
228                         if (values.get(j).time < t)
229                                 break;
230                 }
231                 return j + 1;
232
233         }
234
235         private int find_right(int i, double dt) {
236                 int j;
237                 double t = values.get(i).time + dt;
238                 for (j = i; j < values.size(); j++) {
239                         if (values.get(j).time > t)
240                                 break;
241                 }
242                 return j - 1;
243
244         }
245
246         private double filter_coeff(double dist, double width) {
247                 double ratio = dist / (width / 2);
248
249                 return Math.cos(ratio * Math.PI / 2);
250         }
251
252         public AltosTimeSeries filter(AltosTimeSeries f, double width) {
253                 double  half_width = width/2;
254                 for (int i = 0; i < values.size(); i++) {
255                         double  center_time = values.get(i).time;
256                         double  left_time = center_time - half_width;
257                         double  right_time = center_time + half_width;
258                         double  total_coeff = 0.0;
259                         double  total_value = 0.0;
260
261                         int     left = find_left(i, half_width);
262                         int     right = find_right(i, half_width);
263
264                         for (int j = left; j <= right; j++) {
265                                 double  j_time = values.get(j).time;
266
267                                 if (left_time <= j_time && j_time <= right_time) {
268                                         double  j_left = j == left ? left_time : values.get(j-1).time;
269                                         double  j_right = j == right ? right_time : values.get(j+1).time;
270                                         double  interval = (j_right - j_left) / 2.0;
271                                         double  coeff = filter_coeff(j_time - center_time, width) * interval;
272                                         double  value = values.get(j).value;
273                                         double  partial = value * coeff;
274
275                                         total_coeff += coeff;
276                                         total_value += partial;
277                                 }
278                         }
279                         if (total_coeff != 0.0)
280                                 f.add(center_time, total_value / total_coeff);
281                 }
282                 return f;
283         }
284
285         public AltosTimeSeries(String label, AltosUnits units) {
286                 this.label = label;
287                 this.units = units;
288                 this.values = new ArrayList<AltosTimeValue>();
289         }
290 }