comparison m-toolbox/classes/@ssm/respcst.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % RESPCST gives the timewise impulse response of a statespace model.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: RESPCST gives the timewise impulse response of a statespace
5 % model.
6 %
7 % CALL: [aos ] = respcst(sys, plist_inputs)
8 %
9 % INPUTS:
10 % - sys, (array of) ssm object
11 %
12 % plist with parameters 'inputs', 'states' and 'outputs' to indicate which
13 % inputs, states and outputs variables are taken in account. This requires
14 % proper variable naming. If a variable called appears more that once it
15 % will be used once only.
16 %
17 % OUTPUTS:
18 % - aos the timewise response (one for each input)
19 %
20 % <a href="matlab:utils.helper.displayMethodInfo('ssm', 'respcst')">Parameters Description</a>
21 %
22 % VERSION: $Id: respcst.m,v 1.8 2011/04/08 08:56:23 hewitson Exp $
23 %
24 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
25
26 function varargout = respcst(varargin)
27 %% starting initial checks
28 % Check if this is a call for parameters
29 if utils.helper.isinfocall(varargin{:})
30 varargout{1} = getInfo(varargin{3});
31 return
32 end
33
34 utils.helper.msg(utils.const.msg.MNAME, ['running ', mfilename]);
35
36 % checking number of inputs work data
37 in_names = cell(size(varargin));
38 for ii = 1:nargin,in_names{ii} = inputname(ii);end
39
40 [sys, ssm_invars, rest] = utils.helper.collect_objects(varargin(:), 'ssm', in_names);
41 [pl, ssm_invars2, rest] = utils.helper.collect_objects(rest(:), 'plist', in_names);
42 if ~isempty(rest)
43 pl = combine(pl, plist(rest{:}));
44 end
45 pl = combine(pl, getDefaultPlist());
46
47
48 %% begin function body
49 if numel(sys)>1
50 error('ssm/respcst only works with a single statespace model.')
51 end
52
53 if ~sys.isnumerical
54 error('The input system should be numeric.')
55 end
56
57 %% modifying input ssm
58 if sys.timestep == 0
59 sys = copy(sys, true);
60 sys.modifyTimeStep(0.1);
61 warning('Warning : time-continuous ssm was discretized with a time-step of 0.1s per default') %#ok<WNTAG>
62 end
63
64 %% getting duration of the response
65 tmax = find(pl, 'tmax');
66 if sys.isStable && (tmax == -1)
67 pl_s = settlingTime(sys, pl);
68 timeMax = min(find(pl_s, 'SETTLING_TIME'), 1e5);
69 timeMax = 2^nextPow2( 12* timeMax);
70 else
71 timeMax = 2^nextPow2( tmax./sys.timestep);
72 end
73
74 %% deriving and initializing matrices
75 if find(pl, 'reorganize')
76 sys = copy(sys, true);
77 sys = reorganize(sys, pl, 'set', 'for resp', 'internal', 'internal');
78 end
79
80 A = sys.amats{1,1};
81 B = sys.bmats{1,1};
82 C = sys.cmats{2,1};
83 Cstates = sys.cmats{1,1};
84 D = sys.dmats{2,1};
85 Ts = sys.timestep;
86 Dstates = zeros(size(Cstates,1), size(D,2));
87 Dtot = [Dstates; D];
88
89 Ninputs = size(B,2);
90 Noutputs = size(C,1);
91 Nss = size(A,1);
92 NssOut = size(Cstates,1);
93
94 %% collecting data before saving in loop
95 aos = ao.initObjectWithSize(Noutputs+NssOut, Ninputs);
96 myinfo = getInfo('None');
97 inhist = sys.hist;
98
99 inputType = find(pl, 'response');
100
101
102 for ii=1:Ninputs
103 sys_ss = ss(A, B(:,ii), [Cstates; C], Dtot(:,ii), Ts);
104
105 %% depending on user option, compute response for one input only
106 if strcmpi( inputType, 'IMPULSE')
107 Y = impulse(sys_ss, 1:Ts:(timeMax*Ts));
108 elseif strcmpi( inputType, 'STEP')
109 Y = step(sys_ss, 1:Ts:(timeMax*Ts));
110 else
111 error(['Option ''response'' does not accept the value' inputType]);
112 end
113
114 %% storing responsei in aos for one input only
115
116 for oo=1:NssOut;
117 aos(oo,ii).setData(tsdata( Y(:,oo), 1/Ts));
118 aos(oo,ii).setName([sys.inputs(1).ports(ii).name '->' sys.outputs(1).ports(oo).name]);
119 aos(oo,ii).setXunits('s');
120 aos(oo,ii).setYunits(sys.outputs(1).ports(oo).units);
121 aos(oo,ii).setDescription([inputType ' response from input ' , sys.inputs(1).ports(ii).name , 'to output ', sys.outputs(1).ports(oo).name]);
122 aos(oo,ii).setT0(sys.timestep);
123 aos(oo,ii).addHistory(myinfo, pl , {''}, inhist );
124 end
125 for oo=1:Noutputs;
126 aos(NssOut+oo,ii).setData(tsdata( Y(:,NssOut+oo), 1/Ts));
127 aos(NssOut+oo,ii).setName([sys.inputs(1).ports(ii).name '->' sys.outputs(2).ports(oo).name]);
128 aos(NssOut+oo,ii).setXunits('s');
129 aos(NssOut+oo,ii).setYunits(sys.outputs(2).ports(oo).units);
130 aos(NssOut+oo,ii).setDescription([inputType ' response from input ' , sys.inputs(1).ports(ii).name , 'to output ', sys.outputs(2).ports(oo).name]);
131 aos(NssOut+oo,ii).setT0(sys.timestep);
132 aos(NssOut+oo,ii).addHistory(myinfo, pl , {''}, inhist );
133 end
134 end
135
136 %% Set output depending on nargout
137 if nargout == numel(aos)
138 for jj=1:nargout
139 varargout{jj} = aos(jj);
140 end
141 elseif nargout == 1;
142 varargout{1} = aos;
143 elseif nargout == 0;
144 iplot(aos);
145 else
146 error('Wrong number of outputs')
147 end
148 end
149
150 %--------------------------------------------------------------------------
151 % Get Info Object
152 %--------------------------------------------------------------------------
153 function ii = getInfo(varargin)
154
155 if nargin == 1 && strcmpi(varargin{1}, 'None')
156 sets = {};
157 pl = [];
158 else
159 sets = {'Default'};
160 pl = getDefaultPlist;
161 end
162 % Build info object
163 ii = minfo(mfilename, 'ssm', 'ltpda', utils.const.categories.sigproc, '$Id: respcst.m,v 1.8 2011/04/08 08:56:23 hewitson Exp $', sets, pl);
164 end
165
166 %--------------------------------------------------------------------------
167 % Get Default Plist
168 %--------------------------------------------------------------------------
169
170 function pl = getDefaultPlist()
171 pl = ssm.getInfo('reorganize', 'for resp').plists;
172 pl.remove('set');
173
174 p = param({'response', 'Specify the type of response wanted'},{1, {'impulse', 'step'}, paramValue.SINGLE} );
175 pl.append(p);
176
177 p = param({'tmax', 'Specify the duration of response wanted [s] (automatically set if not specified by user, and system is stable)'},-1 );
178 pl.append(p);
179
180 p = param({'reorganize', 'When set to 0, this means the ssm does not need be modified to match the requested i/o. Faster but dangerous!'}, paramValue.TRUE_FALSE);
181 pl.append(p);
182
183
184 end
185