comparison m-toolbox/classes/@tsdata/tsdata.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 % TSDATA time-series object class constructor.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: TSDATA time-series object class constructor.
5 % Create a time-series data object. If the time-series object
6 % is determined to be evenly sampled, no x vector is stored.
7 %
8 % SUPER CLASSES: data2D < ltpda_data < ltpda_nuo < ltpda_obj
9 %
10 % CONSTRUCTORS:
11 %
12 % ts = tsdata() - creates a blank time-series object
13 % ts = tsdata(y) - creates a time-series object with the given
14 % y-data.
15 % ts = tsdata(x,y) - creates a time-series object with the given
16 % (x,y)-data. The sample rate is then set using
17 % the private method fitfs(). This computes the
18 % best sample rate that fits the data. If the
19 % data is evenly sampled, the sample rate is set
20 % as 1/median(diff(x)) and the x data is then
21 % not stored (empty vector).
22 % ts = tsdata(y,fs) - creates a time-series object with the given
23 % y-data. The data is assumed to be evenly sampled
24 % at the given sample rate with the first sample
25 % assigned time 0. No x vector is created.
26 % ts = tsdata(y,t0) - creates a time-series object with the given
27 % y-data. The data are assumed to be evenly
28 % sampled at 1Hz. The first sample is assumed to
29 % be at 0s offset from t0 and t0 is set to the
30 % user specified value.
31 % ts = tsdata(x,y,fs) - creates a time-series object with the given
32 % x/y data vectors. The sample rate is set to
33 % fs.
34 % ts = tsdata(x,y,t0) - creates a time-series object with the given
35 % x/y data vectors. The t0 property is set to
36 % the supplied t0 and the sample rate is
37 % computed from the x vector using fitfs(). If
38 % the data is found to be evenly sampled, the
39 % x vector is discarded.
40 % ts = tsdata(y,fs,t0) - creates a time-series object with the given
41 % y-data. The data are assumed to be evenly
42 % sampled at fs and the first sample is
43 % assumed to be taken at time t0. No x vector
44 % is generated.
45 % ts = tsdata(x,y,fs,t0)-creates a time-series object with the given
46 % x-data, y-data, fs and t0.
47 %
48 % VERSION: $Id: tsdata.m,v 1.86 2011/11/04 21:11:48 mauro Exp $
49 %
50 % SEE ALSO: tsdata, fsdata, xydata, cdata, data2D, data3D, xyzdata
51 %
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53
54 classdef (Hidden = true) tsdata < data2D
55
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 % Property definition %
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59
60 %---------- Public (read/write) Properties ----------
61 properties
62 end
63
64 %---------- Protected read-only Properties ----------
65 properties (GetAccess = public, SetAccess = protected)
66 t0 = time(0); % time-stamp of the first data sample in UTC format
67 toffset = 0; % time offset from t0 to the first data sample in milliseconds
68 fs = NaN; % sample rate of data
69 nsecs = 0; % the length of this time-series in seconds
70 end
71
72 %---------- Private Properties ----------
73 properties (GetAccess = protected, SetAccess = protected)
74 end
75
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 % Check property setting %
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79
80 methods
81 function set.t0(obj, val)
82 if (~isa(val, 'time') && ~ischar(val) && ~isnumeric(val))|| isempty(val)
83 error('### The value for the property ''t0'' must be a string, a number or a time object');
84 end
85 if ischar(val) || isnumeric(val)
86 obj.t0 = time(val);
87 else
88 obj.t0 = val;
89 end
90 end
91 function set.fs(obj, val)
92 if ~isnumeric(val) || isempty(val) || ~isreal(val) || val < 0
93 error('### The value for the property ''fs'' must be a real positive number');
94 end
95 obj.fs = val;
96 end
97 function set.nsecs(obj, val)
98 if ~isnumeric(val) || isempty(val) || ~isreal(val) || val < 0
99 error('### The value for the property ''nsecs'' must be a real positive number');
100 end
101 obj.nsecs = val;
102 end
103 end
104
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 % Constructor %
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108
109 methods
110 function obj = tsdata(varargin)
111
112 switch nargin
113 case 0
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 %%%%%%%%%%%%%%%%%%%%%%%%%%% no inputs %%%%%%%%%%%%%%%%%%%%%%%%%%%
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117
118 case 1
119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120 %%%%%%%%%%%%%%%%%%%%%%%%%%% one input %%%%%%%%%%%%%%%%%%%%%%%%%%%
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122
123 if isa(varargin{1}, 'tsdata')
124 %%%%%%%%%% data = tsdata(tsdata-object) %%%%%%%%%%
125 %----------- Copy tsdata Object %%%%%%%%%%
126 obj = copy(varargin{1}, 1);
127
128 elseif isstruct(varargin{1})
129 %%%%%%%%%% data = tsdata(struct) %%%%%%%%%%
130 obj = fromStruct(obj, varargin{1});
131
132 elseif isnumeric(varargin{1})
133 %%%%%%%%%% data = tsdata(y-vector) %%%%%%%%%%
134 %----------- y vector
135 obj.setY(varargin{1});
136 obj.fs = 1;
137
138 else
139 error('### Unknown single argument constructor.');
140 end
141
142 case 2
143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 %%%%%%%%%%%%%%%%%%%%%%%%%%% two input %%%%%%%%%%%%%%%%%%%%%%%%%%%
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146
147 if numel(varargin{1}) > numel(varargin{2}) && numel(varargin{2}) == 1 && ~isa(varargin{2}, 'time')
148 %%%%%%%%%% data = tsdata(y-vector, fs) %%%%%%%%%%
149 % tsdata(y,fs)
150 obj.y = varargin{1};
151 obj.fs = varargin{2};
152
153 elseif isa(varargin{1}, 'org.apache.xerces.dom.DeferredElementImpl') && ...
154 isa(varargin{2}, 'history')
155 %%%%%%%%%% obj = tsdata(DOM node, history-objects) %%%%%%%%%%
156 obj = fromDom(obj, varargin{1}, varargin{2});
157
158 elseif numel(varargin{1}) == numel(varargin{2})
159 %%%%%%%%%% data = tsdata(x-vector, y-vector) %%%%%%%%%%
160 % tsdata(x,y)
161 if numel(varargin{1}) > 1
162 [fs_fitfs,t0_fitfs,fitted] = tsdata.fitfs(varargin{1});
163 obj.fs = fs_fitfs;
164 if fitted
165 obj.setXY(varargin{1} - varargin{1}(1), varargin{2});
166 else
167 obj.setY(varargin{2});
168 end
169 obj.toffset = varargin{1}(1) * 1000;
170 else
171 obj.setXY(varargin{1} - varargin{1}(1), varargin{2});
172 obj.fs = 1;
173 t0_fitfs = 0;
174 obj.toffset = varargin{1}(1) * 1000;
175 end
176 obj.t0 = time(t0_fitfs);
177
178 elseif isa(varargin{2}, 'time')
179 %%%%%%%%%% data = tsdata(y-vector, t0) %%%%%%%%%%
180 % tsdata(y,t0)
181 obj.setY(varargin{1});
182 obj.fs = 1;
183 obj.t0 = varargin{2};
184
185 else
186 error('### Unknown two argument constructor.');
187 end
188 case 3
189 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190 %%%%%%%%%%%%%%%%%%%%%%%%%% three input %%%%%%%%%%%%%%%%%%%%%%%%%%
191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192
193 if numel(varargin{1}) == numel(varargin{2}) && numel(varargin{3}) == 1 && ~isa(varargin{3}, 'time')
194 %%%%%%%%%% data = tsdata(x-vector, y-vector, fs) %%%%%%%%%%
195 % tsdata(x,y,fs)
196 obj.fs = varargin{3};
197
198 [fs_fitfs,t0_fitfs,fitted] = tsdata.fitfs(varargin{1});
199 obj.t0 = t0_fitfs;
200 if fitted
201 obj.setXY(varargin{1} - varargin{1}(1), varargin{2});
202 else
203 if abs(fs_fitfs - obj.fs) > 2*eps(obj.fs)
204 error('The requested sample rate (%g) does not match the sample rate computed from the input x values (%g)', obj.fs, fs_fitfs);
205 end
206 obj.setY(varargin{2});
207 end
208 obj.toffset = varargin{1}(1) * 1000;
209
210 elseif numel(varargin{1}) == numel(varargin{2}) && isa(varargin{3}, 'time')
211 %%%%%%%%%% data = tsdata(x-vector, y-vector, t0) %%%%%%%%%%
212 % tsdata(x,y,t0)
213 [fs_fitfs,t0_fitfs,fitted] = tsdata.fitfs(varargin{1});
214 obj.fs = fs_fitfs;
215 obj.t0 = varargin{3} + t0_fitfs;
216 if fitted
217 obj.setXY(varargin{1} - varargin{1}(1), varargin{2});
218 else
219 obj.setY(varargin{2});
220 end
221 obj.toffset = varargin{1}(1) * 1000;
222
223 elseif numel(varargin{1}) > numel(varargin{2}) && isa(varargin{3}, 'time')
224 %%%%%%%%%% data = tsdata(y-vector, fs, t0) %%%%%%%%%%
225 % tsdata(y,fs,t0)
226 obj.setY(varargin{1});
227 obj.fs = varargin{2};
228 obj.t0 = varargin{3};
229 else
230 error('### Unknown three argument constructor.');
231 end
232 case 4
233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 %%%%%%%%%%%%%%%%%%%%%%%%%% four input %%%%%%%%%%%%%%%%%%%%%%%%%%%
235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236
237 if numel(varargin{1}) == numel(varargin{2}) && numel(varargin{3}) == 1 && isa(varargin{4}, 'time')
238 %%%%%%%%%% data = tsdata(x-vector, y-vector, fs, t0) %%%%%%%%%%
239 obj.fs = varargin{3};
240
241 [fs_fitfs,t0_fitfs,fitted] = tsdata.fitfs(varargin{1});
242 if fitted
243 obj.setXY(varargin{1} - varargin{1}(1), varargin{2});
244 else
245 if abs(fs_fitfs - obj.fs) > 2*eps(obj.fs)
246 error('The requested sample rate (%g) does not match the sample rate computed from the input x values (%g)', obj.fs, fs_fitfs);
247 end
248 obj.setY(varargin{2});
249 end
250 obj.toffset = varargin{1}(1) * 1000;
251 obj.fs = fs_fitfs;
252 obj.t0 = varargin{4}+t0_fitfs;
253 else
254 error('### Unknown four argument constructor.');
255 end
256 otherwise
257 error('### Unknown number of constructor arguments.');
258 end
259
260 % Now we can set nsecs
261 if ~isempty(obj.x)
262 % Then we have unevenly sampled data and the data duration
263 % is taken as x(end) - x(1);
264 obj.setNsecs(obj.x(end)-obj.x(1) + 1/obj.fs);
265 else
266 if ~isempty(obj.y)
267 % Then the data is evenly sampled and the
268 % duration of the data is easily computed.
269 Ndata = length(obj.y);
270 obj.setNsecs(Ndata / obj.fs);
271 end
272 end
273
274 end % End constructor
275 end % End public methods
276
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 % Methods (Public, hidden) %
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280
281 methods (Hidden = true)
282 varargout = attachToDom(varargin)
283 end
284
285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 % Methods (protected) %
287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288
289 methods (Access = protected)
290 varargout = fromStruct(varargin)
291 varargout = fromDom(varargin)
292 end
293
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295 % Methods (private) %
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297
298 methods (Access = private)
299 end
300
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 % Methods (static) %
303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 methods (Static)
305 varargout = fitfs(varargin)
306
307 function out = VEROUT()
308 out = '$Id: tsdata.m,v 1.86 2011/11/04 21:11:48 mauro Exp $';
309 end
310
311 function ii = getInfo(varargin)
312 ii = utils.helper.generic_getInfo(varargin{:}, 'tsdata');
313 end
314
315 function out = SETS()
316 out = {'Default'};
317 end
318
319 function out = getDefaultPlist(set)
320 switch lower(set)
321 case 'default'
322 out = plist();
323 otherwise
324 error('### Unknown set [%s]', set');
325 end
326 end
327
328 function obj = initObjectWithSize(n,m)
329 obj = tsdata.newarray([n m]);
330 end
331
332 end % End static methods
333
334 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335 % Methods (static, private) %
336 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
337
338 methods (Static, Access = private)
339 end % End static, private methods
340
341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 % Methods (static, hidden) %
343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344
345 methods (Static = true, Hidden = true)
346 varargout = loadobj(varargin)
347 varargout = update_struct(varargin);
348 end
349
350 end % End classdef