ao-tools/ao-test-gps: Improve output formatting
[fw/altos] / ao-tools / lib / cc-dsp.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 "cephes.h"
20 #include <math.h>
21 #include <stdlib.h>
22
23 static inline double sqr (double x) { return x * x; }
24
25 /*
26  * Kaiser Window digital filter
27  */
28
29 #if 0
30 /* not used in this program */
31 static double highpass (double n, double m, double wc)
32 {
33         double  alpha = m/2;
34         double  dist;
35
36         dist = n - alpha;
37         if (dist == 0)
38                 return (M_PI/2 - wc) / M_PI;
39         return -sin(dist * (M_PI/2-wc)) / (M_PI * dist);
40 }
41 #endif
42
43 static double lowpass (double n, double m, double wc)
44 {
45         double  alpha = m/2;
46         double  dist;
47         dist = n - alpha;
48         if (dist == 0)
49                 return wc / M_PI;
50         return sin (wc * dist) / (M_PI * dist);
51 }
52
53 static double kaiser (double n, double m, double beta)
54 {
55         double alpha = m / 2;
56         return i0 (beta * sqrt (1 - sqr((n - alpha) / alpha))) / i0(beta);
57 }
58
59 static double beta (double A)
60 {
61         if (A > 50)
62                 return 0.1102 * (A - 8.7);
63         else if (A >= 21)
64                 return 0.5842 * pow((A - 21), 0.4) + 0.07886 * (A - 21);
65         else
66                 return 0.0;
67 }
68
69 static int M (double A, double delta_omega)
70 {
71         if (A > 21)
72                 return ceil ((A - 7.95) / (2.285 * delta_omega));
73         else
74                 return ceil(5.79 / delta_omega);
75 }
76
77 struct filter_param {
78         double omega_pass;
79         double delta_omega;
80         double A;
81         double beta;
82         int M;
83 } filter_param_t;
84
85 static struct filter_param
86 filter (double omega_pass, double omega_stop, double error)
87 {
88         struct filter_param  p;
89         p.omega_pass = omega_pass;
90         p.delta_omega = omega_stop - omega_pass;
91         p.A = -20 * log10 (error);
92         p.beta = beta (p.A);
93         p.M = M (p.A, p.delta_omega);
94         if ((p.M & 1) == 1)
95                 p.M++;
96         return p;
97 }
98
99 static double *
100 make_low_pass_filter(double omega_pass, double omega_stop, double error, int *length_p)
101 {
102         struct filter_param     p = filter(omega_pass, omega_stop, error);
103         int             length;
104         int             n;
105         double          *lpf;
106
107         length = p.M + 1;
108         lpf = calloc (length, sizeof(double));
109         for (n = 0; n < length; n++)
110                 lpf[n] = lowpass(n, p.M, omega_pass) * kaiser(n, p.M, p.beta);
111         *length_p = length;
112         return lpf;
113 }
114
115 static double *
116 convolve(double *d, int d_len, double *e, int e_len)
117 {
118         int w = (e_len - 1) / 2;
119         int n;
120         double *con = calloc (d_len, sizeof (double));
121
122         for (n = 0; n < d_len; n++) {
123                 double  v = 0;
124                 int o;
125                 for (o = -w; o <= w; o++) {
126                         int     p = n + o;
127                         double sample = p < 0 ? d[0] : p >= d_len ? d[d_len-1] : d[p];
128                         v += sample * e[o + w];
129                 }
130                 con[n] = v;
131         }
132         return con;
133 }
134
135 double *
136 cc_low_pass(double *data, int data_len, double omega_pass, double omega_stop, double error)
137 {
138         int fir_len;
139         double *fir = make_low_pass_filter(omega_pass, omega_stop, error, &fir_len);
140         double *result;
141
142         result = convolve(data, data_len, fir, fir_len);
143         free(fir);
144         return result;
145 }