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

Generated on Tue 22-Jan-2008 10:39:13 by m2html © 2003