summaryrefslogtreecommitdiff
path: root/altoslib/AltosTimeSeries.java
blob: 1d3d9a6c5185878fb6400412f400c23d2a831581 (plain) (blame)
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
/*
 * 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_11;

import java.util.*;

public class AltosTimeSeries implements Iterable<AltosTimeValue> {
	public String		label;
	public AltosUnits	units;
	List<AltosTimeValue>	values;

	public void add(AltosTimeValue tv) {
		values.add(tv);
	}

	public void add(double time, double value) {
		add(new AltosTimeValue(time, value));
	}

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

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

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

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

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

	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;
//			System.out.printf("%g %g %g\n", time, v.value, value);
			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 double filter_coeff(double dist, double width) {
		double ratio = dist / (width / 2);

		return Math.cos(ratio * Math.PI / 2);
	}

	public AltosTimeSeries filter(AltosTimeSeries f, double width) {
		double	half_width = width/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	coeff = filter_coeff(j_time - center_time, width);
					total_coeff += coeff;
					total_value += coeff * values.get(j).value;
				}
			}
			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>();
	}
}