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_min(struct cc_timedata *d, double min_time, double max_time)
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) {
35 min = d->data[i].value;
42 cc_timedata_min_mag(struct cc_timedata *d, double min_time, double max_time)
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 || fabs(d->data[i].value) < min) {
55 min = fabs(d->data[i].value);
62 cc_timedata_max(struct cc_timedata *d, double min_time, double max_time)
71 for (i = 0; i < d->num; i++)
72 if (min_time <= d->data[i].time && d->data[i].time <= max_time)
73 if (!set || d->data[i].value > max) {
75 max = d->data[i].value;
82 cc_timedata_max_mag(struct cc_timedata *d, double min_time, double max_time)
91 for (i = 0; i < d->num; i++)
92 if (min_time <= d->data[i].time && d->data[i].time <= max_time)
93 if (!set || fabs(d->data[i].value) > max) {
95 max = fabs(d->data[i].value);
102 cc_timedata_average(struct cc_timedata *td, double start_time, double stop_time)
111 prev_time = start_time;
112 for (i = 0; i < td->num; i++) {
113 if (start_time <= td->data[i].time && td->data[i].time <= stop_time) {
114 if (i < td->num - 1 && td->data[i+1].time < stop_time)
115 next_time = (td->data[i].time + td->data[i+1].time) / 2.0;
117 next_time = stop_time;
118 interval = next_time - prev_time;
119 sum += td->data[i].value * interval;
121 prev_time = next_time;
128 cc_perioddata_limits(struct cc_perioddata *d, double min_time, double max_time, int *start, int *stop)
130 double start_d, stop_d;
134 start_d = ceil((min_time - d->start) / d->step);
137 stop_d = floor((max_time - d->start) / d->step);
138 if (stop_d >= d->num)
140 if (stop_d < start_d)
142 *start = (int) start_d;
143 *stop = (int) stop_d;
148 cc_perioddata_min(struct cc_perioddata *d, double min_time, double max_time)
155 if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
157 min = d->data[start];
159 for (i = start + 1; i <= stop; i++)
160 if (d->data[i] < min) {
168 cc_perioddata_min_mag(struct cc_perioddata *d, double min_time, double max_time)
175 if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
177 min = d->data[start];
179 for (i = start + 1; i <= stop; i++)
180 if (fabs(d->data[i]) < min) {
181 min = fabs(d->data[i]);
188 cc_perioddata_max(struct cc_perioddata *d, double min_time, double max_time)
195 if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
197 max = d->data[start];
199 for (i = start + 1; i <= stop; i++)
200 if (d->data[i] > max) {
208 cc_perioddata_max_mag(struct cc_perioddata *d, double min_time, double max_time)
215 if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
217 max = d->data[start];
219 for (i = start + 1; i <= stop; i++)
220 if (fabs(d->data[i]) > max) {
221 max = fabs(d->data[i]);
228 cc_perioddata_average(struct cc_perioddata *d, double min_time, double max_time)
234 if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
236 for (i = start; i <= stop; i++)
238 return sum / (stop - start + 1);
242 cc_perioddata_average_mag(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++)
251 sum += fabs(d->data[i]);
252 return sum / (stop - start + 1);