altoslib: Create new abstraction underneath AltosState for recording values
[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         List<AltosTimeValue>    values;
23
24         public void add(double x, double y) {
25                 values.add(new AltosTimeValue(x, y));
26         }
27
28         public Iterator<AltosTimeValue> iterator() {
29                 return values.iterator();
30         }
31
32         public void integrate(AltosTimeSeries integral) {
33                 double  y = 0.0;
34                 double  x = 0.0;
35                 boolean start = true;
36
37                 for (AltosTimeValue v : values) {
38                         if (start) {
39                                 y = 0.0;
40                                 x = v.x;
41                                 start = false;
42                         } else {
43                                 y += v.y * (v.x - x);
44                                 x = v.x;
45                         }
46                         integral.add(x, y);
47                 }
48         }
49
50         public void differentiate(AltosTimeSeries diff) {
51                 double y = 0.0;
52                 double x = 0.0;
53                 boolean start = true;
54
55                 for (AltosTimeValue v: values) {
56                         if (start) {
57                                 y = 0.0;
58                                 x = v.x;
59                                 start = false;
60                         } else {
61                                 double  dx = v.x - x;
62                                 double  dy = v.y - y;
63
64                                 x = v.x;
65                                 y = v.y;
66                                 if (dx != 0)
67                                         diff.add(x, dy);
68                         }
69                 }
70         }
71
72         private int find_left(int i, double dt) {
73                 int j;
74                 double t = values.get(i).x - dt;
75                 for (j = i; j > 0; j--) {
76                         if (values.get(j).x < t)
77                                 break;
78                 }
79                 return j;
80
81         }
82
83         public void filter(AltosTimeSeries out, double width) {
84                 for (int i = 0; i < values.size(); i++) {
85                 }
86         }
87
88         public AltosTimeSeries(String label, AltosUnits units) {
89                 this.label = label;
90                 this.units = units;
91                 this.values = new ArrayList<AltosTimeValue>();
92         }
93 }