3279
|
1 # Copyright (C) 1996,1998 Auburn University. All Rights Reserved |
3213
|
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 |
3284
|
17 # Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. |
3213
|
18 |
|
19 function outsys = tf2sys(num,den,tsam,inname,outname) |
|
20 # |
|
21 # sys = tf2sys(num,den{,tsam,inname,outname}) |
|
22 # build system data structure from transfer function format data |
|
23 # inputs: |
|
24 # num, den: coefficients of numerator/denominator polynomials |
|
25 # tsam: sampling interval. default: 0 (continuous time) |
|
26 # inname, outname: input/output signal names (string variables) |
|
27 # outputs: sys = system data structure |
|
28 |
|
29 # Written by R. Bruce Tenison July 29, 1994 |
|
30 # Name changed to TF2SYS July 1995 |
|
31 # updated for new system data structure format July 1996 |
|
32 |
|
33 save_val = implicit_str_to_num_ok; |
|
34 implicit_str_to_num_ok = 1; |
|
35 |
|
36 # Test for the correct number of input arguments |
|
37 if ((nargin < 2) || (nargin > 5)) |
|
38 usage('outsys=tf2sys(num,den[,tsam,inname,outname])'); |
|
39 return |
|
40 endif |
|
41 |
|
42 # check input format |
|
43 if( ! ( (is_vector(num) || is_scalar(num)) && ... |
|
44 (is_vector(den) || is_scalar(den))) ) |
|
45 error(['num (',num2str(rows(num)),'x',num2str(columns(num)), ... |
|
46 ') and den (',num2str(rows(den)),'x',num2str(columns(den)), ... |
|
47 ') must be vectors']) |
|
48 endif |
|
49 |
|
50 # strip leading zero coefficients |
|
51 num = tf2sysl(num); |
|
52 den = tf2sysl(den); |
|
53 |
|
54 if (length(num) > length(den)) |
3228
|
55 error("# of poles (%d) < # of zeros (%d)",length(den)-1, length(num)-1); |
3213
|
56 endif |
|
57 |
|
58 # check sampling interval (if any) |
3228
|
59 if(nargin <= 2) tsam = 0; # default |
|
60 elseif (isempty(tsam)) tsam = 0; endif |
3213
|
61 if ( (! (is_scalar(tsam) && (imag(tsam) == 0) )) || (tsam < 0) ) |
|
62 error('tsam must be a positive real scalar') |
|
63 endif |
|
64 |
|
65 outsys.num = num; |
|
66 outsys.den = den; |
|
67 |
|
68 # Set the system vector: active = 0(tf), updated = [1 0 0]; |
3238
|
69 outsys.sys = [0, 1, 0, 0]; |
3213
|
70 |
|
71 # Set defaults |
|
72 outsys.tsam = tsam; |
|
73 outsys.n = length(den)-1; |
|
74 outsys.nz = 0; |
|
75 outsys.yd = 0; # assume discrete-time |
|
76 # check discrete time |
|
77 if(tsam > 0) |
|
78 [outsys.n,outsys.nz] = swap(outsys.n, outsys.nz); |
|
79 outsys.yd = 1; |
|
80 endif |
|
81 |
3228
|
82 outsys.inname = sysdefioname(1,"u"); |
3213
|
83 outsys.outname = sysdefioname(1,"y"); |
3228
|
84 outsys.stname = sysdefstname(outsys.n,outsys.nz); |
3213
|
85 |
|
86 # Set name of input |
|
87 if (nargin > 3) |
3228
|
88 # make sure its a list of a single string |
|
89 if(!isempty(inname)) |
|
90 if(!is_list(inname)) inname = list(inname); endif |
|
91 if( !is_signal_list(inname) ) |
|
92 error("inname must be a string or list of strings"); |
|
93 endif |
|
94 if(length(inname) > 1) |
|
95 warning("tf2sys: %d input names provided; first used",length(inname)); |
|
96 inname = inname(1); |
|
97 endif |
|
98 outsys = syssetsignals(outsys,"in",inname); |
3213
|
99 endif |
|
100 endif |
|
101 |
|
102 # Set name of output |
|
103 if (nargin > 4) |
3228
|
104 if(!isempty(outname)) |
|
105 if(!is_list(outname)) outname = list(outname); endif |
|
106 if(!is_signal_list(outname)) |
|
107 error("outname must be a string or a list of strings"); |
|
108 endif |
|
109 if(length(outname) > 1) |
|
110 warning("tf2sys: %d output names provided; first used",length(outname)); |
|
111 outname = outname(1); |
|
112 endif |
|
113 outsys = syssetsignals(outsys,"out",outname); |
3213
|
114 endif |
|
115 endif |
|
116 |
|
117 implicit_str_to_num_ok = save_val; |
|
118 endfunction |