3228
|
1 # Copyright (C) 1996,1998 A. Scottedward Hodel |
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 |
|
17 # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
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 |
3228
|
32 # $Revision: 2.0.0.0 $ |
3213
|
33 |
|
34 save_val = implicit_str_to_num_ok; |
|
35 implicit_str_to_num_ok = 1; |
|
36 |
|
37 # Test for the correct number of input arguments |
|
38 if ((nargin < 2) || (nargin > 5)) |
|
39 usage('outsys=tf2sys(num,den[,tsam,inname,outname])'); |
|
40 return |
|
41 endif |
|
42 |
|
43 # check input format |
|
44 if( ! ( (is_vector(num) || is_scalar(num)) && ... |
|
45 (is_vector(den) || is_scalar(den))) ) |
|
46 error(['num (',num2str(rows(num)),'x',num2str(columns(num)), ... |
|
47 ') and den (',num2str(rows(den)),'x',num2str(columns(den)), ... |
|
48 ') must be vectors']) |
|
49 endif |
|
50 |
|
51 # strip leading zero coefficients |
|
52 num = tf2sysl(num); |
|
53 den = tf2sysl(den); |
|
54 |
|
55 if (length(num) > length(den)) |
3228
|
56 error("# of poles (%d) < # of zeros (%d)",length(den)-1, length(num)-1); |
3213
|
57 endif |
|
58 |
|
59 # check sampling interval (if any) |
3228
|
60 if(nargin <= 2) tsam = 0; # default |
|
61 elseif (isempty(tsam)) tsam = 0; endif |
3213
|
62 if ( (! (is_scalar(tsam) && (imag(tsam) == 0) )) || (tsam < 0) ) |
|
63 error('tsam must be a positive real scalar') |
|
64 endif |
|
65 |
|
66 outsys.num = num; |
|
67 outsys.den = den; |
|
68 |
|
69 # Set the system vector: active = 0(tf), updated = [1 0 0]; |
|
70 outsys.sys = [0 1 0 0]; |
|
71 |
|
72 # Set defaults |
|
73 outsys.tsam = tsam; |
|
74 outsys.n = length(den)-1; |
|
75 outsys.nz = 0; |
|
76 outsys.yd = 0; # assume discrete-time |
|
77 # check discrete time |
|
78 if(tsam > 0) |
|
79 [outsys.n,outsys.nz] = swap(outsys.n, outsys.nz); |
|
80 outsys.yd = 1; |
|
81 endif |
|
82 |
3228
|
83 outsys.inname = sysdefioname(1,"u"); |
3213
|
84 outsys.outname = sysdefioname(1,"y"); |
3228
|
85 outsys.stname = sysdefstname(outsys.n,outsys.nz); |
3213
|
86 |
|
87 # Set name of input |
|
88 if (nargin > 3) |
3228
|
89 # make sure its a list of a single string |
|
90 if(!isempty(inname)) |
|
91 if(!is_list(inname)) inname = list(inname); endif |
|
92 if( !is_signal_list(inname) ) |
|
93 error("inname must be a string or list of strings"); |
|
94 endif |
|
95 if(length(inname) > 1) |
|
96 warning("tf2sys: %d input names provided; first used",length(inname)); |
|
97 inname = inname(1); |
|
98 endif |
|
99 outsys = syssetsignals(outsys,"in",inname); |
3213
|
100 endif |
|
101 endif |
|
102 |
|
103 # Set name of output |
|
104 if (nargin > 4) |
3228
|
105 if(!isempty(outname)) |
|
106 if(!is_list(outname)) outname = list(outname); endif |
|
107 if(!is_signal_list(outname)) |
|
108 error("outname must be a string or a list of strings"); |
|
109 endif |
|
110 if(length(outname) > 1) |
|
111 warning("tf2sys: %d output names provided; first used",length(outname)); |
|
112 outname = outname(1); |
|
113 endif |
|
114 outsys = syssetsignals(outsys,"out",outname); |
3213
|
115 endif |
|
116 endif |
|
117 |
|
118 implicit_str_to_num_ok = save_val; |
|
119 endfunction |