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
       x       - time samples vector
       y       - 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(y)     - creates a time-series object with the given
                            x-data.
       ts = tsdata(x,y)   - 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(y,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 %       x       - time samples vector
0013 %       y       - 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(y)     - creates a time-series object with the given
0024 %                            x-data.
0025 %       ts = tsdata(x,y)   - 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(y,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 VERSION  = '$Id: tsdata.html,v 1.14 2008/03/31 10:27:36 hewitson Exp $';
0038 CATEGORY = 'Constructor';
0039 
0040 %%%%%   Is this a 'Params' call?     %%%%%
0041 if  nargin == 2 && isa(varargin{1}, 'tsdata') && ischar(varargin{2})
0042   if strcmp(varargin{2}, 'Params')
0043     ts = plist();
0044     return
0045   elseif strcmp(varargin{2}, 'Version')
0046     ts = VERSION;
0047     return
0048   elseif strcmp(varargin{2}, 'Category')
0049     ts = CATEGORY;
0050     return
0051   end
0052 end
0053 
0054 %%%%%%%%%%%%%%%%%%%%%%%%   define tsdata properties   %%%%%%%%%%%%%%%%%%%%%%%%%
0055 
0056   function ts = init(version)
0057     ts.name      = 'None';
0058     ts.x         = [];
0059     ts.y         = [];
0060     ts.fs        = 0;
0061     ts.xunits    = '';
0062     ts.yunits    = '';
0063     ts.nsecs     = 0;
0064     ts.t0        = time('t0_default');
0065     ts.version   = version;
0066     ts.created   = time;
0067     ts = class(ts, 'tsdata');
0068   end
0069 
0070 %%%%%%%%%%%%%%%%%%%%%%%%%%   Create tsdata object   %%%%%%%%%%%%%%%%%%%%%%%%%%%
0071 
0072 %%%%%%%%%%  ts = tsdata()   %%%%%%%%%%
0073 % create default tsdata object
0074 if nargin == 0
0075 
0076   ts = init(VERSION);
0077 
0078 elseif nargin == 1
0079 
0080   %%%%%%%%%%% From File %%%%%%%%%%%%%%%%
0081   if ischar(varargin{1})
0082 
0083     filename = varargin{1};
0084     [path, name, ext, vers] = fileparts(filename);
0085     switch ext
0086       case '.mat'
0087         ts = load(filename);
0088         ts = ts.a;
0089       case '.xml'
0090         root_node = xmlread(filename);
0091         ts = ltpda_xmlread(root_node, 'tsdata');
0092       otherwise
0093         error('### Unknown file type.');
0094     end
0095   %%%%%%%%%%  ts = tsdata(tsdata)   %%%%%%%%%%
0096   elseif isa(varargin{1}, 'tsdata')
0097     ts = varargin{1};
0098 
0099   %%%%%%%%%%  ts = tsdata(struct)   %%%%%%%%%%
0100   elseif isstruct(varargin{1})
0101 
0102     ts = init(VERSION);
0103 
0104     fields = fieldnames(varargin{1});
0105     for ii = 1:length(fields)
0106       field = fields{ii};
0107 
0108       %%% created -> time-object
0109       if strcmp(field, 'created')
0110         created = varargin{1}.created;
0111         if isstruct(created)
0112           created = time(created);
0113         end
0114         ts.created     = created;
0115       %%% t0 -> time-object
0116       elseif strcmp(field, 't0')
0117         t0 = varargin{1}.t0;
0118         if isstruct(t0)
0119           t0 = time(t0);
0120         end
0121         ts.t0 = t0;
0122       %%% All other
0123       else
0124         try
0125           ts.(field) = varargin{1}.(field);
0126         catch
0127           error('### The field ''%s'' in the struct is not a tsdata property.', field)
0128         end
0129       end
0130     end
0131 
0132   %%%%%%%%%%  ts = tsdata(y_vector)   %%%%%%%%%%
0133   elseif isnumeric(varargin{1}) && length(varargin{1}) > 1
0134 
0135     ts = init(VERSION);
0136 
0137     ts.y     = varargin{1};
0138     ts.fs    = 1;
0139     ts.nsecs = length(ts.y)/ts.fs;
0140 
0141     % Unify the y-axis
0142     if size(ts.y,2) > size(ts.y,1)
0143       ts.y = [ts.y].';
0144     end
0145     ts = setTime(ts);
0146 
0147   %%%%%%%%%%  ts = tsdata(plist)   %%%%%%%%%%
0148   elseif isa(varargin{1}, 'plist')
0149     %% is the plist is empty then return an empty tsdata object
0150     if nparams(varargin{1}) == 0
0151       ts = init(VERSION);
0152     else
0153       error('### Unknown tsdata constructor method.');
0154     end
0155 
0156   else
0157     error('### Unknown tsdata constructor method.');
0158   end
0159 
0160 elseif nargin == 2
0161 
0162   %%%%%%%%%%% From DATABASE
0163   if isa(varargin{1}, 'database')
0164     ts = retrieve(varargin{1}, varargin{2:end});
0165     return
0166   %%%%%%%%%%  ts = tsdata(x_vector, y_vector)   %%%%%%%%%%
0167   elseif length(varargin{1}) == length(varargin{2}) && length(varargin{1})>1
0168 
0169     ts = init(VERSION);
0170 
0171     ts.x       = varargin{1};
0172     ts.y       = varargin{2};
0173     ts.fs      = 1.0/(ts.x(min(2,length(ts.x)))-ts.x(1));
0174     ts.nsecs   = ts.x(end) - ts.x(1) + 1/ts.fs;
0175 
0176     % Unify the y-axis
0177     if size(ts.y,2) > size(ts.y,1)
0178       ts.y = [ts.y].';
0179     end
0180     ts = reshapeT(ts);
0181 
0182   %%%%%%%%%%  ts = tsdata(y_vector, fs)   %%%%%%%%%%
0183   else
0184     % check we have fs properly here
0185     if length(varargin{2}) > 1
0186       error(['### unknown constructor call. Either x and y should be the' ...
0187         'same length, or fs should be a single value.']);
0188     end
0189 
0190     ts = init(VERSION);
0191 
0192     ts.y       = varargin{1};
0193     ts.fs      = varargin{2};
0194     ts.nsecs   = length(ts.y)/ts.fs;
0195 
0196     if size(ts.y,2) > size(ts.y,1)
0197       ts.y = [ts.y].';
0198     end
0199     ts = setTime(ts);
0200 
0201   end
0202 else
0203   error('### Unknown number of constructor arguments.');
0204 end
0205 
0206 
0207 end % function ts = tsdata(varargin)

Generated on Mon 31-Mar-2008 12:20:24 by m2html © 2003