]> Creatis software - CreaPhase.git/blob - octave_packages/control-2.3.52/@lti/c2d.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / control-2.3.52 / @lti / c2d.m
1 ## Copyright (C) 2009, 2011   Lukas F. Reichlin
2 ##
3 ## This file is part of LTI Syncope.
4 ##
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.
9 ##
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.
14 ##
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/>.
17
18 ## -*- texinfo -*-
19 ## @deftypefn {Function File} {@var{sys} =} c2d (@var{sys}, @var{tsam})
20 ## @deftypefnx {Function File} {@var{sys} =} c2d (@var{sys}, @var{tsam}, @var{method})
21 ## @deftypefnx {Function File} {@var{sys} =} c2d (@var{sys}, @var{tsam}, @var{"prewarp"}, @var{w0})
22 ## Convert the continuous lti model into its discrete-time equivalent.
23 ##
24 ## @strong{Inputs}
25 ## @table @var
26 ## @item sys
27 ## Continuous-time LTI model.
28 ## @item tsam
29 ## Sampling time in seconds.
30 ## @item method
31 ## Optional conversion method.  If not specified, default method @var{"zoh"}
32 ## is taken.
33 ## @table @var
34 ## @item "zoh"
35 ## Zero-order hold or matrix exponential.
36 ## @item "tustin", "bilin"
37 ## Bilinear transformation or Tustin approximation.
38 ## @item "prewarp"
39 ## Bilinear transformation with pre-warping at frequency @var{w0}.
40 ## @end table
41 ## @end table
42 ##
43 ## @strong{Outputs}
44 ## @table @var
45 ## @item sys
46 ## Discrete-time LTI model.
47 ## @end table
48 ## @end deftypefn
49
50 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
51 ## Created: October 2009
52 ## Version: 0.3
53
54 function sys = c2d (sys, tsam, method = "std", w0 = 0)
55
56   if (nargin < 2 || nargin > 4)
57     print_usage ();
58   endif
59
60   if (! isa (sys, "lti"))
61     error ("c2d: first argument is not an lti model");
62   endif
63
64   if (isdt (sys))
65     error ("c2d: system is already discrete-time");
66   endif
67
68   if (! issample (tsam))
69     error ("c2d: second argument is not a valid sample time");
70   endif
71
72   if (! ischar (method))
73     error ("c2d: third argument is not a string");
74   endif
75
76   if (! issample (w0, 0))
77     error ("c2d: fourth argument is not a valid pre-warping frequency");
78   endif
79
80   sys = __c2d__ (sys, tsam, lower (method), w0);
81   sys.tsam = tsam;
82
83 endfunction
84
85
86 ## bilinear transformation
87 ## using oct-file directly
88 %!shared Mo, Me
89 %! A = [  1.0  0.5
90 %!        0.5  1.0 ].';
91 %!
92 %! B = [  0.0 -1.0
93 %!        1.0  0.0 ].';
94 %!
95 %! C = [ -1.0  0.0
96 %!        0.0  1.0 ].';
97 %!
98 %! D = [  1.0  0.0
99 %!        0.0 -1.0 ].';
100 %!
101 %! [Ao, Bo, Co, Do] = slab04md (A, B, C, D, 1.0, 1.0, false);
102 %!
103 %! Ae = [ -1.0000  -4.0000
104 %!        -4.0000  -1.0000 ];
105 %!
106 %! Be = [  2.8284   0.0000
107 %!         0.0000  -2.8284 ];
108 %!
109 %! Ce = [  0.0000   2.8284
110 %!        -2.8284   0.0000 ];
111 %!
112 %! De = [ -1.0000   0.0000
113 %!         0.0000  -3.0000 ];
114 %!
115 %! Mo = [Ao, Bo; Co, Do];
116 %! Me = [Ae, Be; Ce, De];
117 %!
118 %!assert (Mo, Me, 1e-4);
119
120
121 ## bilinear transformation
122 ## user function
123 %!shared Mo, Me
124 %! A = [  1.0  0.5
125 %!        0.5  1.0 ].';
126 %!
127 %! B = [  0.0 -1.0
128 %!        1.0  0.0 ].';
129 %!
130 %! C = [ -1.0  0.0
131 %!        0.0  1.0 ].';
132 %!
133 %! D = [  1.0  0.0
134 %!        0.0 -1.0 ].';
135 %!
136 %! [Ao, Bo, Co, Do] = ssdata (c2d (ss (A, B, C, D), 2, "tustin"));
137 %!
138 %! Ae = [ -1.0000  -4.0000
139 %!        -4.0000  -1.0000 ];
140 %!
141 %! Be = [  2.8284   0.0000
142 %!         0.0000  -2.8284 ];
143 %!
144 %! Ce = [  0.0000   2.8284
145 %!        -2.8284   0.0000 ];
146 %!
147 %! De = [ -1.0000   0.0000
148 %!         0.0000  -3.0000 ];
149 %!
150 %! Mo = [Ao, Bo; Co, Do];
151 %! Me = [Ae, Be; Ce, De];
152 %!
153 %!assert (Mo, Me, 1e-4);
154
155
156 ## bilinear transformation
157 ## both directions
158 %!shared Mo, Me
159 %! A = [  1.0  0.5
160 %!        0.5  1.0 ];
161 %!
162 %! B = [  0.0 -1.0
163 %!        1.0  0.0 ];
164 %!
165 %! C = [ -1.0  0.0
166 %!        0.0  1.0 ];
167 %!
168 %! D = [  1.0  0.0
169 %!        0.0 -1.0 ];
170 %!
171 %! [Ao, Bo, Co, Do] = ssdata (d2c (c2d (ss (A, B, C, D), 2, "tustin"), "tustin"));
172 %!
173 %! Mo = [Ao, Bo; Co, Do];
174 %! Me = [A, B; C, D];
175 %!
176 %!assert (Mo, Me, 1e-4);
177
178
179 ## zero-order hold
180 ## both directions
181 %!shared Mo, Me
182 %! A = [  1.0  0.5
183 %!        0.5  1.0 ];
184 %!
185 %! B = [  0.0 -1.0
186 %!        1.0  0.0 ];
187 %!
188 %! C = [ -1.0  0.0
189 %!        0.0  1.0 ];
190 %!
191 %! D = [  1.0  0.0
192 %!        0.0 -1.0 ];
193 %!
194 %! [Ao, Bo, Co, Do] = ssdata (d2c (c2d (ss (A, B, C, D), 2, "zoh"), "zoh"));
195 %!
196 %! Mo = [Ao, Bo; Co, Do];
197 %! Me = [A, B; C, D];
198 %!
199 %!assert (Mo, Me, 1e-4);
200
201
202 ## bilinear transformation with pre-warping
203 ## both directions
204 %!shared Mo, Me
205 %! A = [  1.0  0.5
206 %!        0.5  1.0 ];
207 %!
208 %! B = [  0.0 -1.0
209 %!        1.0  0.0 ];
210 %!
211 %! C = [ -1.0  0.0
212 %!        0.0  1.0 ];
213 %!
214 %! D = [  1.0  0.0
215 %!        0.0 -1.0 ];
216 %!
217 %! [Ao, Bo, Co, Do] = ssdata (d2c (c2d (ss (A, B, C, D), 2, "prewarp", 1000), "prewarp", 1000));
218 %!
219 %! Mo = [Ao, Bo; Co, Do];
220 %! Me = [A, B; C, D];
221 %!
222 %!assert (Mo, Me, 1e-4);