Home > classes > @tsdata > tsdata.m

tsdata

PURPOSE ^

TSDATA time-series object class constructor.

SYNOPSIS ^

function ts = tsdata(varargin)

DESCRIPTION ^

 TSDATA time-series object class constructor.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION: TSDATA time-series object class constructor.
              Create a time-series data object.

     Properties:
       name    - name of time-series object
       fs      - sample rate of data
       t       - time samples vector
       x       - x samples vector
       nsecs   - the length of this time-series in seconds
       xunits  - units to interpret the time samples (e.g., seconds)
       yunits  - units to interpret the data samples (e.g., Volts)
       t0      - time-stamp of the first data sample in UTC format yyyy-mm-dd HH:MM:SS
       created - creation time of this fsdata object. Created by the function now
       version - version of the constructor code

     Possible constructors:
       ts = tsdata()      - creates a blank time-series object
       ts = tsdata(x)     - creates a time-series object with the given
                            x-data.
       ts = tsdata(t,x)   - creates a time-series object with the given
                            (t,x)-data. The sample rate is then set as
                            1/(t(2)-t(1)).
       ts = tsdata(x,fs)  - creates a time-series object with the given
                            x-data. The time vector t[] is grown from the
                            sample rate. The first sample is assigned time 0.

 HISTORY: 30-01-2007 Hewitson
             Creation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function ts = tsdata(varargin)
