File: lfilter.c

package info (click to toggle)
python-scipy 0.7.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,500 kB
  • ctags: 36,081
  • sloc: cpp: 216,880; fortran: 76,016; python: 71,576; ansic: 62,118; makefile: 243; sh: 17
file content (330 lines) | stat: -rw-r--r-- 8,760 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <Python.h>

static int
RawFilter(const PyArrayObject *b, const PyArrayObject *a,
	   const PyArrayObject *x, const PyArrayObject *zi,
	   const PyArrayObject *zf, PyArrayObject *y, int axis,
	   BasicFilterFunction *filter_func);

static char doc_linear_filter[] = "(y,Vf) = _linear_filter(b,a,X,Dim=-1,Vi=None)  implemented using Direct Form II transposed flow diagram. If Vi is not given, Vf is not returned.";
 
/*
 * XXX: Error checking not done yet
 */
static PyObject *
sigtools_linear_filter(PyObject * dummy, PyObject * args)
{
	PyObject *b, *a, *X, *Vi;
	PyArrayObject *arY, *arb, *ara, *arX, *arVi, *arVf;
	int axis, typenum, theaxis, st;
	char *ara_ptr, input_flag = 0, *azero;
	intp na, nb, nal;
	BasicFilterFunction *basic_filter;

        axis = -1;
        Vi = NULL;
	if (!PyArg_ParseTuple(args, "OOO|iO", &b, &a, &X, &axis, &Vi)) {
		return NULL;
	}

	typenum = PyArray_ObjectType(b, 0);
	typenum = PyArray_ObjectType(a, typenum);
	typenum = PyArray_ObjectType(X, typenum);
	if (Vi != NULL) {
		typenum = PyArray_ObjectType(Vi, typenum);
	}

	arY = arVf = arVi = NULL;
	ara = (PyArrayObject *) PyArray_ContiguousFromObject(a, typenum, 1, 1);
	arb = (PyArrayObject *) PyArray_ContiguousFromObject(b, typenum, 1, 1);
	arX = (PyArrayObject *) PyArray_FromObject(X, typenum, 0, 0);
        /* XXX: fix failure handling here */
	if (ara == NULL || arb == NULL || arX == NULL) {
		goto fail;
	}

	if (axis < -arX->nd || axis > arX->nd - 1) {
		PyErr_SetString(PyExc_ValueError,
				"selected axis is out of range");
		goto fail;
	}
	if (axis < 0) {
		theaxis = arX->nd + axis;
	} else {
		theaxis = axis;
	}

	if (Vi != NULL) {
                Py_ssize_t nvi;
		arVi = (PyArrayObject *) PyArray_FromObject(Vi, typenum,
							    arX->nd, arX->nd);
		if (arVi == NULL)
			goto fail;

                nvi = PyArray_Size((PyObject *) arVi);
                if (nvi > 0) {
                        input_flag = 1;
                } else {
                        input_flag = 0;
                        Py_DECREF(arVi);
                        arVi = NULL;
                }
	}

	arY = (PyArrayObject *) PyArray_SimpleNew(arX->nd,
						  arX->dimensions, typenum);
	if (arY == NULL) {
		goto fail;
	}

	if (input_flag) {
		arVf = (PyArrayObject *) PyArray_SimpleNew(arVi->nd,
							   arVi->dimensions,
						           typenum);
	}

	basic_filter = BasicFilterFunctions[(int) (arX->descr->type_num)];
	if (basic_filter == NULL) {
		PyErr_SetString(PyExc_ValueError,
				"linear_filter not available for this type");
		goto fail;
	}

	/* Skip over leading zeros in vector representing denominator (a) */
	/* XXX: handle this correctly */
	azero = PyArray_Zero(ara);
	ara_ptr = ara->data;
        nal = PyArray_ITEMSIZE(ara);
	if (memcmp(ara_ptr, azero, nal) == 0) {
		PyErr_SetString(PyExc_ValueError,
				"BUG: filter coefficient a[0] == 0 not supported yet");
                goto fail;
	}
	PyDataMem_FREE(azero);

	na = PyArray_SIZE(ara);
	nb = PyArray_SIZE(arb);
	if (input_flag) {
		if (arVi->dimensions[theaxis] != (na > nb ? na : nb) - 1) {
			PyErr_SetString(PyExc_ValueError,
					"The number of initial conditions must be max([len(a),len(b)]) - 1");
			goto fail;
		}
	}

	st = RawFilter(arb, ara, arX, arVi, arVf, arY, theaxis, basic_filter);
	if (st) {
		goto fail;
	}

	Py_XDECREF(ara);
	Py_XDECREF(arb);
	Py_XDECREF(arX);
	Py_XDECREF(arVi);

	if (!input_flag) {
		return PyArray_Return(arY);
	} else {
		return Py_BuildValue("(NN)", arY, arVf);
	}


fail:
	Py_XDECREF(ara);
	Py_XDECREF(arb);
	Py_XDECREF(arX);
	Py_XDECREF(arVi);
	Py_XDECREF(arVf);
	Py_XDECREF(arY);
	return NULL;
}

static int
zfill(const PyArrayObject *x, intp nx, char* xzfilled, intp nxzfilled)
{
	char *xzero;
	intp i, nxl;

	nxl = PyArray_ITEMSIZE(x);

	/* PyArray_Zero does not take const pointer, hence the cast */
	xzero = PyArray_Zero((PyArrayObject*)x);

	if (nx > 0) {
		memcpy(xzfilled, x->data, nx * nxl);
	}
	for(i = nx; i < nxzfilled; ++i) {
		memcpy(xzfilled + i * nxl, xzero, nxl);
	}

	PyDataMem_FREE(xzero);

	return 0;
}

