2 * Copyright (C) <2001> David A. Schleef <ds@schleef.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
29 #include "functable.h"
35 functable_func_sinc (double *fx, double *dfx, double x, void *closure)
44 *dfx = (cos (x) - sin (x) / x) / x;
48 functable_func_boxcar (double *fx, double *dfx, double x, void *closure)
50 double width = *(double *) closure;
52 if (x < width && x > -width) {
61 functable_func_hanning (double *fx, double *dfx, double x, void *closure)
63 double width = *(double *) closure;
65 if (x < width && x > -width) {
67 *fx = (1 - x * x) * (1 - x * x);
68 *dfx = -2 * 2 * x / width * (1 - x * x);
81 ft = malloc (sizeof (Functable));
82 memset (ft, 0, sizeof (Functable));
88 functable_free (Functable * ft)
94 functable_set_length (Functable * t, int length)
100 functable_set_offset (Functable * t, double offset)
106 functable_set_multiplier (Functable * t, double multiplier)
108 t->multiplier = multiplier;
112 functable_calculate (Functable * t, FunctableFunc func, void *closure)
122 t->fx = malloc (sizeof (double) * (t->length + 1));
123 t->dfx = malloc (sizeof (double) * (t->length + 1));
125 t->inv_multiplier = 1.0 / t->multiplier;
127 for (i = 0; i < t->length + 1; i++) {
128 x = t->offset + t->multiplier * i;
130 func (&t->fx[i], &t->dfx[i], x, closure);
135 functable_calculate_multiply (Functable * t, FunctableFunc func, void *closure)
140 for (i = 0; i < t->length + 1; i++) {
141 double afx, adfx, bfx, bdfx;
145 x = t->offset + t->multiplier * i;
146 func (&bfx, &bdfx, x, closure);
147 t->fx[i] = afx * bfx;
148 t->dfx[i] = afx * bdfx + adfx * bfx;
154 functable_evaluate (Functable * t, double x)
157 double f0, f1, w0, w1;
161 if (x < t->offset || x > (t->offset + t->length * t->multiplier)) {
162 RESAMPLE_DEBUG ("x out of range %g", x);
166 x *= t->inv_multiplier;
173 f1 = 3 * x2 - 2 * x3;
175 w0 = (x - 2 * x2 + x3) * t->multiplier;
176 w1 = (-x2 + x3) * t->multiplier;
178 w = t->fx[i] * f0 + t->fx[i + 1] * f1 + t->dfx[i] * w0 + t->dfx[i + 1] * w1;
180 /*w = t->fx[i] * (1-x) + t->fx[i+1] * x; */
187 functable_fir (Functable * t, double x, int n, double *data, int len)
190 double f0, f1, w0, w1;
203 f1 = 3 * x2 - 2 * x3;
205 w0 = (x - 2 * x2 + x3) * t->multiplier;
206 w1 = (-x2 + x3) * t->multiplier;
209 for (j = 0; j < len; j++) {
210 w = t->fx[i] * f0 + t->fx[i + 1] * f1 + t->dfx[i] * w0 + t->dfx[i + 1] * w1;
211 sum += data[j * 2] * w;
219 functable_fir2 (Functable * t, double *r0, double *r1, double x,
220 int n, double *data, int len)
223 double f0, f1, w0, w1;
230 x *= t->inv_multiplier;
238 f1 = 3 * x2 - 2 * x3;
240 w0 = (x - 2 * x2 + x3) * t->multiplier;
241 w1 = (-x2 + x3) * t->multiplier;
245 for (j = 0; j < len; j++) {
246 w = t->fx[i] * f0 + t->fx[i + 1] * f1 + t->dfx[i] * w0 + t->dfx[i + 1] * w1;
247 sum0 += data[j * 2] * w;
248 sum1 += data[j * 2 + 1] * w;