2 * Copyright © 2017 Keith Packard <keithp@keithp.com>
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.
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.
15 package org.altusmetrum.altoslib_11;
19 public class AltosTimeSeries implements Iterable<AltosTimeValue> {
21 public AltosUnits units;
22 ArrayList<AltosTimeValue> values;
24 public void add(AltosTimeValue tv) {
28 public void add(double time, double value) {
29 add(new AltosTimeValue(time, value));
32 public AltosTimeValue get(int i) {
36 private double lerp(AltosTimeValue v0, AltosTimeValue v1, double t) {
38 if (v0.time == v1.time)
39 return (v0.value + v1.value) / 2;
41 return (v0.value * (v1.time - t) + v1.value * (t - v0.time)) / (v1.time - v0.time);
44 private int after_index(double time) {
46 int hi = values.size() - 1;
49 int mid = (lo + hi) / 2;
51 if (values.get(mid).time < time)
59 /* Compute a value for an arbitrary time */
60 public double value(double time) {
61 int after = after_index(time);
65 ret = values.get(0).value;
66 else if (after == values.size())
67 ret = values.get(after - 1).value;
69 AltosTimeValue b = values.get(after-1);
70 AltosTimeValue a = values.get(after);
71 ret = lerp(b, a, time);
76 /* Find the value just before an arbitrary time */
77 public double value_before(double time) {
78 int after = after_index(time);
81 return values.get(0).value;
82 return values.get(after-1).value;
85 /* Find the value just after an arbitrary time */
86 public double value_after(double time) {
87 int after = after_index(time);
89 if (after == values.size())
90 return values.get(after-1).value;
91 return values.get(after).value;
94 public double time_of(double value) {
95 double last = AltosLib.MISSING;
96 for (AltosTimeValue v : values) {
105 return values.size();
108 public Iterator<AltosTimeValue> iterator() {
109 return values.iterator();
112 public AltosTimeValue max() {
113 AltosTimeValue max = null;
114 for (AltosTimeValue tv : values)
115 if (max == null || tv.value > max.value)
120 public AltosTimeValue max(double start_time, double end_time) {
121 AltosTimeValue max = null;
122 for (AltosTimeValue tv : values) {
123 if (start_time <= tv.time && tv.time <= end_time)
124 if (max == null || tv.value > max.value)
130 public AltosTimeValue min() {
131 AltosTimeValue min = null;
132 for (AltosTimeValue tv : values) {
133 if (min == null || tv.value < min.value)
139 public AltosTimeValue min(double start_time, double end_time) {
140 AltosTimeValue min = null;
141 for (AltosTimeValue tv : values) {
142 if (start_time <= tv.time && tv.time <= end_time)
143 if (min == null || tv.value < min.value)
149 public double average() {
152 for (AltosTimeValue tv : values) {
157 return AltosLib.MISSING;
158 return total / count;
161 public double average(double start_time, double end_time) {
164 for (AltosTimeValue tv : values) {
165 if (start_time <= tv.time && tv.time <= end_time) {
171 return AltosLib.MISSING;
172 return total / count;
175 public AltosTimeSeries integrate(AltosTimeSeries integral) {
179 boolean start = true;
181 for (AltosTimeValue v : values) {
186 value += (pvalue + v.value) / 2.0 * (v.time - time);
190 integral.add(time, value);
196 public AltosTimeSeries differentiate(AltosTimeSeries diff) {
199 boolean start = true;
201 for (AltosTimeValue v: values) {
207 double dx = v.time - time;
208 double dy = v.value - value;
211 diff.add(time, dy/dx);
220 private int find_left(int i, double dt) {
222 double t = values.get(i).time - dt;
223 for (j = i; j >= 0; j--) {
224 if (values.get(j).time < t)
231 private int find_right(int i, double dt) {
233 double t = values.get(i).time + dt;
234 for (j = i; j < values.size(); j++) {
235 if (values.get(j).time > t)
242 private double filter_coeff(double dist, double width) {
243 double ratio = dist / (width / 2);
245 return Math.cos(ratio * Math.PI / 2);
248 public AltosTimeSeries filter(AltosTimeSeries f, double width) {
249 double half_width = width/2;
250 for (int i = 0; i < values.size(); i++) {
251 double center_time = values.get(i).time;
252 double left_time = center_time - half_width;
253 double right_time = center_time + half_width;
254 double total_coeff = 0.0;
255 double total_value = 0.0;
257 int left = find_left(i, half_width);
258 int right = find_right(i, half_width);
260 for (int j = left; j <= right; j++) {
261 double j_time = values.get(j).time;
263 if (left_time <= j_time && j_time <= right_time) {
264 double j_left = j == left ? left_time : values.get(j-1).time;
265 double j_right = j == right ? right_time : values.get(j+1).time;
266 double interval = (j_right - j_left) / 2.0;
267 double coeff = filter_coeff(j_time - center_time, width) * interval;
268 double value = values.get(j).value;
269 double partial = value * coeff;
271 total_coeff += coeff;
272 total_value += partial;
275 if (total_coeff != 0.0)
276 f.add(center_time, total_value / total_coeff);
281 public AltosTimeSeries(String label, AltosUnits units) {
284 this.values = new ArrayList<AltosTimeValue>();