1 // =============================================================================
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) 2014 - Scilab Enterprises - Pierre-Aime Agnel
5 // This file is distributed under the same license as the Scilab package.
6 // =============================================================================
8 // <-- unit tests for function filter -->
10 // <-- CLI SHELL MODE -->
12 //==============================================================================
13 // Error handling tests
14 //==============================================================================
17 func_call = "filter(B, A, x)";
20 err_msg = msprintf(_("%s: Wrong type for input argument #%d: Real matrix or polynomial expected.\n"), fname, 1);
24 assert_checkerror(func_call, err_msg);
26 err_msg = msprintf(_("%s: Wrong type for input argument #%d: Real matrix or polynomial expected.\n"), fname, 2);
29 assert_checkerror(func_call, err_msg);
31 err_msg = msprintf(_("%s: Wrong type for input argument #%d: Real matrix expected.\n"), fname, 3);
35 assert_checkerror(func_call, err_msg);
37 err_msg = msprintf(_("%s: Wrong type for input argument #%d: Real matrix expected.\n"), fname, 4);
40 func_call = "filter(B, A, x, z)";
41 assert_checkerror(func_call, err_msg);
44 err_msg = msprintf(_("%s: Wrong type for input argument #%d: Real matrix or polynomial expected.\n"), fname, 1);
47 assert_checkerror(func_call, err_msg);
49 err_msg = msprintf(_("%s: Wrong type for input argument #%d: Real matrix or polynomial expected.\n"), fname, 2);
52 assert_checkerror(func_call, err_msg);
54 err_msg = msprintf(_("%s: Wrong type for input argument #%d: Real matrix expected.\n"), fname, 3);
57 assert_checkerror(func_call, err_msg);
61 err_msg = msprintf(_("%s: Wrong type for input argument #%d: Real matrix expected.\n"), fname, 4);
62 assert_checkerror(func_call, err_msg);
64 // Check vector values
67 err_msg = msprintf(_("%s: Wrong size for input argument #%d: Vector expected.\n"), fname, 1);
68 assert_checkerror(func_call, err_msg);
72 err_msg = msprintf(_("%s: Wrong size for input argument #%d: Vector expected.\n"), fname, 2);
73 assert_checkerror(func_call, err_msg);
77 err_msg = msprintf(_("%s: Wrong size for input argument #%d: Vector expected.\n"), fname, 3);
78 assert_checkerror(func_call, err_msg);
82 err_msg = msprintf(_("%s: Wrong size for input argument #%d: Vector expected.\n"), fname, 4);
83 assert_checkerror(func_call, err_msg);
85 // User mixes polynomial and vector notation
89 err_msg = msprintf(_("%s: Incompatible input arguments #%d and #%d: a polynomial and 1-by-1 matrix or two polynomials expected.\n"), fname, 1, 2);
90 assert_checkerror(func_call, err_msg);
94 err_msg = msprintf(_("%s: Incompatible input arguments #%d and #%d: a polynomial and 1-by-1 matrix or two polynomials expected.\n"), fname, 1, 2);
95 assert_checkerror(func_call, err_msg);
97 // Denominator must have first coefficient not equal to 0
99 err_msg = msprintf(_("%s: Wrong value for input argument #%d: First element must not be %s.\n"), fname, 2, "0");
100 assert_checkerror(func_call, err_msg);
104 err_msg = msprintf(_("%s: Wrong value for input argument #%d: First element must not be %s.\n"), fname, 2, "0");
105 assert_checkerror(func_call, err_msg);
107 //==============================================================================
109 //==============================================================================
111 // Integrator y(n) = y(n - 1) + x(n)
112 // Filter is 1/(1 - 1 * z**-1)
118 res = filter(B, A, x);
119 assert_checkalmostequal(res, y);
121 // Same behaviour with a polynomial
124 res = filter(B, A, x);
125 assert_checkalmostequal(res, y);
127 // Integrator with delay y(n) = y(n-1) + x(n-1)
128 // Filter is z**-1 / (1 + z**-1)
131 res = filter(B, A, x);
132 assert_checkalmostequal(res, y);
136 res = filter(B, A, x);
137 assert_checkalmostequal(res, y);
139 // Derivator y(n) = x(n) - x(n-1)
140 // Filter is 1 - z**-1
145 res = filter(B, A, x);
146 assert_checkalmostequal(res, y);
148 // Complex filter y(n) = 2 * y(n-1) - 3 * y(n-2) + x(n-2) - 2 * x(n-1) + x(n)
149 // Filter is (1 - 2z**-1 + z**-2) / (1 - 2z**-1 + 3z**-2)
154 res = filter(B, A, x);
155 assert_checkalmostequal(res, y);