Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / ao-tools / lib / cc-analyse.c
1 /*
2  * Copyright © 2009 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "cc.h"
19 #include <math.h>
20
21 int
22 cc_timedata_min(struct cc_timedata *d, double min_time, double max_time)
23 {
24         int     i;
25         int     set = 0;
26         int     min_i = -1;
27         double  min;
28
29         if (d->num == 0)
30                 return -1;
31         for (i = 0; i < d->num; i++)
32                 if (min_time <= d->data[i].time && d->data[i].time <= max_time)
33                         if (!set || d->data[i].value < min) {
34                                 min_i = i;
35                                 min = d->data[i].value;
36                                 set = 1;
37                         }
38         return min_i;
39 }
40
41 int
42 cc_timedata_max(struct cc_timedata *d, double min_time, double max_time)
43 {
44         int     i;
45         double  max;
46         int     max_i = -1;
47         int     set = 0;
48
49         if (d->num == 0)
50                 return -1;
51         for (i = 0; i < d->num; i++)
52                 if (min_time <= d->data[i].time && d->data[i].time <= max_time)
53                         if (!set || d->data[i].value > max) {
54                                 max_i = i;
55                                 max = d->data[i].value;
56                                 set = 1;
57                         }
58         return max_i;
59 }
60
61 int
62 cc_perioddata_min(struct cc_perioddata *d, double min_time, double max_time)
63 {
64         int     start, stop;
65         int     i;
66         double  min;
67         int     min_i;
68
69         if (d->num == 0)
70                 return -1;
71         start = (int) ceil((min_time - d->start) / d->step);
72         if (start < 0)
73                 start = 0;
74         stop = (int) floor((max_time - d->start) / d->step);
75         if (stop >= d->num)
76                 stop = d->num - 1;
77         if (stop < start)
78                 return -1;
79         min = d->data[start];
80         min_i = start;
81         for (i = start + 1; i <= stop; i++)
82                 if (d->data[i] < min) {
83                         min = d->data[i];
84                         min_i = i;
85                 }
86         return min_i;
87 }
88
89 int
90 cc_perioddata_max(struct cc_perioddata *d, double min_time, double max_time)
91 {
92         int     start, stop;
93         int     i;
94         double  max;
95         int     max_i;
96
97         if (d->num == 0)
98                 return -1;
99         start = (int) ceil((min_time - d->start) / d->step);
100         if (start < 0)
101                 start = 0;
102         stop = (int) floor((max_time - d->start) / d->step);
103         if (stop >= d->num)
104                 stop = d->num - 1;
105         if (stop < start)
106                 return -1;
107         max = d->data[start];
108         max_i = start;
109         for (i = start + 1; i <= stop; i++)
110                 if (fabs(d->data[i]) > max) {
111                         max = fabs(d->data[i]);
112                         max_i = i;
113                 }
114         return max_i;
115 }