Add plots to ao-postflight using the plplot library
[fw/altos] / ao-tools / lib / cc-period.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 <stdlib.h>
20
21 struct cc_perioddata *
22 cc_period_make(struct cc_timedata *td, double start_time, double stop_time)
23 {
24         int                     len = stop_time - start_time + 1;
25         struct cc_perioddata    *pd;
26         int                     i;
27         double                  prev_time;
28         double                  next_time;
29         double                  interval;
30
31         pd = calloc(1, sizeof (struct cc_perioddata));
32         pd->start = start_time;
33         pd->step = 1;
34         pd->num = len;
35         pd->data = calloc(len, sizeof(double));
36         prev_time = start_time;
37         for (i = 0; i < td->num; i++) {
38                 if (start_time <= td->data[i].time && td->data[i].time <= stop_time) {
39                         int     pos = td->data[i].time - start_time;
40
41                         if (i < td->num - 1 && td->data[i+1].time < stop_time)
42                                 next_time = (td->data[i].time + td->data[i+1].time) / 2.0;
43                         else
44                                 next_time = stop_time;
45                         interval = next_time - prev_time;
46                         pd->data[pos] = td->data[i].value * interval;
47                         prev_time = next_time;
48                 }
49         }
50         return pd;
51 }
52
53 struct cc_perioddata *
54 cc_period_low_pass(struct cc_perioddata *raw, double omega_pass, double omega_stop, double error)
55 {
56         struct cc_perioddata *filtered;
57
58         filtered = calloc (1, sizeof (struct cc_perioddata));
59         filtered->start = raw->start;
60         filtered->step = raw->step;
61         filtered->num = raw->num;
62         filtered->data = cc_low_pass(raw->data, raw->num, omega_pass, omega_stop, error);
63         return filtered;
64 }