30b24d82bcf3503fe6f059e7ae022c512f700994
[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> {
20         public String                   label;
21         public AltosUnits               units;
22         ArrayList<AltosTimeValue>       values;
23
24         public void add(AltosTimeValue tv) {
25                 values.add(tv);
26         }
27
28         public void add(double time, double value) {
29                 add(new AltosTimeValue(time, value));
30         }
31
32         public AltosTimeValue get(int i) {
33                 return values.get(i);
34         }
35
36         private double lerp(AltosTimeValue v0, AltosTimeValue v1, double t) {
37                 /* degenerate case */
38                 if (v0.time == v1.time)
39                         return (v0.value + v1.value) / 2;
40
41                 return (v0.value * (v1.time - t) + v1.value * (t - v0.time)) / v1.time - v0.time;
42         }
43
44         private int after_index(double time) {
45                 int     lo = 0;
46                 int     hi = values.size() - 1;
47
48                 while (lo <= hi) {
49                         int mid = (lo + hi) / 2;
50
51                         if (values.get(mid).time < time)
52                                 lo = mid + 1;
53                         else
54                                 hi = mid - 1;
55                 }
56                 return lo;
57         }
58
59         /* Compute a value for an arbitrary time */
60         public double value(double time) {
61                 int after = after_index(time);
62                 if (after == 0)
63                         return values.get(0).value;
64                 if (after == values.size())
65                         return values.get(after - 1).value;
66
67                 return lerp(values.get(after-1), values.get(after), time);
68         }
69
70         /* Find the value just before an arbitrary time */
71         public double value_before(double time) {
72                 int after = after_index(time);
73
74                 if (after == 0)
75                         return values.get(0).value;
76                 return values.get(after-1).value;
77         }
78
79         /* Find the value just after an arbitrary time */
80         public double value_after(double time) {
81                 int after = after_index(time);
82
83                 if (after == values.size())
84                         return values.get(after-1).value;
85                 return values.get(after).value;
86         }
87
88         public int size() {
89                 return values.size();
90         }
91
92         public Iterator<AltosTimeValue> iterator() {
93                 return values.iterator();
94         }
95
96         public double max() {
97                 double max = AltosLib.MISSING;
98                 for (AltosTimeValue tv : values) {
99                         if (max == AltosLib.MISSING || tv.value > max)
100                                 max = tv.value;
101                 }
102                 return max;
103         }
104
105         public double min() {
106                 double min = AltosLib.MISSING;
107                 for (AltosTimeValue tv : values) {
108                         if (min == AltosLib.MISSING || tv.value < min)
109                                 min = tv.value;
110                 }
111                 return min;
112         }
113
114         public AltosTimeSeries integrate(AltosTimeSeries integral) {
115                 double  value = 0.0;
116                 double  pvalue = 0.0;
117                 double  time = 0.0;
118                 boolean start = true;
119
120                 for (AltosTimeValue v : values) {
121                         if (start) {
122                                 value = 0.0;
123                                 start = false;
124                         } else {
125                                 value += (pvalue + v.value) / 2.0 * (v.time - time);
126                         }
127                         pvalue = v.value;
128                         time = v.time;
129 //                      System.out.printf("%g %g %g\n", time, v.value, value);
130                         integral.add(time, value);
131
132                 }
133                 return integral;
134         }
135
136         public AltosTimeSeries differentiate(AltosTimeSeries diff) {
137                 double value = 0.0;
138                 double time = 0.0;
139                 boolean start = true;
140
141                 for (AltosTimeValue v: values) {
142                         if (start) {
143                                 value = v.value;
144                                 time = v.time;
145                                 start = false;
146                         } else {
147                                 double  dx = v.time - time;
148                                 double  dy = v.value - value;
149
150                                 if (dx != 0)
151                                         diff.add(time, dy/dx);
152
153                                 time = v.time;
154                                 value = v.value;
155                         }
156                 }
157                 return diff;
158         }
159
160         private int find_left(int i, double dt) {
161                 int j;
162                 double t = values.get(i).time - dt;
163                 for (j = i; j >= 0; j--)        {
164                         if (values.get(j).time < t)
165                                 break;
166                 }
167                 return j + 1;
168
169         }
170
171         private int find_right(int i, double dt) {
172                 int j;
173                 double t = values.get(i).time + dt;
174                 for (j = i; j < values.size(); j++) {
175                         if (values.get(j).time > t)
176                                 break;
177                 }
178                 return j - 1;
179
180         }
181
182         private double filter_coeff(double dist, double width) {
183                 double ratio = dist / (width / 2);
184
185                 return Math.cos(ratio * Math.PI / 2);
186         }
187
188         public AltosTimeSeries filter(AltosTimeSeries f, double width) {
189                 double  half_width = width/2;
190                 for (int i = 0; i < values.size(); i++) {
191                         double  center_time = values.get(i).time;
192                         double  left_time = center_time - half_width;
193                         double  right_time = center_time + half_width;
194                         double  total_coeff = 0.0;
195                         double  total_value = 0.0;
196
197                         int     left = find_left(i, half_width);
198                         int     right = find_right(i, half_width);
199
200                         for (int j = left; j <= right; j++) {
201                                 double  j_time = values.get(j).time;
202
203                                 if (left_time <= j_time && j_time <= right_time) {
204                                         double  j_left = j == left ? left_time : values.get(j-1).time;
205                                         double  j_right = j == right ? right_time : values.get(j+1).time;
206                                         double  interval = (j_right - j_left) / 2.0;
207                                         double  coeff = filter_coeff(j_time - center_time, width) * interval;
208
209                                         total_coeff += coeff;
210                                         total_value += coeff * values.get(j).value;
211                                 }
212                         }
213                         if (total_coeff != 0.0)
214                                 f.add(center_time, total_value / total_coeff);
215                 }
216                 return f;
217         }
218
219         public AltosTimeSeries(String label, AltosUnits units) {
220                 this.label = label;
221                 this.units = units;
222                 this.values = new ArrayList<AltosTimeValue>();
223         }
224 }