0002 % TSDATA time-series object class constructor.
0003 %
0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0005 %
0006 % DESCRIPTION: TSDATA time-series object class constructor.
0007 %              Create a time-series data object.
0008 %
0009 %     Properties:
0010 %       name    - name of time-series object
0011 %       fs      - sample rate of data
0012 %       t       - time samples vector
0013 %       x       - x samples vector
0014 %       nsecs   - the length of this time-series in seconds
0015 %       xunits  - units to interpret the time samples (e.g., seconds)
0016 %       yunits  - units to interpret the data samples (e.g., Volts)
0017 %       t0      - time-stamp of the first data sample in UTC format yyyy-mm-dd HH:MM:SS
0018 %       created - creation time of this fsdata object. Created by the function now
0019 %       version - version of the constructor code
0020 %
0021 %     Possible constructors:
0022 %       ts = tsdata()      - creates a blank time-series object
0023 %       ts = tsdata(x)     - creates a time-series object with the given
0024 %                            x-data.
0025 %       ts = tsdata(t,x)   - creates a time-series object with the given
0026 %                            (t,x)-data. The sample rate is then set as
0027 %                            1/(t(2)-t(1)).
0028 %       ts = tsdata(x,fs)  - creates a time-series object with the given
0029 %                            x-data. The time vector t[] is grown from the
0030 %                            sample rate. The first sample is assigned time 0.
0031 %
0032 % HISTORY: 30-01-2007 Hewitson
0033 %             Creation
0034 %
0035 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0036 
0037 ALGONAME = mfilename;
0038 VERSION  = '$Id: tsdata.m,v 1.20 2007/10/12 15:30:16 ingo Exp $';
0039 
0040 %%%%%%%%%%%%%%%%%%%%%%%%   define tsdata properties   %%%%%%%%%%%%%%%%%%%%%%%%%
0041 
0042   function ts = init
0043     ts.name      = 'None';
0044     ts.t         = [];
0045     ts.x         = [];
0046     ts.fs        = 0;
0047     ts.xunits    = '';
0048     ts.yunits    = '';
0049     ts.nsecs     = 0;
0050     ts.t0        = time('t0_default');
0051     ts.version   = VERSION;
0052     ts.created   = time;
0053     ts = class(ts, 'tsdata');
0054   end
0055 
0056 %%%%%%%%%%%%%%%%%%%%%%%%%%   Create tsdata object   %%%%%%%%%%%%%%%%%%%%%%%%%%%
0057 
0058 %%%%%%%%%%  ts = tsdata()   %%%%%%%%%%
0059 % create default tsdata object
0060 if nargin == 0
0061 
0062   ts = init();
0063 
0064 elseif nargin == 1
0065 
0066   %%%%%%%%%% Create from XML fragment %%%%%%%%%%%
0067   if isa(varargin{1}, 'org.apache.xerces.dom.DeferredElementImpl')
0068     ts = fromxml(varargin{1});
0069   %%%%%%%%%%% From File %%%%%%%%%%%%%%%%
0070   elseif ischar(varargin{1})
0071 
0072     filename = varargin{1};
0073     [path, name, ext, vers] = fileparts(filename);
0074     switch ext
0075       case '.mat'
0076         ts = load(filename);
0077       case '.xml'
0078         ts = xmlparse(tsdata, filename);
0079       otherwise
0080         error('### Unknown file type.');
0081     end
0082   %%%%%%%%%%  ts = tsdata(tsdata)   %%%%%%%%%%
0083   elseif isa(varargin{1}, 'tsdata')
0084     ts = varargin{1};
0085 
0086   %%%%%%%%%%  ts = tsdata(struct)   %%%%%%%%%%
0087   elseif isstruct(varargin{1})
0088 
0089     ts = init();
0090 
0091     fields = fieldnames(varargin{1});
0092     for ii = 1:length(fields)
0093       field = fields{ii};
0094 
0095       %%% created -> time-object
0096       if strcmp(field, 'created')
0097         created = varargin{1}.created;
0098         if isstruct(created)
0099           created = time(created);
0100         end
0101         ts.created     = created;
0102       %%% t0 -> time-object
0103       elseif strcmp(field, 't0')
0104         t0 = varargin{1}.t0;
0105         if isstruct(t0)
0106           t0 = time(t0);
0107         end
0108         ts.t0 = t0;
0109       %%% All other
0110       else
0111         try
0112           ts.(field) = varargin{1}.(field);
0113         catch
0114           error('### The field ''%s'' in the struct is not a tsdata property.', field)
0115         end
0116       end
0117     end
0118 
0119   %%%%%%%%%%  ts = tsdata(y_vector)   %%%%%%%%%%
0120   else
0121 
0122     ts = init();
0123 
0124     ts.x     = varargin{1};
0125     ts.fs    = 1;
0126     ts.nsecs = length(ts.x)/ts.fs;
0127 
0128     % Unify the y-axis
0129     if size(ts.x,2) > size(ts.x,1)
0130       ts.x = [ts.x].';
0131     end
0132     ts = setTime(ts);
0133 
0134     % OLD Version:
0135     %     ts.name    = 'None';
0136     %     ts.t       = [];
0137     %     ts.x       = varargin{1};
0138     %     ts.fs      = 1;
0139     %     ts.xunits  = '';
0140     %     ts.yunits  = '';
0141     %     ts.nsecs   = length(ts.x)/ts.fs;
0142     %     ts.t0      = time('t0_default');
0143     %     ts.version = VERSION;
0144     %     ts.created   = time;
0145     %     ts = class(ts, 'tsdata');
0146     %     ts = setTime(ts);
0147     %     if size(ts.x,2) > size(ts.x,1)
0148     %       ts.x = [ts.x].';
0149     %     end
0150 
0151   end
0152 
0153 elseif nargin == 2
0154 
0155   %%%%%%%%%%% From DATABASE
0156   if isa(varargin{1}, 'database')
0157     ts = retrieve(varargin{1}, varargin{2:end});
0158     return
0159   %%%%%%%%%%  ts = tsdata(x_vector, y_vector)   %%%%%%%%%%
0160   elseif length(varargin{1}) == length(varargin{2}) && length(varargin{1})>1
0161 
0162     ts = init();
0163 
0164     ts.t       = varargin{1};
0165     ts.x       = varargin{2};
0166     ts.fs      = 1.0/(ts.t(min(2,length(ts.t)))-ts.t(1));
0167     ts.nsecs   = ts.t(end) - ts.t(1) + 1/ts.fs;
0168 
0169     % Unify the y-axis
0170     if size(ts.x,2) > size(ts.x,1)
0171       ts.x = [ts.x].';
0172     end
0173     ts = reshapeT(ts);
0174 
0175     % OLD Version
0176     %     ts.name    = 'None';
0177     %     ts.t       = varargin{1};
0178     %     ts.x       = varargin{2};
0179     %     ts.fs      = 1.0/(ts.t(min(2,length(ts.t)))-ts.t(1));
0180     %     ts.xunits  = '';
0181     %     ts.yunits  = '';
0182     %     ts.nsecs   = ts.t(end) - ts.t(1) + 1/ts.fs;
0183     %     ts.t0      = time('t0_default');
0184     %     ts.version = VERSION;
0185     %     ts.created   = time;
0186     %     ts = class(ts, 'tsdata');
0187     %     if size(ts.x,2) > size(ts.x,1)
0188     %       ts.x = [ts.x].';
0189     %     end
0190     %     ts = reshapeT(ts);
0191 
0192   %%%%%%%%%%  ts = tsdata(y_vector, fs)   %%%%%%%%%%
0193   else
0194     % check we have fs properly here
0195     if length(varargin{2}) > 1
0196       error(['### unknown constructor call. Either t and x should be the' ...
0197         'same length, or fs should be a single value.']);
0198     end
0199 
0200     ts = init();
0201 
0202     ts.x       = varargin{1};
0203     ts.fs      = varargin{2};
0204     ts.nsecs   = length(ts.x)/ts.fs;
0205 
0206     if size(ts.x,2) > size(ts.x,1)
0207       ts.x = [ts.x].';
0208     end
0209     ts = setTime(ts);
0210 
0211     % OLD Version:
0212     %     ts.name    = 'None';
0213     %     ts.t       = [];
0214     %     ts.x       = varargin{1};
0215     %     ts.fs      = varargin{2};
0216     %     ts.xunits  = '';
0217     %     ts.yunits  = '';
0218     %     ts.nsecs   = length(ts.x)/ts.fs;
0219     %     ts.t0      = time('t0_default');
0220     %     ts.version = VERSION;
0221     %     ts.created   = time;
0222     %     ts = class(ts, 'tsdata');
0223     %     if size(ts.x,2) > size(ts.x,1)
0224     %       ts.x = [ts.x].';
0225     %     end
0226     %     ts = setTime(ts);
0227 
0228   end
0229 else
0230   error('### Unknown number of constructor arguments.');
0231 end
0232 
0233 
0234 end % function ts = tsdata(varargin)

Generated on Fri 02-Nov-2007 19:39:27 by m2html © 2003