41 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL)
78 if (norm == static_cast<T>(0.0))
80 error (
"filter: the first element of A must be non-zero");
85 if (dim < 0 || dim > x_dims.
length ())
87 error (
"filter: DIM must be a valid dimension");
96 if (si_len != ab_len - 1)
98 error (
"filter: first dimension of SI must be of length max (length (a), length (b)) - 1");
104 error (
"filter: dimensionality of SI and X must agree");
110 if (si_dims(i) != x_dims(i-1))
112 error (
"filter: dimensionality of SI and X must agree");
118 if (si_dims(i) != x_dims(i))
120 error (
"filter: dimensionality of SI and X must agree");
128 if (norm != static_cast<T>(1.0))
134 if (a_len <= 1 && si_len <= 0)
140 for (
int i = 0; i < dim; i++)
141 x_stride *= x_dims(i);
148 x_offset = num * x_len;
153 while (x_offset >= x_stride)
155 x_offset -= x_stride;
158 x_offset += x_offset2 * x_stride * x_len;
167 const T *pa = a.
data ();
168 const T *pb = b.
data ();
169 const T *px = x.
data ();
175 i++, idx += x_stride)
177 py[idx] = psi[0] + pb[0] * px[idx];
185 psi[j] = psi[j+1] - pa[j+1] * py[idx] + pb[j+1] * px[idx];
188 psi[si_len-1] = pb[si_len] * px[idx] - pa[si_len] * py[idx];
194 psi[0] = pb[si_len] * px[idx] - pa[si_len] * py[idx];
203 const T *pb = b.
data ();
204 const T *px = x.
data ();
210 i++, idx += x_stride)
212 py[idx] = psi[0] + pb[0] * px[idx];
220 psi[j] = psi[j+1] + pb[j+1] * px[idx];
223 psi[si_len-1] = pb[si_len] * px[idx];
229 psi[0] = pb[1] * px[idx];
238 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL)
265 while (dim < x_dims.
length () && x_dims(dim) <= 1)
269 if (dim == x_dims.
length ())
272 else if (dim < 0 || dim > x_dims.
length ())
274 error (
"filter: DIM must be a valid dimension");
283 for (
int i = dim; i > 0; i--)
284 si_dims(i) = si_dims(i-1);
289 return filter (b, a, x, si, dim);
294 @deftypefn {Built-in Function} {y =} filter (@var{b}, @var{a}, @var{x})\n\
295 @deftypefnx {Built-in Function} {[@var{y}, @var{sf}] =} filter (@var{b}, @var{a}, @var{x}, @var{si})\n\
296 @deftypefnx {Built-in Function} {[@var{y}, @var{sf}] =} filter (@var{b}, @var{a}, @var{x}, [], @var{dim})\n\
297 @deftypefnx {Built-in Function} {[@var{y}, @var{sf}] =} filter (@var{b}, @var{a}, @var{x}, @var{si}, @var{dim})\n\
298 Return the solution to the following linear, time-invariant difference\n\
302 \\sum_{k=0}^N a_{k+1} y_{n-k} = \\sum_{k=0}^M b_{k+1} x_{n-k}, \\qquad\n\
307 @c Set example in small font to prevent overfull line\n\
312 SUM a(k+1) y(n-k) = SUM b(k+1) x(n-k) for 1<=n<=length(x)\n\
322 N=length(a)-1 and M=length(b)-1.\n\
325 $a \\in \\Re^{N-1}$, $b \\in \\Re^{M-1}$, and $x \\in \\Re^P$.\n\
327 The result is calculated over the first non-singleton dimension of @var{x}\n\
328 or over @var{dim} if supplied.\n\
330 An equivalent form of the equation is:\n\
333 y_n = -\\sum_{k=1}^N c_{k+1} y_{n-k} + \\sum_{k=0}^M d_{k+1} x_{n-k}, \\qquad\n\
338 @c Set example in small font to prevent overfull line\n\
343 y(n) = - SUM c(k+1) y(n-k) + SUM d(k+1) x(n-k) for 1<=n<=length(x)\n\
353 c = a/a(1) and d = b/a(1).\n\
356 $c = a/a_1$ and $d = b/a_1$.\n\
359 If the fourth argument @var{si} is provided, it is taken as the\n\
360 initial state of the system and the final state is returned as\n\
361 @var{sf}. The state vector is a column vector whose length is\n\
362 equal to the length of the longest coefficient vector minus one.\n\
363 If @var{si} is not supplied, the initial state vector is set to all\n\
366 In terms of the Z Transform, y is the result of passing the discrete-\n\
367 time signal x through a system characterized by the following rational\n\
371 H(z) = {\\displaystyle\\sum_{k=0}^M d_{k+1} z^{-k}\n\
372 \\over 1 + \\displaystyle\\sum_{k+1}^N c_{k+1} z^{-k}}\n\
382 H(z) = ---------------------\n\
384 1 + SUM c(k+1) z^(-k)\n\
390 @seealso{filter2, fftfilt, freqz}\n\
395 int nargin = args.
length ();
397 if (nargin < 3 || nargin > 5)
403 const char *errmsg =
"filter: arguments a and b must be vectors";
410 dim = args(4).nint_value () - 1;
411 if (dim < 0 || dim >= x_dims.
length ())
413 error (
"filter: DIM must be a valid dimension");
421 while (dim < x_dims.
length () && x_dims(dim) <= 1)
425 if (dim == x_dims.
length ())
429 bool isfloat = (args(0).is_single_type ()
430 || args(1).is_single_type ()
431 || args(2).is_single_type ()
432 || (nargin >= 4 && args(3).is_single_type ()));
434 if (args(0).is_complex_type ()
435 || args(1).is_complex_type ()
436 || args(2).is_complex_type ()
437 || (nargin >= 4 && args(3).is_complex_type ()))
450 if (nargin == 3 || args(3).is_empty ())
458 for (
int i = dim; i > 0; i--)
459 si_dims(i) = si_dims(i-1);
466 si = args(3).float_complex_array_value ();
498 if (nargin == 3 || args(3).is_empty ())
506 for (
int i = dim; i > 0; i--)
507 si_dims(i) = si_dims(i-1);
514 si = args(3).complex_array_value ();
549 if (nargin == 3 || args(3).is_empty ())
557 for (
int i = dim; i > 0; i--)
558 si_dims(i) = si_dims(i-1);
565 si = args(3).float_array_value ();
597 if (nargin == 3 || args(3).is_empty ())
605 for (
int i = dim; i > 0; i--)
606 si_dims(i) = si_dims(i-1);
613 si = args(3).array_value ();