[go: up one dir, main page]

File: AltosTimeSeries.java

package info (click to toggle)
altos 1.9-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 67,272 kB
  • sloc: ansic: 90,607; java: 39,048; makefile: 6,591; sh: 3,008; xml: 1,972; pascal: 1,597
file content (347 lines) | stat: -rw-r--r-- 8,065 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * Copyright © 2017 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package org.altusmetrum.altoslib_13;

import java.util.*;

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));
	}

	public AltosTimeValue get(int i) {
		return values.get(i);
	}

	private double lerp(AltosTimeValue v0, AltosTimeValue v1, double t) {
		/* degenerate case */
		if (v0.time == v1.time)
			return (v0.value + v1.value) / 2;

		return (v0.value * (v1.time - t) + v1.value * (t - v0.time)) / (v1.time - v0.time);
	}

	private int after_index(double time) {
		int	lo = 0;
		int	hi = values.size() - 1;

		while (lo <= hi) {
			int mid = (lo + hi) / 2;

			if (values.get(mid).time < time)
				lo = mid + 1;
			else
				hi = mid - 1;
		}
		return lo;
	}

	/* Compute a value for an arbitrary time */
	public double value(double time) {
		int after = after_index(time);
		double ret;

		if (after == 0)
			ret = values.get(0).value;
		else if (after == values.size())
			ret = values.get(after - 1).value;
		else {
			AltosTimeValue b = values.get(after-1);
			AltosTimeValue a = values.get(after);
			ret = lerp(b, a, time);
		}
		return ret;
	}

	/* Find the value just before an arbitrary time */
	public double value_before(double time) {
		int after = after_index(time);

		if (after == 0)
			return values.get(0).value;
		return values.get(after-1).value;
	}

	/* Find the value just after an arbitrary time */
	public double value_after(double time) {
		int after = after_index(time);

		if (after == values.size())
			return values.get(after-1).value;
		return values.get(after).value;
	}

	public double time_of(double value) {
		double	last = AltosLib.MISSING;
		for (AltosTimeValue v : values) {
			if (v.value >= value)
				return v.time;
			last = v.time;
		}
		return last;
	}

	public int size() {
		return values.size();
	}

	public Iterator<AltosTimeValue> iterator() {
		return values.iterator();
	}

	public AltosTimeValue max() {
		AltosTimeValue max = null;
		for (AltosTimeValue tv : values)
			if (max == null || tv.value > max.value)
				max = tv;
		return max;
	}

	public AltosTimeValue max(double start_time, double end_time) {
		AltosTimeValue max = null;
		for (AltosTimeValue tv : values) {
			if (start_time <= tv.time && tv.time <= end_time)
				if (max == null || tv.value > max.value)
					max = tv;
		}
		return max;
	}

	public AltosTimeValue min() {
		AltosTimeValue min = null;
		for (AltosTimeValue tv : values) {
			if (min == null || tv.value < min.value)
				min = tv;
		}
		return min;
	}

	public AltosTimeValue min(double start_time, double end_time) {
		AltosTimeValue min = null;
		for (AltosTimeValue tv : values) {
			if (start_time <= tv.time && tv.time <= end_time)
				if (min == null || tv.value < min.value)
					min = tv;
		}
		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_value = 0;
		double total_time = 0;
		AltosTimeValue prev = null;
		for (AltosTimeValue tv : values) {
			if (prev != null) {
				total_value += (tv.value + prev.value) / 2 * (tv.time - prev.time);
				total_time += (tv.time - prev.time);
			}
			prev = tv;
		}
		if (total_time == 0)
			return AltosLib.MISSING;
		return total_value / total_time;
	}

	public double average(double start_time, double end_time) {
		double total_value = 0;
		double total_time = 0;
		AltosTimeValue prev = null;
		for (AltosTimeValue tv : values) {
			if (start_time <= tv.time && tv.time <= end_time) {
				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 (total_time == 0)
			return AltosLib.MISSING;
		return total_value / total_time;
	}

	public AltosTimeSeries integrate(AltosTimeSeries integral) {
		double	value = 0.0;
		double	pvalue = 0.0;
		double 	time = 0.0;
		boolean start = true;

		for (AltosTimeValue v : values) {
			if (start) {
				value = 0.0;
				start = false;
			} else {
				value += (pvalue + v.value) / 2.0 * (v.time - time);
			}
			pvalue = v.value;
			time = v.time;
			integral.add(time, value);

		}
		return integral;
	}

	public AltosTimeSeries differentiate(AltosTimeSeries diff) {
		double value = 0.0;
		double time = 0.0;
		boolean start = true;

		for (AltosTimeValue v: values) {
			if (start) {
				value = v.value;
				time = v.time;
				start = false;
			} else {
				double	dx = v.time - time;
				double	dy = v.value - value;

				if (dx != 0)
					diff.add(time, dy/dx);

				time = v.time;
				value = v.value;
			}
		}
		return diff;
	}

	private int find_left(int i, double dt) {
		int j;
		double t = values.get(i).time - dt;
		for (j = i; j >= 0; j--)	{
			if (values.get(j).time < t)
				break;
		}
		return j + 1;

	}

	private int find_right(int i, double dt) {
		int j;
		double t = values.get(i).time + dt;
		for (j = i; j < values.size(); j++) {
			if (values.get(j).time > t)
				break;
		}
		return j - 1;

	}

	private static double i0(double x) {
		double	ds = 1, d = 0, s = 0;

		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;
			double	right_time = center_time + half_width;
			double	total_coeff = 0.0;
			double	total_value = 0.0;

			int	left = find_left(i, half_width);
			int	right = find_right(i, half_width);

			for (int j = left; j <= right; j++) {
				double	j_time = values.get(j).time;

				if (left_time <= j_time && j_time <= right_time) {
					double	j_left = j == left ? left_time : values.get(j-1).time;
					double	j_right = j == right ? right_time : values.get(j+1).time;
					double	interval = (j_right - j_left) / 2.0;
					double	coeff = filter_coeff(j_time - center_time, width) * interval;
					double	value = values.get(j).value;
					double	partial = value * coeff;

					total_coeff += coeff;
					total_value += partial;
				}
			}
			if (total_coeff != 0.0)
				f.add(center_time, total_value / total_coeff);
		}
		return f;
	}

	public AltosTimeSeries(String label, AltosUnits units) {
		this.label = label;
		this.units = units;
		this.values = new ArrayList<AltosTimeValue>();
	}
}