Initial commit
[pftoolbox.git] / simulators / @simulator / graph.m
blob472ce74d15e4cf54129841decf83013c7c1312a1
1 function [simobj, cfilters]=graph(varargin);\r
2 % Graph method for the simulator class.\r
3 % Takes a simulator object and a filter, or a cell array of filters. Simulates data,\r
4 % filters it and visualizes the result.\r
5 %\r
6 % Syntax: (* = optional)\r
7 %\r
8 % graph(simobj, filtarray, steps, states*, u*, t*);\r
9 % graph(simobj, filtarray, steps, states*, u*, Ts*);\r
10 %\r
11 % In arguments:\r
12 %\r
13 % 1. simobj\r
14 %       simulator object that will be used for the filtering\r
15 % 2. filtarray\r
16 %       A filter object, or cell array of filter objects for use with the simulated data.\r
17 %       If a filter object is empty, the model assigned to the simulator object will be\r
18 %       assigned to the empty filter object.\r
19 % 3. steps\r
20 %       A positive integer containing the number of desired simulation steps.\r
21 %       These will be carried out, filtered and visualized. Note that no previous simulation or\r
22 %       filtered data (from the objects) will be used.\r
23 % 4* states\r
24 %       A scalar or a vector containing the states to be visualized.\r
25 % 4* []\r
26 %       'states' is set to 1, ie the first state is plotted.\r
27 % 5* u\r
28 %       A matrix u(t) containg deterministic data.\r
29 % 5* []\r
30 %       No u(t) will be used in the calculations.\r
31 % 6* Ts\r
32 %       A vector containing the desired sample points, where Ts(k) represents the\r
33 %       time of step k.\r
34 % 6* t\r
35 %       The time of the first simulation step. The discrete times of the succeeding steps will be\r
36 %       calculated using the T property of the assigned model.\r
37 %       The result is a generated sample point vector, Ts, that will be stored in the\r
38 %       simulator object.\r
39 % 6* []\r
40 %       't' will be set to simobj.t and the sample points, 'Ts', will be generated on\r
41 %       the basis of this.\r
43 % Toolbox for nonlinear filtering.\r
44 % Copyright (C) 2005  Jakob Rosén <jakob.rosen@gmail.com>\r
45 %\r
46 % This program is free software; you can redistribute it and/or\r
47 % modify it under the terms of the GNU General Public License\r
48 % as published by the Free Software Foundation; either version 2\r
49 % of the License, or (at your option) any later version.\r
50 %\r
51 % This program is distributed in the hope that it will be useful,\r
52 % but WITHOUT ANY WARRANTY; without even the implied warranty of\r
53 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
54 % GNU General Public License for more details.\r
55 %\r
56 % You should have received a copy of the GNU General Public License\r
57 % along with this program; if not, write to the Free Software\r
58 % Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\r
60 simobj=varargin{1};\r
61 if isempty(simobj)\r
62         error('The simulator object has no model!');\r
63 end\r
64 cfilters=varargin{2};\r
65 steps=varargin{3};\r
66 T=get(get(simobj,'model'),'T');         % Get the discrete time of the model\r
68 if isa(cfilters,'cell')\r
69         % A cell array was supplied. Calculate its length!\r
70         filters=length(cfilters);\r
71 else\r
72         % A single object was supplied. Make it into a cell array for compability!\r
73         cfilters={cfilters};\r
74         filters=1;\r
75 end\r
77 % Since these arguments defaults to [], we don't have to go through the long procedure with them\r
78 if nargin>=5; u=varargin{5}; else; u=[]; end;\r
80 % Declare the arguments\r
81 states=[];\r
83 % Fetch arguments, if they exist\r
84 if nargin>=4; states=varargin{4}; end\r
86 % If an empty argument, or no argument at all, was supplied - use its default value!\r
87 if isempty(states)\r
88         states=1;\r
89 end\r
91 % Default t, Ts\r
92 t=simobj.t;                     % Use t stored in the simulator object\r
93 Ts=(t:T:(t+(steps-1)*T))';      % Calculate sample points\r
95 % Fetch t or Ts, if they are supplied and non-empty\r
96 if nargin>=6;                   % Ts or t was supplied by the user\r
97         t=varargin{6};\r
98         if length(t)>1\r
99                 if length(t)~=steps\r
100                         error('Incorrect size of supplied Ts vector');\r
101                 else\r
102                         % A sample point vector Ts was supplied. Use it!\r
103                         if size(t,2)>1  % Ts is a row vector (we expect only one-dimensional vectors)\r
104                                 Ts=t';  % Transpose and copy it!\r
105                         else\r
106                                 Ts=t;   % Ts is a column vector. Just copy it!\r
107                         end\r
108                 end\r
109         elseif length(t)        % Don't overwrite old Ts if the new t=[]\r
110                 Ts=(t:T:(t+(steps-1)*T))'; % Generate new sample points with respect to t\r
111         end\r
112 end\r
114 % Check if any filter object is empty. If so, use the model of the simulator object.\r
115 for j=1:filters\r
116         if isempty(cfilters{j})\r
117                 filtobj=cfilters{j};    % set.m needs a named variable\r
118                 cfilters{j}=set(filtobj,'model',simobj.model);\r
119         end;\r
120 end             \r
122 % Start the action\r
123 hf=figure('Name','Graph');\r
124 wbar = waitbar(0,'Simulating...');\r
126 legendcell=cell(filters+1,1);\r
127 legendcell{1}=class(simobj);\r
129 [y xtrue]=simulate(simobj,steps,u,Ts);\r
130 figure(hf);\r
131 plot(Ts,xtrue(states,:),getcolors(1));\r
133 %drawnow\r
134 hold on\r
136 for j=1:filters\r
137         waitbar(j/(filters+1),wbar,strcat('Filtering: ',class(cfilters{j})));\r
138         figure(wbar);\r
139         drawnow\r
140         % The assignin construction introduces some "redundant" code, since\r
141         % the filter command isn't able to operate on the cell array directly.\r
142         filtobj=cfilters{j};\r
143         [xhat xpred]=filter(filtobj,y,u,Ts);\r
144         % The filter object is written back into the array for simplicity.\r
145         cfilters{j}=filtobj;\r
146         figure(hf);\r
147         plot(Ts,xhat(states,:),getcolors(j+1));\r
148 %       drawnow\r
149         legendcell{j+1}=class(cfilters{j});\r
150 end;\r
152 waitbar(1,wbar,'Complete!');\r
153 drawnow\r
155 hold off\r
156 xlabel('Time (t)')\r
157 legend(legendcell{:})\r
159 close(wbar);\r