2 * Copyright © 2009 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; version 2 of the License.
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.
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.
22 cc_timedata_limits(struct cc_timedata *d, double min_time, double max_time, int *start, int *stop)
27 for (i = 0; i < d->num; i++) {
28 if (*start < 0 && min_time <= d->data[i].time)
30 if (d->data[i].time <= max_time)
36 cc_timedata_min(struct cc_timedata *d, double min_time, double max_time)
45 for (i = 0; i < d->num; i++)
46 if (min_time <= d->data[i].time && d->data[i].time <= max_time)
47 if (!set || d->data[i].value < min) {
49 min = d->data[i].value;
56 cc_timedata_min_mag(struct cc_timedata *d, double min_time, double max_time)
65 for (i = 0; i < d->num; i++)
66 if (min_time <= d->data[i].time && d->data[i].time <= max_time)
67 if (!set || fabs(d->data[i].value) < min) {
69 min = fabs(d->data[i].value);
76 cc_timedata_max(struct cc_timedata *d, double min_time, double max_time)
85 for (i = 0; i < d->num; i++)
86 if (min_time <= d->data[i].time && d->data[i].time <= max_time)
87 if (!set || d->data[i].value > max) {
89 max = d->data[i].value;
96 cc_timedata_max_mag(struct cc_timedata *d, double min_time, double max_time)
105 for (i = 0; i < d->num; i++)
106 if (min_time <= d->data[i].time && d->data[i].time <= max_time)
107 if (!set || fabs(d->data[i].value) > max) {
109 max = fabs(d->data[i].value);
116 cc_timedata_average(struct cc_timedata *td, double start_time, double stop_time)
125 prev_time = start_time;
126 for (i = 0; i < td->num; i++) {
127 if (start_time <= td->data[i].time && td->data[i].time <= stop_time) {
128 if (i < td->num - 1 && td->data[i+1].time < stop_time)
129 next_time = (td->data[i].time + td->data[i+1].time) / 2.0;
131 next_time = stop_time;
132 interval = next_time - prev_time;
133 sum += td->data[i].value * interval;
135 prev_time = next_time;
142 cc_perioddata_limits(struct cc_perioddata *d, double min_time, double max_time, int *start, int *stop)
144 double start_d, stop_d;
148 start_d = ceil((min_time - d->start) / d->step);
151 stop_d = floor((max_time - d->start) / d->step);
152 if (stop_d >= d->num)
154 if (stop_d < start_d)
156 *start = (int) start_d;
157 *stop = (int) stop_d;
162 cc_perioddata_min(struct cc_perioddata *d, double min_time, double max_time)
169 if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
171 min = d->data[start];
173 for (i = start + 1; i <= stop; i++)
174 if (d->data[i] < min) {
182 cc_perioddata_min_mag(struct cc_perioddata *d, double min_time, double max_time)
189 if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
191 min = d->data[start];
193 for (i = start + 1; i <= stop; i++)
194 if (fabs(d->data[i]) < min) {
195 min = fabs(d->data[i]);
202 cc_perioddata_max(struct cc_perioddata *d, double min_time, double max_time)
209 if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
211 max = d->data[start];
213 for (i = start + 1; i <= stop; i++)
214 if (d->data[i] > max) {
222 cc_perioddata_max_mag(struct cc_perioddata *d, double min_time, double max_time)
229 if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
231 max = d->data[start];
233 for (i = start + 1; i <= stop; i++)
234 if (fabs(d->data[i]) > max) {
235 max = fabs(d->data[i]);
242 cc_perioddata_average(struct cc_perioddata *d, double min_time, double max_time)
248 if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
250 for (i = start; i <= stop; i++)
252 return sum / (stop - start + 1);
256 cc_perioddata_average_mag(struct cc_perioddata *d, double min_time, double max_time)
262 if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
264 for (i = start; i <= stop; i++)
265 sum += fabs(d->data[i]);
266 return sum / (stop - start + 1);