File: tf2ss.m

package info (click to toggle)
octave-control 1.0.11-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,628 kB
  • ctags: 160
  • sloc: makefile: 64; sh: 4
file content (134 lines) | stat: -rw-r--r-- 3,529 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
## Copyright (C) 1996, 1998, 2000, 2002, 2004, 2005, 2007
##               Auburn University.  All rights reserved.
##
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or (at
## your option) any later version.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {[@var{a}, @var{b}, @var{c}, @var{d}] =} tf2ss (@var{num}, @var{den})
## Conversion from transfer function to state-space.
## The state space system:
## @iftex
## @tex
## $$ \dot x = Ax + Bu $$
## $$ y = Cx + Du $$
## @end tex
## @end iftex
## @ifinfo
## @example
##       .
##       x = Ax + Bu
##       y = Cx + Du
## @end example
## @end ifinfo
## is obtained from a transfer function:
## @iftex
## @tex
## $$ G(s) = { { \rm num }(s) \over { \rm den }(s) } $$
## @end tex
## @end iftex
## @ifinfo
## @example
##                 num(s)
##           G(s)=-------
##                 den(s)
## @end example
## @end ifinfo
##
## The vector @var{den} must contain only one row, whereas the vector 
## @var{num} may contain as many rows as there are outputs @var{y} of 
## the system. The state space system matrices obtained from this function 
## will be in controllable canonical form as described in @cite{Modern Control 
## Theory}, (Brogan, 1991).
## @end deftypefn

## Author: R. Bruce Tenison <btenison@eng.auburn.edu>
## Created: June 22, 1994
## mod A S Hodel July, Aug  1995

function [a, b, c, d] = tf2ss (num, den)

  if (nargin != 2)
    print_usage ();
  elseif (isempty (num))
    error ("tf2ss: empty numerator");
  elseif (isempty (den))
    error ("tf2ss: empy denominator");
  elseif (! isvector (num))
    error ("num(%dx%d) must be a vector", rows (num), columns (num));
  elseif (! isvector (den))
    error ("den(%dx%d) must be a vector", rows (den), columns (den));
  endif

  ## strip leading zeros from num, den
  nz = find (num != 0);
  if (isempty (nz))
    num = 0;
  else
    num = num(nz(1):length(num));
  endif
  nz = find (den != 0);
  if (isempty (nz))
    error ("denominator is 0.");
  else
    den = den(nz(1):length(den));
  endif

  ## force num, den to be row vectors
  num = vec (num)';
  den = vec (den)';
  nn = length (num);
  nd = length (den);
  if (nn > nd)
    error ("deg(num)=%d > deg(den)= %d", nn, nd);
  endif

   ## Check sizes
   if (nd == 1)
     a = b = c = [];
     d = num(:,1) / den(1);
   else
    ## Pad num so that length(num) = length(den)
    if (nd-nn > 0)
      num = [zeros(1,nd-nn), num];
    endif

    ## Normalize the numerator and denominator vector w.r.t. the leading
    ## coefficient
    d1 = den(1);
    num = num / d1;
    den = den(2:nd)/d1;
    sw = nd-1:-1:1;

    ## Form the A matrix
    if (nd > 2)
      a = [zeros(nd-2,1), eye(nd-2,nd-2); -den(sw)];
    else
      a = -den(sw);
    endif

    ## Form the B matrix
    b = zeros (nd-1, 1);
    b(nd-1,1) = 1;

    ## Form the C matrix
    c = num(:,2:nd)-num(:,1)*den;
    c = c(:,sw);

    ## Form the D matrix
    d = num(:,1);
  endif

endfunction