2a4e59521907a36d621199c2e1a6a6e19d5f8072
[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 #include <math.h>
21
22 struct cc_perioddata *
23 cc_period_make(struct cc_timedata *td, double start_time, double stop_time)
24 {
25         int                     len = stop_time - start_time + 1;
26         struct cc_perioddata    *pd;
27         int                     i, j;
28         double                  t;
29
30         pd = calloc(1, sizeof (struct cc_perioddata));
31         pd->start = start_time;
32         pd->step = 1;
33         pd->num = len;
34         pd->data = calloc(len, sizeof(double));
35         j = 0;
36         for (i = 0; i < pd->num; i++) {
37                 t = start_time + i * pd->step;
38                 while (j < td->num - 1 && fabs(t - td->data[j].time) > fabs(t - td->data[j+1].time))
39                         j++;
40                 pd->data[i] = td->data[j].value;
41         }
42         return pd;
43 }
44
45 struct cc_perioddata *
46 cc_period_low_pass(struct cc_perioddata *raw, double omega_pass, double omega_stop, double error)
47 {
48         struct cc_perioddata *filtered;
49
50         filtered = calloc (1, sizeof (struct cc_perioddata));
51         filtered->start = raw->start;
52         filtered->step = raw->step;
53         filtered->num = raw->num;
54         filtered->data = cc_low_pass(raw->data, raw->num, omega_pass, omega_stop, error);
55         return filtered;
56 }