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
6 % Syntax: (* = optional)
\r
8 % graph(simobj, filtarray, steps, states*, u*, t*);
\r
9 % graph(simobj, filtarray, steps, states*, u*, Ts*);
\r
14 % simulator object that will be used for the filtering
\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
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
24 % A scalar or a vector containing the states to be visualized.
\r
26 % 'states' is set to 1, ie the first state is plotted.
\r
28 % A matrix u(t) containg deterministic data.
\r
30 % No u(t) will be used in the calculations.
\r
32 % A vector containing the desired sample points, where Ts(k) represents the
\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
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
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
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
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
62 error('The simulator object has no model!');
\r
64 cfilters=varargin{2};
\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
72 % A single object was supplied. Make it into a cell array for compability!
\r
73 cfilters={cfilters};
\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
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
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
100 error('Incorrect size of supplied Ts vector');
\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
106 Ts=t; % Ts is a column vector. Just copy it!
\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
114 % Check if any filter object is empty. If so, use the model of the simulator object.
\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
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
131 plot(Ts,xtrue(states,:),getcolors(1));
\r
137 waitbar(j/(filters+1),wbar,strcat('Filtering: ',class(cfilters{j})));
\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
147 plot(Ts,xhat(states,:),getcolors(j+1));
\r
149 legendcell{j+1}=class(cfilters{j});
\r
152 waitbar(1,wbar,'Complete!');
\r
157 legend(legendcell{:})
\r