comparison m-toolbox/classes/@ssm/doSimulate.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 % DOSIMULATE simulates a discrete ssm with given inputs
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: DOSIMULATE simulates a discrete ssm with given inputs.
5 %
6 % CALL: [x, y, lastX] = doSimulate(SSini, Nsamples, A, Baos, Coutputs, Cstates, Daos, Bnoise, Dnoise, Bcst, Dcst, aos_vect, doTerminate, terminationCond, displayTime, timestep)
7
8 %
9 % INPUTS:
10 %
11 % OUTPUTS:
12 %
13 %
14 % VERSION: $Id: doSimulate.m,v 1.8 2010/09/08 11:39:20 adrien Exp $
15 %
16 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17
18 % TO DO: Check input aos for the timestep, tsdata, and ssm.timestep
19 % options to be defined (NL case)
20 % add check if one input mach no ssm input variable
21 % allow use of other LTPDA functions to generate white noise
22
23
24 function [x, y, lastX] = doSimulate(SSini, Nsamples, A, Baos, Coutputs, Cstates, Daos, Bnoise, Dnoise, Bcst, Dcst, aos_vect, doTerminate, terminationCond, displayTime, timestep, forceComplete)
25
26 % We do a simple simulate if all these are satisfied:
27 % 1) Bnoise is empty or all zeros
28 % 2) Cstates is empty
29 % 3) Dnoise is empty or all zeros
30 % 4) Bcst is empty or all zeros
31 % 5) Dcst is empty or all zeros
32 % 6) doTerminate is false
33
34 if (isempty(Bnoise) || all(all(Bnoise==0))) && ...
35 isempty(Cstates) && ...
36 (isempty(Dnoise) || all(all(Dnoise==0))) && ...
37 (isempty(Bcst) || all(all(Bcst==0))) && ...
38 (isempty(Dcst) || all(all(Dcst==0))) && ...
39 ~doTerminate && ...
40 ~forceComplete
41 % do a fast simulation
42 Nstates = numel(SSini);
43
44 if Nstates >= 100
45 % except if Matlab is faster
46 [x,y,lastX] = doSimulateSimple(SSini, Nsamples, A, Baos, Coutputs, Daos, aos_vect);
47 else
48 try
49 % call to the mex file
50 x = [];
51 [y,lastX] = ltpda_ssmsim(SSini, A.', Coutputs.', Cstates.', Baos.', Daos.', aos_vect);
52 catch
53 % backup if the mex-file is broken
54 warning('Failed to run mex file ltpda_ssmsim');
55 [x,y,lastX] = doSimulateSimple(SSini, Nsamples, A, Baos, Coutputs, Daos, aos_vect);
56 end
57 end
58 else
59 % the standard old script, more complete (DC and noise inputs)
60 [x,y,lastX] = doSimulateComplete(SSini, Nsamples, A, Baos, Coutputs, Cstates, Daos, Bnoise, Dnoise, Bcst, Dcst, aos_vect, doTerminate, terminationCond, displayTime, timestep);
61 end
62
63 end
64
65
66 function [x,y,lastX] = doSimulateSimple(lastX, Nsamples, A, Baos, Coutputs, Daos, aos_vect)
67
68 disp('Running simulate simple...');
69 % initializing fields
70 x = [];
71 y = zeros(size(Coutputs,1), Nsamples);
72 lastX = A*lastX + Baos*aos_vect(:,1);
73 % time for displaying remaining time
74
75 % simulation loop
76 for k = 1:Nsamples
77 % computing and storing outputs
78 y(:,k) = Coutputs*lastX + Daos*aos_vect(:,k);
79 % computing and storing states
80 lastX = A*lastX + Baos*aos_vect(:,k);
81 end
82
83 end
84
85 function [x,y,lastX] = doSimulateComplete(lastX, Nsamples, A, Baos, Coutputs, Cstates, Daos, Bnoise, Dnoise, Bcst, Dcst, aos_vect, doTerminate, terminationCond, displayTime, timestep)
86
87 disp('Running simulate complete...');
88
89 %% converting to sparse matrices
90 if numel(A)>0; if(sum(sum(A==0))/numel(A))>0.5; A = sparse(A); end, end
91 if numel(Baos)>0; if(sum(sum(Baos==0))/numel(Baos))>0.5; Baos = sparse(Baos); end, end
92 if numel(Coutputs)>0; if(sum(sum(Coutputs==0))/numel(Coutputs))>0.5; Coutputs = sparse(Coutputs); end, end
93 if numel(Cstates)>0; if(sum(sum(Cstates==0))/numel(Cstates))>0.5; Cstates = sparse(Cstates); end, end
94 if numel(Daos)>0; if(sum(sum(Daos==0))/numel(Daos))>0.5; Daos = sparse(Daos); end, end
95 if numel(Bnoise)>0; if(sum(sum(Bnoise==0))/numel(Bnoise))>0.5; Bnoise = sparse(Bnoise); end, end
96 if numel(Dnoise)>0; if(sum(sum(Dnoise==0))/numel(Dnoise))>0.5; Dnoise = sparse(Dnoise); end, end
97 if numel(Bcst)>0; if(sum(sum(Bcst==0))/numel(Bcst))>0.5; Bcst = sparse(Bcst); end, end
98 if numel(Dcst)>0; if(sum(sum(Dcst==0))/numel(Dcst))>0.5; Dcst = sparse(Dcst); end, end
99
100 %% initializing fields
101 x = zeros(size(Cstates,1), Nsamples);
102 y = zeros(size(Coutputs,1), Nsamples);
103 Nnoise = size(Bnoise,2);
104 BLOCK_SIZE = min( [ floor(1e6/(size(Baos,2) + size(Baos,1) + size(Bnoise,2) + 1)) , Nsamples]);
105 noise_array = randn(Nnoise, BLOCK_SIZE);
106 lastX = A*lastX + Bcst + Bnoise*noise_array(:,1) + Baos*aos_vect(:,1);
107 % time for displaying remaining time
108 time1=time;
109
110 %% simulation loop
111 for k = 1:Nsamples
112 %% writing white noise and displaying time
113 if rem(k,BLOCK_SIZE)==0
114 noise_array = randn(Nnoise, BLOCK_SIZE);
115 if displayTime
116 display( [' simulation time : ',num2str(k*timestep) ]);
117 time2 = time;
118 tloop = floor(time2.utc_epoch_milli-time1.utc_epoch_milli)/1000;
119 display( ['remaining computing time : ',num2str(tloop*(Nsamples-k+1)/k), 's' ]);
120 end
121 end
122
123 %% evaluating differences
124 knoise = rem(k,BLOCK_SIZE-1)+1;
125
126 %% computing and storing outputs
127 y(:,k) = Coutputs*lastX + Dcst + Dnoise*noise_array(:,knoise) + Daos*aos_vect(:,k);
128
129 %% computing and storing states
130 x(:,k) = Cstates*lastX;
131 lastX = A*lastX + Bcst + Bnoise*noise_array(:,knoise) + Baos*aos_vect(:,k);
132
133 %% checking possible termination condition
134 if doTerminate
135 if eval(terminationCond)
136 x = x(:,1:k);
137 y = y(:,1:k);
138 break;
139 end
140 end
141
142 end
143
144 end
145