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.m,v 1.24 2008/02/13 12:12:49 mauro 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
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();
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();
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   else
0134 
0135     ts = init();
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   end
0148 
0149 elseif nargin == 2
0150 
0151   %%%%%%%%%%% From DATABASE
0152   if isa(varargin{1}, 'database')
0153     ts = retrieve(varargin{1}, varargin{2:end});
0154     return
0155   %%%%%%%%%%  ts = tsdata(x_vector, y_vector)   %%%%%%%%%%
0156   elseif length(varargin{1}) == length(varargin{2}) && length(varargin{1})>1
0157 
0158     ts = init();
0159 
0160     ts.x       = varargin{1};
0161     ts.y       = varargin{2};
0162     ts.fs      = 1.0/(ts.x(min(2,length(ts.x)))-ts.x(1));
0163     ts.nsecs   = ts.x(end) - ts.x(1) + 1/ts.fs;
0164 
0165     % Unify the y-axis
0166     if size(ts.y,2) > size(ts.y,1)
0167       ts.y = [ts.y].';
0168     end
0169     ts = reshapeT(ts);
0170 
0171   %%%%%%%%%%  ts = tsdata(y_vector, fs)   %%%%%%%%%%
0172   else
0173     % check we have fs properly here
0174     if length(varargin{2}) > 1
0175       error(['### unknown constructor call. Either x and y should be the' ...
0176         'same length, or fs should be a single value.']);
0177     end
0178 
0179     ts = init();
0180 
0181     ts.y       = varargin{1};
0182     ts.fs      = varargin{2};
0183     ts.nsecs   = length(ts.y)/ts.fs;
0184 
0185     if size(ts.y,2) > size(ts.y,1)
0186       ts.y = [ts.y].';
0187     end
0188     ts = setTime(ts);
0189 
0190   end
0191 else
0192   error('### Unknown number of constructor arguments.');
0193 end
0194 
0195 
0196 end % function ts = tsdata(varargin)

Generated on Fri 07-Mar-2008 15:46:43 by m2html © 2003