0
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
1 % SIMULATE simulates a discrete ssm with given inputs
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
3 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
4 % DESCRIPTION: SIMULATE simulates a discrete ssm with given inputs.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
5 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
6 % CALL: [mat_out pl_out] = simulate(sys, pl)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
7 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
8 % INPUTS:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
9 % - sys, (array of) ssm object
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
10 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
11 % OUTPUTS:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
12 % _ mat_out contains specified returned aos
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
13 % _ pl_out contains 'lastX', the last state position
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
14 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
15 % <a href="matlab:utils.helper.displayMethodInfo('ssm', 'simulate')">Parameters Description</a>
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
16 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
17 % VERSION: $Id: simulate.m,v 1.104 2011/07/11 10:44:38 adrien Exp $
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
18 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
20
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
21 % TO DO: options to be defined (NL case)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
22 % allow use of other LTPDA functions to generate white noise
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
23
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
24
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
25 function varargout = simulate(varargin)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
26
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
27 %% starting initial checks
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
28
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
29 % use the caller is method flag
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
30 callerIsMethod = utils.helper.callerIsMethod;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
31
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
32 % Check if this is a call for parameters
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
33 if utils.helper.isinfocall(varargin{:})
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
34 varargout{1} = getInfo(varargin{3});
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
35 return
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
36 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
37
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
38 utils.helper.msg(utils.const.msg.PROC3, ['running ', mfilename]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
39
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
40 % Collect input variable names
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
41 in_names = cell(size(varargin));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
42 for ii = 1:nargin,in_names{ii} = inputname(ii);end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
43
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
44 % Collect all SSMs and plists
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
45 [sys, ssm_invars, rest] = utils.helper.collect_objects(varargin(:), 'ssm', in_names);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
46 [pl, invars2, rest] = utils.helper.collect_objects(rest(:), 'plist');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
47 if ~isempty(rest)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
48 pl = combine(pl, plist(rest{:}));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
49 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
50 pl = combine(pl, getDefaultPlist());
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
51
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
52
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
53 %% begin function body
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
54
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
55 tini = pl.find('t0');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
56 if isempty(tini)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
57 tini = pl.find('tini'); % check for the old name
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
58 if isempty(tini)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
59 tini = time;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
60 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
61 warning('The use of the parameter ''tini'' is deprecated and will be removed in future releases. Use ''t0'' instead');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
62 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
63 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
64 if isa(tini,'double')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
65 tini = time(tini);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
66 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
67
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
68
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
69 %% retrieve system infos
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
70 if numel(sys)~=1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
71 error('simulate needs exactly one ssm as an input')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
72 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
73 if ~sys.isnumerical
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
74 error(['error because system ',sys.name,' is not numerical']);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
75 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
76 timestep = sys.timestep;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
77 if timestep==0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
78 error('timestep should not be 0 in simulate!!')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
79 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
80
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
81 if callerIsMethod
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
82 % we don't need the history of the input
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
83 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
84 inhist = sys.hist;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
85 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
86
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
87 if pl.isparam('noise variable names')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
88 error('The noise option used must be split between "covariance" and "cpsd". "noise variable names" does not exist anymore!')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
89 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
90
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
91 %% display time ?
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
92 displayTime = find(pl, 'displayTime');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
93
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
94 %% initial state
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
95 ssini = find(pl,'ssini');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
96 if isempty(ssini)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
97 initialize = find(pl, 'initialize');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
98 if initialize
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
99 ssini = sys.steadyState(pl);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
100 ssini = find(ssini, 'state');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
101 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
102 ssini = cell(sys.Nss,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
103 for i=1:sys.Nss
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
104 ssini{i} = zeros(sys.sssizes(i),1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
105 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
106 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
107 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
108 ssSizesIni = sys.statesizes;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
109 SSini = double(ssm.blockMatFusion(ssini,ssSizesIni,1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
110
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
111 %% collecting simulation i/o data
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
112
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
113 % values
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
114 aos_in = find(pl, 'aos');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
115 constants_in = find(pl, 'constants');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
116 cov_in = find(pl, 'covariance');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
117 cpsd_in = find(pl, 'CPSD');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
118 noise_in = blkdiag(cov_in, cpsd_in/(timestep*2));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
119 [U1,S1,V1] = svd(noise_in.'); %#ok<NASGU>
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
120 if (sum(S1<0)>0)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
121 error('Covariance matrix is not positive definite')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
122 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
123 noise_mat = U1*sqrt(S1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
124
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
125 %% modifying system's ordering
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
126 if find(pl, 'reorganize')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
127 sys = reorganize(sys, pl, 'set', 'for simulate', 'internal', 'internal');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
128 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
129
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
130 %% getting system's i/o sizes
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
131 inputSizes = sys.inputsizes;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
132 outputSizes = sys.outputsizes;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
133
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
134 Naos_in = inputSizes(1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
135 Nnoise = inputSizes(2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
136 Nconstants = inputSizes(3);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
137 NstatesOut = outputSizes(1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
138 NoutputsOut = outputSizes(2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
139
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
140 if numel(aos_in)~=Naos_in
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
141 error(['There are ' num2str(numel(aos_in)) ' input aos and ' num2str(Naos_in) ' corresponding inputs indexed.' ])
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
142 elseif numel(diag(noise_in))~=Nnoise
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
143 error(['There are ' num2str(numel(diag(noise_in))) ' input noise variances and ' num2str(Nnoise) ' corresponding inputs indexed.' ])
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
144 elseif numel(constants_in)~=Nconstants
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
145 error(['There are ' num2str(numel(constants_in)) ' input constants and ' num2str(Nconstants) ' corresponding inputs indexed.' ])
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
146 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
147
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
148 A = sys.amats{1,1};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
149 Coutputs = sys.cmats{2,1};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
150 Cstates = sys.cmats{1,1};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
151 Baos = sys.bmats{1,1};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
152 Daos = sys.dmats{2,1};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
153 Bnoise = sys.bmats{1,2} * noise_mat;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
154 Dnoise = sys.dmats{2,2} * noise_mat;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
155 Bcst = sys.bmats{1,3} * reshape(constants_in, Nconstants, 1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
156 Dcst = sys.dmats{2,3} * reshape(constants_in, Nconstants, 1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
157
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
158 %% getting correct number of samples
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
159 Nsamples = find(pl, 'Nsamples');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
160 f0 = 1/timestep;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
161 for i=1:Naos_in
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
162 Nsamples = min(Nsamples,length(aos_in(i).y));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
163 try
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
164 if ~(f0==aos_in(i).fs)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
165 str = ['WARNING : ssm frequency is ',num2str(f0),...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
166 ' but sampling frequency of ao named ',...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
167 aos_in(i).name, ' is ', num2str(aos_in(i).fs) ];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
168 utils.helper.msg(utils.const.msg.MNAME, str);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
169 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
170 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
171 % maybe tdata should be retrieved and verified to be equal, rather than this.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
172 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
173 if Nsamples == inf % case there is no input!
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
174 error('warning : no input option ''Nsamples'' providing simulation duration is available!!')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
175 Nsamples = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
176 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
177
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
178 %% termination condition
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
179 if strcmp(find(pl, 'termincond'),'');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
180 doTerminate = false;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
181 terminationCond = '';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
182 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
183 doTerminate = true;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
184 terminationCond = find(pl, 'termincond');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
185 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
186
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
187 %% ao vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
188 aos_vect = zeros(Naos_in, Nsamples);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
189 for j = 1:Naos_in
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
190 aos_vect(j,:) = aos_in(j).y(1:Nsamples).';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
191 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
192
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
193 %% simulation loop
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
194 [x, y, lastX] = ssm.doSimulate(...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
195 SSini, Nsamples, ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
196 A, Baos, Coutputs, Cstates, Daos, Bnoise, Dnoise, Bcst, Dcst,...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
197 aos_vect, doTerminate, terminationCond, displayTime, timestep, pl.find('force complete'));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
198
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
199 %% saving in aos
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
200 fs = 1/timestep;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
201 isysStr = sys.name;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
202
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
203 ao_out = ao.initObjectWithSize(1,NstatesOut+NoutputsOut);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
204 for i=1:NstatesOut
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
205 ao_out(i).setData(tsdata( x(i,:), fs ));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
206 ao_out(i).setName(sys.outputs(1).ports(i).name);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
207 ao_out(i).setXunits('s');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
208 ao_out(i).setYunits(sys.outputs(1).ports(i).units);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
209 ao_out(i).setDescription(...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
210 ['simulation for ' isysStr, ' : ', sys.outputs(1).ports(i).name,...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
211 ' ' sys.outputs(1).ports(i).description]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
212 ao_out(i).setT0(tini);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
213 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
214 for i=1:NoutputsOut
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
215 ao_out(NstatesOut+i).setData(tsdata( y(i,:), fs ));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
216 ao_out(NstatesOut+i).setName(sys.outputs(2).ports(i).name);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
217 ao_out(NstatesOut+i).setXunits('s');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
218 ao_out(NstatesOut+i).setYunits(sys.outputs(2).ports(i).units);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
219 ao_out(NstatesOut+i).setDescription(...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
220 ['simulation for, ' isysStr, ' : ', sys.outputs(2).ports(i).name, ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
221 ' ', sys.outputs(2).ports(i).description]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
222 ao_out(NstatesOut+i).setT0(tini);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
223 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
224
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
225 %% construct output plist
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
226 plist_out = plist('lastX', ssm.blockMatRecut(lastX,ssSizesIni,1) );
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
227
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
228 %% construct output matrix object
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
229 out = matrix(ao_out);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
230 if callerIsMethod
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
231 % do nothing
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
232 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
233 myinfo = getInfo('None');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
234 out.addHistory(myinfo, pl , ssm_invars(1), inhist );
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
235 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
236
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
237 %% Set output depending on nargout
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
238 if nargout == 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
239 varargout = {out};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
240 elseif nargout == 2;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
241 varargout = {out plist_out};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
242 elseif nargout == 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
243 iplot(ao_out);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
244 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
245 error('Wrong number of outputs')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
246 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
247 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
248
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
249 %--------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
250 % Get Info Object
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
251 %--------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
252 function ii = getInfo(varargin)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
253
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
254 if nargin == 1 && strcmpi(varargin{1}, 'None')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
255 sets = {};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
256 pl = [];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
257 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
258 sets = {'Default'};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
259 pl = getDefaultPlist;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
260 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
261 % Build info object
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
262 ii = minfo(mfilename, 'ssm', 'ltpda', utils.const.categories.op, '$Id: simulate.m,v 1.104 2011/07/11 10:44:38 adrien Exp $', sets, pl);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
263 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
264
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
265 %--------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
266 % Get Default Plist
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
267 %--------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
268 function pl = getDefaultPlist()
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
269 pl = ssm.getInfo('reorganize', 'for simulate').plists;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
270 pl.remove('set');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
271
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
272 p = param({'covariance', 'The covariance of this noise between input ports for the <i>time-discrete</i> noise model.'}, []);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
273 pl.append(p);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
274
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
275 p = param({'CPSD', 'The one sided cross-psd of the white noise between input ports.'}, []);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
276 pl.append(p);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
277
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
278 p = param({'aos', 'An array of input AOs.'}, ao.initObjectWithSize(1,0));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
279 pl.append(p);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
280
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
281 p = param({'constants', 'Array of DC values for the different corresponding inputs.'}, paramValue.DOUBLE_VALUE(zeros(0,1)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
282 pl.append(p);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
283
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
284 p = param({'Nsamples', 'The maximum number of samples to simulate (AO length(s) overide this).'}, paramValue.DOUBLE_VALUE(inf));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
285 pl.append(p);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
286
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
287 p = param({'ssini', 'A cell-array of vectors that give the initial position for simulation.'}, {});
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
288 pl.append(p);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
289
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
290 p = param({'initialize', 'When set to 1, a random state value is computed for the initial point.'}, paramValue.FALSE_TRUE);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
291 pl.append(p);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
292
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
293 p = param({'tini', 'Same as t0; kept for backwards compatibility.'}, paramValue.EMPTY_DOUBLE );
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
294 pl.append(p);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
295
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
296 p = param({'t0', 'The initial simulation time (seconds).'}, paramValue.EMPTY_DOUBLE );
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
297 pl.append(p);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
298
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
299 p = param({'displayTime', 'Switch off/on the display'}, paramValue.TRUE_FALSE);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
300 pl.append(p);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
301
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
302 p = param({'termincond', 'A string to evaluate a termination condition on the states in x (''lastX'') or outputs in y (''lastY'')'}, paramValue.EMPTY_STRING);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
303 pl.append(p);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
304
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
305 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);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
306 pl.append(p);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
307
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
308 p = param({'force complete', 'Force the use of the complete simulation code.'}, paramValue.FALSE_TRUE);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
309 pl.append(p);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
310
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
311 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
312
|