altos: Remove unused ao_adc_get from ao_adc_stm.c
[fw/altos] / ao-tools / lib / cc-integrate.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; 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  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "cc.h"
20 #include <stdlib.h>
21
22 struct cc_timedata *
23 cc_timedata_convert(struct cc_timedata *d, double (*f)(double v, double a), double a)
24 {
25         struct cc_timedata      *r;
26         int                     n;
27
28         r = calloc (1, sizeof (struct cc_timedata));
29         r->num = d->num;
30         r->size = d->num;
31         r->data = calloc (r->size, sizeof (struct cc_timedataelt));
32         r->time_offset = d->time_offset;
33         for (n = 0; n < d->num; n++) {
34                 r->data[n].time = d->data[n].time;
35                 r->data[n].value = f(d->data[n].value, a);
36         }
37         return r;
38 }
39
40 struct cc_timedata *
41 cc_timedata_integrate(struct cc_timedata *d, double min_time, double max_time)
42 {
43         struct cc_timedata      *i;
44         int                     n, m;
45         int                     start, stop;
46
47         cc_timedata_limits(d, min_time, max_time, &start, &stop);
48         i = calloc (1, sizeof (struct cc_timedata));
49         i->num = stop - start + 1;
50         i->size = i->num;
51         i->data = calloc (i->size, sizeof (struct cc_timedataelt));
52         i->time_offset = d->data[start].time;
53         for (n = 0; n < i->num; n++) {
54                 m = n + start;
55                 i->data[n].time = d->data[m].time;
56                 if (n == 0) {
57                         i->data[n].value = 0;
58                 } else {
59                         i->data[n].value = i->data[n-1].value +
60                                 (d->data[m].value + d->data[m-1].value) / 2 *
61                                 ((d->data[m].time - d->data[m-1].time) / 100.0);
62                 }
63         }
64         return i;
65 }
66
67 struct cc_perioddata *
68 cc_perioddata_differentiate(struct cc_perioddata *i)
69 {
70         struct cc_perioddata    *d;
71         int                     n;
72
73         d = calloc (1, sizeof (struct cc_perioddata));
74         d->num = i->num;
75         d->start = i->start;
76         d->step = i->step;
77         d->data = calloc (d->num, sizeof(double));
78         for (n = 1; n < d->num; n++)
79                 d->data[n] = (i->data[n] - i->data[n-1]) / (i->step / 100.0);
80         d->data[0] = d->data[1];
81         return d;
82 }