1 ## Copyright (C) 2012 Lukas F. Reichlin
3 ## This file is part of LTI Syncope.
5 ## LTI Syncope is free software: you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation, either version 3 of the License, or
8 ## (at your option) any later version.
10 ## LTI Syncope is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with LTI Syncope. If not, see <http://www.gnu.org/licenses/>.
19 ## @deftypefn {Function File} {@var{sys} =} filt (@var{num}, @var{den}, @dots{})
20 ## @deftypefnx {Function File} {@var{sys} =} filt (@var{num}, @var{den}, @var{tsam}, @dots{})
21 ## Create discrete-time transfer function model from data in DSP format.
26 ## Numerator or cell of numerators. Each numerator must be a row vector
27 ## containing the coefficients of the polynomial in ascending powers of z^-1.
28 ## num@{i,j@} contains the numerator polynomial from input j to output i.
29 ## In the SISO case, a single vector is accepted as well.
31 ## Denominator or cell of denominators. Each denominator must be a row vector
32 ## containing the coefficients of the polynomial in ascending powers of z^-1.
33 ## den@{i,j@} contains the denominator polynomial from input j to output i.
34 ## In the SISO case, a single vector is accepted as well.
36 ## Sampling time in seconds. If @var{tsam} is not specified,
37 ## default value -1 (unspecified) is taken.
39 ## Optional pairs of properties and values.
40 ## Type @command{set (filt)} for more information.
46 ## Discrete-time transfer function model.
53 ## H(z^-1) = -------------------
54 ## 1 + 4 z^-1 + 2 z^-2
56 ## octave:1> H = filt ([0, 3], [1, 4, 2])
58 ## Transfer function 'H' from input 'u1' to output ...
61 ## y1: -------------------
62 ## 1 + 4 z^-1 + 2 z^-2
64 ## Sampling time: unspecified
65 ## Discrete-time model.
72 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
73 ## Created: April 2012
76 function sys = filt (num = {}, den = {}, tsam = -1, varargin)
84 case 1 # filt (sys), filt (matrix)
85 if (isa (num, "lti") || is_real_matrix (num))
87 ## sys.inv = true; # would be a problem for continuous-time LTI models
93 otherwise # filt (num, den, ...)
101 ## convert from z^-1 to z
102 ## expand each channel by z^x, where x is the largest exponent of z^-1 (z^-x)
104 ## remove trailing zeros
105 ## such that polynomials are as short as possible
106 num = cellfun (@__remove_trailing_zeros__, num, "uniformoutput", false);
107 den = cellfun (@__remove_trailing_zeros__, den, "uniformoutput", false);
109 ## make numerator and denominator polynomials equally long
110 ## by adding trailing zeros
111 lnum = cellfun (@length, num, "uniformoutput", false);
112 lden = cellfun (@length, den, "uniformoutput", false);
114 lmax = cellfun (@max, lnum, lden, "uniformoutput", false);
116 num = cellfun (@postpad, num, lmax, "uniformoutput", false);
117 den = cellfun (@postpad, den, lmax, "uniformoutput", false);
119 ## use standard tf constructor
120 ## sys is stored in standard z form, not z^-1
121 ## so we can mix it with regular transfer function models
122 ## property "inv", true displays sys in z^-1 form
123 sys = tf (num, den, tsam, "inv", true, varargin{:});
128 %!shared num, den, n1, d1, n2, d2, n2e, d2e
131 %! sys = filt (num, den);
132 %! [n1, d1] = filtdata (sys, "vector");
133 %! [n2, d2] = tfdata (sys, "vector");
136 %!assert (n1, num, 1e-4);
137 %!assert (d1, den, 1e-4);
138 %!assert (n2, n2e, 1e-4);
139 %!assert (d2, d2e, 1e-4);