/*
 * a and b assumed to be contiguous
 *
 * XXX: this code is very conservative, and could be considerably sped up for
 * the usual cases (like contiguity).
 *
 * XXX: the code should be refactored (at least with/without initial
 * condition), some code is wasteful here
 */
static int
RawFilter(const PyArrayObject *b, const PyArrayObject *a,
	   const PyArrayObject *x, const PyArrayObject *zi,
	   const PyArrayObject *zf, PyArrayObject *y, int axis,
	   BasicFilterFunction *filter_func)
{
	PyArrayIterObject *itx, *ity, *itzi, *itzf;
	intp nitx, i, nxl, nzfl, j;
	intp na, nb, nal, nbl;
	intp nfilt;
	char *azfilled, *bzfilled, *zfzfilled, *yoyo;
	PyArray_CopySwapFunc *copyswap = x->descr->f->copyswap;

	itx = (PyArrayIterObject *)PyArray_IterAllButAxis(
		(PyObject *)x, &axis);
	if (itx == NULL) {
		PyErr_SetString(PyExc_MemoryError,
				"Could not create itx");
		goto fail;
	}
	nitx = itx->size;

	ity = (PyArrayIterObject *)PyArray_IterAllButAxis(
		(PyObject *)y, &axis);
	if (ity == NULL) {
		PyErr_SetString(PyExc_MemoryError,
				"Could not create ity");
		goto clean_itx;
	}

        if (zi != NULL) {
                itzi = (PyArrayIterObject *)PyArray_IterAllButAxis(
                        (PyObject *)zi, &axis);
                if (itzi == NULL) {
			PyErr_SetString(PyExc_MemoryError,
					"Could not create itzi");
			goto clean_ity;
                }

                itzf = (PyArrayIterObject *)PyArray_IterAllButAxis(
                        (PyObject *)zf, &axis);
                if (itzf == NULL) {
			PyErr_SetString(PyExc_MemoryError,
					"Could not create itzf");
			goto clean_itzi;
                }
        }

	na = PyArray_SIZE(a);
	nal = PyArray_ITEMSIZE(a);
	nb = PyArray_SIZE(b);
	nbl = PyArray_ITEMSIZE(b);

	nfilt = na > nb ? na : nb;

	azfilled = malloc(nal * nfilt);
	if (azfilled == NULL) {
		PyErr_SetString(PyExc_MemoryError,
				"Could not create azfilled");
		goto clean_itzf;
	}
	bzfilled = malloc(nbl * nfilt);
	if (bzfilled == NULL) {
		PyErr_SetString(PyExc_MemoryError,
				"Could not create bzfilled");
		goto clean_azfilled;
	}

	nxl = PyArray_ITEMSIZE(x);
	zfzfilled = malloc(nxl * (nfilt-1) );
	if (zfzfilled == NULL) {
		PyErr_SetString(PyExc_MemoryError,
				"Could not create zfzfilled");
		goto clean_bzfilled;
	}
	/* Initialize zfzilled to 0, so that we can use Py_XINCREF/Py_XDECREF
	 * on it for object arrays (necessary for copyswap to work correctly).
	 * Stricly speaking, it is not needed for fundamental types (as values
	 * are copied instead of pointers, without refcounts), but oh well...
	 */
	memset(zfzfilled, 0, nxl * (nfilt-1));

	zfill(a, na, azfilled, nfilt);
	zfill(b, nb, bzfilled, nfilt);

        /* XXX: Check that zf and zi have same type ? */
        if (zf != NULL) {
                nzfl = PyArray_ITEMSIZE(zf);
        } else {
                nzfl = 0;
        }

        /* Iterate over the input array */
        for(i = 0; i < nitx; ++i) {
                if (zi != NULL) {
                        yoyo = itzi->dataptr;
                        /* Copy initial conditions zi in zfzfilled buffer */
                        for(j = 0; j < nfilt - 1; ++j) {
                                copyswap(zfzfilled + j * nzfl, yoyo, 0, NULL);
                                yoyo += itzi->strides[axis];
                        }
                        PyArray_ITER_NEXT(itzi);
                } else {
                        zfill(x, 0, zfzfilled, nfilt-1);
                }

                filter_func(bzfilled, azfilled,
                            itx->dataptr, ity->dataptr, zfzfilled,
                            nfilt, PyArray_DIM(x, axis), itx->strides[axis],
                            ity->strides[axis]);
                PyArray_ITER_NEXT(itx);
                PyArray_ITER_NEXT(ity);

                /* Copy tmp buffer fo final values back into zf output array */
                if (zi != NULL) {
                        yoyo = itzf->dataptr;
                        for(j = 0; j < nfilt - 1; ++j) {
                                copyswap(yoyo, zfzfilled + j * nzfl, 0, NULL);
                                yoyo += itzf->strides[axis];
                        }
                        PyArray_ITER_NEXT(itzf);
                }
	}

	/* Free up allocated memory */
	free(zfzfilled);
	free(bzfilled);
	free(azfilled);

	if (zi != NULL) {
                Py_DECREF(itzf);
                Py_DECREF(itzi);
        }
	Py_DECREF(ity);
	Py_DECREF(itx);

	return 0;

clean_bzfilled:
	free(bzfilled);
clean_azfilled:
	free(azfilled);
clean_itzf:
	if (zf != NULL) {
		Py_DECREF(itzf);
	}
clean_itzi:
	if (zi != NULL) {
		Py_DECREF(itzi);
	}
clean_ity:
	Py_DECREF(ity);
clean_itx:
	Py_DECREF(itx);
fail:
	return -1;
}