1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) INRIA - Serge STEER <serge.steer@inria.fr>
4 // Copyright (C) 2012 - 2016 - Scilab Enterprises
6 // This file is hereby licensed under the terms of the GNU GPL v2.0,
7 // pursuant to article 5.3.4 of the CeCILL v.2.1.
8 // This file was originally licensed under the terms of the CeCILL v2.1,
9 // and continues to be available under such terms.
10 // For more information, see the COPYING file which you should have received
11 // along with this program.
13 function [y, z] = filter(b, a, x, z)
14 //Implements a direct form II transposed implementation of the standard
21 error(msprintf(_("%s: Wrong number of input arguments: %d to %d expected.\n"), fname, 3, 4));
33 if type_b <> "constant" & type_b <> "polynomial"
34 error(msprintf(_("%s: Wrong type for input argument #%d: Real matrix or polynomial expected.\n"), fname, 1));
37 if type_a <> "constant" & type_a <> "polynomial"
38 error(msprintf(_("%s: Wrong type for input argument #%d: Real matrix or polynomial expected.\n"), fname, 2));
41 if type_x <> "constant"
42 error(msprintf(_("%s: Wrong type for input argument #%d: Real matrix expected.\n"), fname, 3));
45 if type_z <> "constant"
46 error(msprintf(_("%s: Wrong type for input argument #%d: Real matrix expected.\n"), fname, 4));
50 error(msprintf(_("%s: Wrong type for input argument #%d: Real matrix or polynomial expected.\n"), fname, 1));
54 error(msprintf(_("%s: Wrong type for input argument #%d: Real matrix or polynomial expected.\n"), fname, 2));
58 error(msprintf(_("%s: Wrong type for input argument #%d: Real matrix expected.\n"), fname, 3));
62 error(msprintf(_("%s: Wrong type for input argument #%d: Real matrix expected.\n"), fname, 4));
65 if (size(a, "c") <> 1) & (size(a, "r") <> 1)
66 error(msprintf(_("%s: Wrong size for input argument #%d: Vector expected.\n"), fname, 1));
69 if (size(b, "c") <> 1) & (size(b, "r") <> 1)
70 error(msprintf(_("%s: Wrong size for input argument #%d: Vector expected.\n"), fname, 2));
73 if (size(x, "c") <> 1) & (size(x, "r") <> 1)
74 error(msprintf(_("%s: Wrong size for input argument #%d: Vector expected.\n"), fname, 3));
77 if (size(z, "c") <> 1) & (size(z, "r") <> 1)
78 error(msprintf(_("%s: Wrong size for input argument #%d: Vector expected.\n"), fname, 4));
81 // User mixes polynomial and vector notation
82 if type_b == "polynomial" & size(a, "*") <> 1
83 error(msprintf(_("%s: Incompatible input arguments #%d and #%d: a polynomial and 1-by-1 matrix or two polynomials expected.\n"), fname, 1, 2));
86 // User mixes polynomial and vector notation
87 if type_a == "polynomial" & size(b, "*") <> 1
88 error(msprintf(_("%s: Incompatible input arguments #%d and #%d: a polynomial and 1-by-1 matrix or two polynomials expected.\n"), fname, 1, 2));
91 if type_b == "polynomial" | type_a == "polynomial"
97 deg = max(deg_b, deg_a);
98 b = coeff(b, deg:-1:0);
99 a = coeff(a, deg:-1:0);
102 ////remove high order coefficients equal to zero
103 //i = 0; while b($ - i) == 0, i = i + 1; end;
106 ////remove high order coefficients equal to zero
107 //i = 1; while a(i) == 0, i = i + 1; end
111 error(msprintf(_("%s: Wrong value for input argument #%d: First element must not be %s.\n"), fname, 2, "0"));
114 //force vector orientation
115 b = matrix(b, -1, 1);
116 a = matrix(a, -1, 1);
118 x = matrix(x, 1, -1);
124 n = max(size(b, "*"), size(a, "*"))-1;
132 //pad the numerator and denominator if necessary
133 a($ + 1:(n + 1)) = 0;
134 b($ + 1:(n + 1)) = 0;
136 //form state space representation
137 A = [-a(2:$), [eye(n - 1, n - 1); zeros(1, n - 1)] ];
138 B = b(2:$) - a(2:$) * b(1); //C = eye(1, n); D = b(1);
140 [z, X] = ltitr(A, B, x, z);
141 y = X(1, :) + b(1) * x;
147 //make y orientation similar to the x one