3381
|
1 ## Copyright (C) 1996,1998 Auburn University. All Rights Reserved |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by the |
|
7 ## Free Software Foundation; either version 2, or (at your option) any |
|
8 ## later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 ## for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. |
3346
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File } { @var{sys} = } tf2sys( @var{num}, @var{den} @{, @var{tsam}, @var{inname}, @var{outname} @}) |
|
21 ## build system data structure from transfer function format data |
|
22 ## |
|
23 ## @strong{Inputs} |
|
24 ## @table @var |
|
25 ## @item num, den |
|
26 ## coefficients of numerator/denominator polynomials |
|
27 ## @item tsam |
|
28 ## sampling interval. default: 0 (continuous time) |
|
29 ## @item inname, outname |
|
30 ## input/output signal names; may be a string or list with a single string |
|
31 ## entry. |
|
32 ## @end table |
|
33 ## |
|
34 ## @strong{Outputs} |
|
35 ## @var{sys} = system data structure |
|
36 ## |
|
37 ## @strong{Example} |
|
38 ## @example |
|
39 ## octave:1> sys=tf2sys([2 1],[1 2 1],0.1); |
|
40 ## octave:2> sysout(sys) |
|
41 ## Input(s) |
|
42 ## 1: u_1 |
|
43 ## Output(s): |
|
44 ## 1: y_1 (discrete) |
|
45 ## Sampling interval: 0.1 |
|
46 ## transfer function form: |
|
47 ## 2*z^1 + 1 |
|
48 ## ----------------- |
|
49 ## 1*z^2 + 2*z^1 + 1 |
|
50 ## @end example |
|
51 ## @end deftypefn |
|
52 |
3213
|
53 function outsys = tf2sys(num,den,tsam,inname,outname) |
3381
|
54 ## Written by R. Bruce Tenison July 29, 1994 |
|
55 ## Name changed to TF2SYS July 1995 |
|
56 ## updated for new system data structure format July 1996 |
3213
|
57 |
|
58 save_val = implicit_str_to_num_ok; |
|
59 implicit_str_to_num_ok = 1; |
|
60 |
3381
|
61 ## Test for the correct number of input arguments |
3213
|
62 if ((nargin < 2) || (nargin > 5)) |
|
63 usage('outsys=tf2sys(num,den[,tsam,inname,outname])'); |
|
64 return |
|
65 endif |
|
66 |
3381
|
67 ## check input format |
3213
|
68 if( ! ( (is_vector(num) || is_scalar(num)) && ... |
|
69 (is_vector(den) || is_scalar(den))) ) |
|
70 error(['num (',num2str(rows(num)),'x',num2str(columns(num)), ... |
|
71 ') and den (',num2str(rows(den)),'x',num2str(columns(den)), ... |
|
72 ') must be vectors']) |
|
73 endif |
|
74 |
3381
|
75 ## strip leading zero coefficients |
3213
|
76 num = tf2sysl(num); |
|
77 den = tf2sysl(den); |
|
78 |
|
79 if (length(num) > length(den)) |
3228
|
80 error("# of poles (%d) < # of zeros (%d)",length(den)-1, length(num)-1); |
3213
|
81 endif |
|
82 |
3381
|
83 ## check sampling interval (if any) |
3228
|
84 if(nargin <= 2) tsam = 0; # default |
|
85 elseif (isempty(tsam)) tsam = 0; endif |
3213
|
86 if ( (! (is_scalar(tsam) && (imag(tsam) == 0) )) || (tsam < 0) ) |
|
87 error('tsam must be a positive real scalar') |
|
88 endif |
|
89 |
|
90 outsys.num = num; |
|
91 outsys.den = den; |
|
92 |
3381
|
93 ## Set the system vector: active = 0(tf), updated = [1 0 0]; |
3238
|
94 outsys.sys = [0, 1, 0, 0]; |
3213
|
95 |
3381
|
96 ## Set defaults |
3213
|
97 outsys.tsam = tsam; |
|
98 outsys.n = length(den)-1; |
|
99 outsys.nz = 0; |
|
100 outsys.yd = 0; # assume discrete-time |
3381
|
101 ## check discrete time |
3213
|
102 if(tsam > 0) |
|
103 [outsys.n,outsys.nz] = swap(outsys.n, outsys.nz); |
|
104 outsys.yd = 1; |
|
105 endif |
|
106 |
3228
|
107 outsys.inname = sysdefioname(1,"u"); |
3213
|
108 outsys.outname = sysdefioname(1,"y"); |
3228
|
109 outsys.stname = sysdefstname(outsys.n,outsys.nz); |
3213
|
110 |
3381
|
111 ## Set name of input |
3213
|
112 if (nargin > 3) |
3381
|
113 ## make sure its a list of a single string |
3228
|
114 if(!isempty(inname)) |
|
115 if(!is_list(inname)) inname = list(inname); endif |
|
116 if( !is_signal_list(inname) ) |
|
117 error("inname must be a string or list of strings"); |
|
118 endif |
|
119 if(length(inname) > 1) |
|
120 warning("tf2sys: %d input names provided; first used",length(inname)); |
|
121 inname = inname(1); |
|
122 endif |
|
123 outsys = syssetsignals(outsys,"in",inname); |
3213
|
124 endif |
|
125 endif |
|
126 |
3381
|
127 ## Set name of output |
3213
|
128 if (nargin > 4) |
3228
|
129 if(!isempty(outname)) |
|
130 if(!is_list(outname)) outname = list(outname); endif |
|
131 if(!is_signal_list(outname)) |
|
132 error("outname must be a string or a list of strings"); |
|
133 endif |
|
134 if(length(outname) > 1) |
|
135 warning("tf2sys: %d output names provided; first used",length(outname)); |
|
136 outname = outname(1); |
|
137 endif |
|
138 outsys = syssetsignals(outsys,"out",outname); |
3213
|
139 endif |
|
140 endif |
|
141 |
|
142 implicit_str_to_num_ok = save_val; |
|
143 endfunction |