Mercurial > forge
changeset 10000:8a65d8abd503 octave-forge
system-identitfication: Adding devel of timeseries class
author | jpicarbajal |
---|---|
date | Wed, 11 Apr 2012 09:57:29 +0000 |
parents | e9b30cee9148 |
children | a79cdab8c4db |
files | main/system-identification/devel/@timeseries/get.m main/system-identification/devel/@timeseries/plot.m main/system-identification/devel/@timeseries/set.m main/system-identification/devel/@timeseries/timeseries.m |
diffstat | 4 files changed, 300 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/system-identification/devel/@timeseries/get.m Wed Apr 11 09:57:29 2012 +0000 @@ -0,0 +1,47 @@ +%% Copyright (c) 2012 Juan Pablo Carbajal <carbajal@ifi.uzh.ch> +%% +%% This program is free software: you can redistribute it and/or modify +%% it under the terms of the GNU General Public License as published by +%% the Free Software Foundation, either version 3 of the License, or +%% any later version. +%% +%% This program is distributed in the hope that it will be useful, +%% but WITHOUT ANY WARRANTY; without even the implied warranty of +%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +%% GNU General Public License for more details. +%% +%% You should have received a copy of the GNU General Public License +%% along with this program. If not, see <http://www.gnu.org/licenses/>. + +%% -*- texinfo -*- +%% @deftypefn {Function File} {@var{field} = } get (@var{obj}, @var{fieldname}) +%% Get the value of a field of the timeseries object. +%% +%% If @var{fieldname} is a cell, it returns a structure containing the fields +%% retrieved. +%% +%% @end deftypefn + +function field = get (obj, fieldname) + + if !iscell (fieldname) + fieldname = {fieldname}; + end + + tf = ismember (fieldname, fieldnames(obj)); + not_found = {fieldname{!tf}}; + + if !isempty (not_found) + msg = @(x) warning("Field '%s' not found in object timeseries.\n",x); + cellfun (msg, not_found); + end + + fieldname = {fieldname{tf}}; + func = @(x) getfield (struct (obj), x); + f = cellfun (func, fieldname, 'UniformOutput', false); + field = cell2struct (f, fieldname, 2); + + if numel(fieldname) == 1 + field = field.(fieldname{1}); + end +endfunction
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/system-identification/devel/@timeseries/plot.m Wed Apr 11 09:57:29 2012 +0000 @@ -0,0 +1,40 @@ +%% Copyright (c) 2012 Juan Pablo Carbajal <carbajal@ifi.uzh.ch> +%% +%% This program is free software: you can redistribute it and/or modify +%% it under the terms of the GNU General Public License as published by +%% the Free Software Foundation, either version 3 of the License, or +%% any later version. +%% +%% This program is distributed in the hope that it will be useful, +%% but WITHOUT ANY WARRANTY; without even the implied warranty of +%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +%% GNU General Public License for more details. +%% +%% You should have received a copy of the GNU General Public License +%% along with this program. If not, see <http://www.gnu.org/licenses/>. + +%% -*- texinfo -*- +%% @deftypefn {Function File} {@var{h} = } plot (@var{obj}) +%% Plots the timeseries object. +%% +%% @end deftypefn + +function varargout = plot (obj, varargin) +%% TODO this is a placeholder function + + t = obj.Time; + y = obj.Data; + names = obj.Name; + + hplot = plot (t, y); + axis tight + grid on + hleg = legend (hplot, names); + + h.plot = hplot; + h.legend = hleg; + if nargout == 1 + varargout{1} = h; + end + +endfunction
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/system-identification/devel/@timeseries/set.m Wed Apr 11 09:57:29 2012 +0000 @@ -0,0 +1,94 @@ +%% Copyright (c) 2012 Juan Pablo Carbajal <carbajal@ifi.uzh.ch> +%% +%% This program is free software: you can redistribute it and/or modify +%% it under the terms of the GNU General Public License as published by +%% the Free Software Foundation, either version 3 of the License, or +%% any later version. +%% +%% This program is distributed in the hope that it will be useful, +%% but WITHOUT ANY WARRANTY; without even the implied warranty of +%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +%% GNU General Public License for more details. +%% +%% You should have received a copy of the GNU General Public License +%% along with this program. If not, see <http://www.gnu.org/licenses/>. + +%% -*- texinfo -*- +%% @deftypefn {Function File} set (@var{obj}, @var{fieldname},@var{value}) +%% @deftypefnx {Function File} set (@var{obj}, @var{fieldname},@var{value},@dots) +%% Set the value of a field of the timeseries object. +%% +%% If @var{fieldname} is a cell so must be @var{value}, it sets the values of +%% all matching fields. +%% +%% The function also accepts property-value pairs. +%% +%% @end deftypefn + +function obj = set (obj, varargin) + + if numel (varargin) == 2 && iscell (varargin{1}) && iscell (varargin{2}) + %% The arguments are two cells, expecting fields and values. + fieldname = varargin{1}; + value = varargin{2}; + else + fieldname = {varargin{1:2:end}}; + value = {varargin{2:2:end}}; + end + + + if numel (fieldname) != numel (value) + error ('timeseries:set:InvalidArgument', ... + 'FIELDS and VALUES must have the same number of elements.'); + end + + tf = ismember (fieldname, fieldnames(obj)); + not_found = {fieldname{!tf}}; + + if !isempty (not_found) + msg = @(x) warning("Field '%s' not found in object timeseries.\n",x); + cellfun (msg, not_found); + end + + fieldname = {fieldname{tf}}; + value = {value{tf}}; + + %% Check data consistency + [tf idx] = ismember ({'Time','Data'},fieldname); + if tf(1) && !tf(2) + if length(value{idx(1)}) != size(obj.Data , 1) + error ('timeseries:set:InvalidArgument', ... + ["Time vector must have lenght equal to the first dimension" ... + "of Data (%d)\n"], ... + size(obj.Data , 1)); + end + elseif !tf(1) && tf(2) + if length(obj.Time) != size(value{idx(2)}, 1) + warning ('timeseries:set:InvalidArgument', ... + ["Data vector of different size as Time vector (%d)." ... + " Time vector will be reseted."], length(obj.Time)); + fieldname{end+1} = 'Time'; + value{end+1} = 0:1:(size(value{idx(2)}, 1)-1); + end + elseif tf(1) && tf(2) + if length(value{idx(1)}) != size(value{idx(2)}, 1) + error ('timeseries:set:InvalidArgument', ... + ["Time vector must have lenght equal to the first dimension" ... + "of Data (%d)\n"], ... + size(value{idx(2)}, 1)); + end + end + + if tf(1) + fieldname{end+1} = 'Length'; + value{end+1} = length(value{idx(1)}); + end + + %% Set values + % Can't avoid the loop with cellfun, cause the handle to cellfun is treated as + % a external function. + for i = 1:numel(fieldname) + obj.(fieldname{i}) = value{i}; + end + +endfunction
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/system-identification/devel/@timeseries/timeseries.m Wed Apr 11 09:57:29 2012 +0000 @@ -0,0 +1,119 @@ +## Copyright (c) 2012 Juan Pablo Carbajal <carbajal@ifi.uzh.ch> +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, see <http://www.gnu.org/licenses/>. + + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{obj} =} timeseries () +## @deftypefnx {Function File} {@var{obj} =} timeseries (@var{data}) +## @deftypefnx {Function File} {@var{obj} =} timeseries (@var{data},@var{time}) +## @deftypefnx {Function File} {@var{obj} =} timeseries (@dots,@var{Property},@var{Value}) +## Create object of the timeseries class. +## +## If no input argument is provided the object is empty. +## If the @var{data} argument is present, creates the timeseries object using the specified data. +## If @var{data} and @var{time} are provided, creates the timeseries object using the specified data and time. +## Property-value pairs can be specified to set several properties of the object. +## +## @end deftypefn + +function timeseries = timeseries(Data=[],Time=[],varargin) + + timeseries = struct (); + + # --- Parse arguments --- # + parser = inputParser (); + parser.FunctionName = "timeseries"; + parser = addParamValue (parser,'Data', Data, @ismatrix); + parser = addParamValue (parser,'DataInfo', ... + struct ('Unit', '', 'UserData','',... + 'Interpolation', ... + struct ('Fhandle', [], 'Name', 'linear')), ... + @isstruct); + func = @(x) arrayfun ( ... + @(x)sprintf('Series %d',x), 1:size(Data,2), 'UniformOutput', false); + parser = addParamValue (parser,'Name', func(), @iscell); + parser = addParamValue (parser,'Time', Time, @ismatrix); + parser = parse(parser,varargin{:}); + + %% Data + %% Timeseries data, where each data sample corresponds to a specific time + %% The data can be a scalar, a vector, or a multidimensional array. The first + %% dimension of the data must align with Time (this is less general than MATLAB implementation). + %% By default, NA represent missing or unspecified data. + %% Though it is not handled properly yet. + timeseries.Data = parser.Results.Data; + + %% Length + %% Length of the time vector in the timeseries object. Internal use. + timeseries.Length = size (timeseries.Data, 1); + + %% DataInfo + %% Contains fields for storing contextual information about Data: + %% Unit — String that specifies data units + %% Interpolation — A struct that specifies the interpolation method for this + %% timeseries object. + %% Fields of the struct include: + %% Fhandle — Function handle to a user-defined interpolation function. + %% Name — String that specifies the name of the interpolation method. + %% 'linear' is the default. + %% UserData — Any user-defined information entered as a string + timeseries.DataInfo = parser.Results.DataInfo; + + %% Name + %% The timeseries object name entered as a string. This name can differ from + %% the name of the timeseries variable in the workspace. + timeseries.Name = parser.Results.Name; + + %% Time + %% Array of time values. The lenght must coincide with the length of the first + %% dimension of the data. + timeseries.Time = parser.Results.Time; + if isempty (timeseries.Time) + timeseries.Time = 0:1:(timeseries.Length-1); + end + + %% Events + %% Not used. + timeseries.Events = []; + + %% IsTimeFirst + %% Not used + timeseries.IsTimeFirst = []; + + %% Quality + %% Not used + timeseries.Quality = []; + + %% QualityInfo + %% Not used + timeseries.QualityInfo = []; + + %% TimeInfo + %% Not used. + timeseries.TimeInfo = []; + + %% TreatNaNasMissing + %% Not implemented. Logical value that specifies how to treat NA values in Data. + timeseries.TreatNaNasMissing = []; + + %% UserData + %% Not used. + timeseries.UserData = []; + + timeseries = class (timeseries, 'timeseries'); + +endfunction + +%!test