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>, Comparable<AltosTimeSeries> {
21 public AltosUnits units;
22 ArrayList<AltosTimeValue> values;
24 public int compareTo(AltosTimeSeries other) {
25 return label.compareTo(other.label);
28 public void add(AltosTimeValue tv) {
32 public void add(double time, double value) {
33 add(new AltosTimeValue(time, value));
36 public AltosTimeValue get(int i) {
40 private double lerp(AltosTimeValue v0, AltosTimeValue v1, double t) {
42 if (v0.time == v1.time)
43 return (v0.value + v1.value) / 2;
45 return (v0.value * (v1.time - t) + v1.value * (t - v0.time)) / (v1.time - v0.time);
48 private int after_index(double time) {
50 int hi = values.size() - 1;
53 int mid = (lo + hi) / 2;
55 if (values.get(mid).time < time)
63 /* Compute a value for an arbitrary time */
64 public double value(double time) {
65 int after = after_index(time);
69 ret = values.get(0).value;
70 else if (after == values.size())
71 ret = values.get(after - 1).value;
73 AltosTimeValue b = values.get(after-1);
74 AltosTimeValue a = values.get(after);
75 ret = lerp(b, a, time);
80 /* Find the value just before an arbitrary time */
81 public double value_before(double time) {
82 int after = after_index(time);
85 return values.get(0).value;
86 return values.get(after-1).value;
89 /* Find the value just after an arbitrary time */
90 public double value_after(double time) {
91 int after = after_index(time);
93 if (after == values.size())
94 return values.get(after-1).value;
95 return values.get(after).value;
98 public double time_of(double value) {
99 double last = AltosLib.MISSING;
100 for (AltosTimeValue v : values) {
101 if (v.value >= value)
109 return values.size();
112 public Iterator<AltosTimeValue> iterator() {
113 return values.iterator();
116 public AltosTimeValue max() {
117 AltosTimeValue max = null;
118 for (AltosTimeValue tv : values)
119 if (max == null || tv.value > max.value)
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)
134 public AltosTimeValue min() {
135 AltosTimeValue min = null;
136 for (AltosTimeValue tv : values) {
137 if (min == null || tv.value < min.value)
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)
153 public double average() {
156 for (AltosTimeValue tv : values) {
161 return AltosLib.MISSING;
162 return total / count;
165 public double average(double start_time, double end_time) {
168 for (AltosTimeValue tv : values) {
169 if (start_time <= tv.time && tv.time <= end_time) {
175 return AltosLib.MISSING;
176 return total / count;
179 public AltosTimeSeries integrate(AltosTimeSeries integral) {
183 boolean start = true;
185 for (AltosTimeValue v : values) {
190 value += (pvalue + v.value) / 2.0 * (v.time - time);
194 integral.add(time, value);
200 public AltosTimeSeries differentiate(AltosTimeSeries diff) {
203 boolean start = true;
205 for (AltosTimeValue v: values) {
211 double dx = v.time - time;
212 double dy = v.value - value;
215 diff.add(time, dy/dx);
224 private int find_left(int i, double dt) {
226 double t = values.get(i).time - dt;
227 for (j = i; j >= 0; j--) {
228 if (values.get(j).time < t)
235 private int find_right(int i, double dt) {
237 double t = values.get(i).time + dt;
238 for (j = i; j < values.size(); j++) {
239 if (values.get(j).time > t)
246 private double filter_coeff(double dist, double width) {
247 double ratio = dist / (width / 2);
249 return Math.cos(ratio * Math.PI / 2);
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;
261 int left = find_left(i, half_width);
262 int right = find_right(i, half_width);
264 for (int j = left; j <= right; j++) {
265 double j_time = values.get(j).time;
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;
275 total_coeff += coeff;
276 total_value += partial;
279 if (total_coeff != 0.0)
280 f.add(center_time, total_value / total_coeff);
285 public AltosTimeSeries(String label, AltosUnits units) {
288 this.values = new ArrayList<AltosTimeValue>();