]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/data2fun.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / data2fun.m
1 %% Copyright (c) 2011 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
2 %%
3 %% This program is free software; you can redistribute it and/or modify it under
4 %% the terms of the GNU General Public License as published by the Free Software
5 %% Foundation; either version 3 of the License, or (at your option) any later
6 %% version.
7 %%
8 %% This program is distributed in the hope that it will be useful, but WITHOUT
9 %% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 %% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 %% details.
12 %%
13 %% You should have received a copy of the GNU General Public License along with
14 %% this program; if not, see <http://www.gnu.org/licenses/>.
15
16 %% -*- texinfo -*-
17 %% @deftypefn {Function File} {[@var{fhandle}, @var{fullname}] = } data2fun (@var{ti}, @var{yi})
18 %% @deftypefnx {Function File} {[ @dots{} ] = } data2fun (@var{ti}, @var{yi},@var{property},@var{value})
19 %% Creates a vectorized function based on data samples using interpolation.
20 %%
21 %% The values given in @var{yi} (N-by-k matrix) correspond to evaluations of the
22 %% function y(t) at the points @var{ti} (N-by-1 matrix).
23 %% The data is interpolated and the function handle to the generated interpolant
24 %% is returned.
25 %%
26 %% The function accepts property-value pairs described below.
27 %%
28 %% @table @samp
29 %% @item file
30 %% Code is generated and .m file is created. The @var{value} contains the name
31 %% of the function. The returned function handle is a handle to that file. If
32 %% @var{value} is empty, then a name is automatically generated using
33 %% @code{tmpnam} and the file is created in the current directory. @var{value}
34 %% must not have an extension, since .m will be appended.
35 %% Numerical value used in the function are stored in a .mat file with the same
36 %% name as the function.
37 %%
38 %% @item interp
39 %% Type of interpolation. See @code{interp1}.
40 %%
41 %% @end table
42 %%
43 %% @seealso{interp1}
44 %% @end deftypefn
45
46 function [fhandle fullfname] = data2fun( t, y, varargin)
47
48   %% Check input arguments
49   interp_args = {"spline"};
50   given = struct("file",false);
51   if ~isempty(varargin)
52       % Arguments
53       interp_args = varargin;
54
55       opt_args = fieldnames (given);
56       [tf idx] = ismember( opt_args, varargin);
57       for i=1:numel(opt_args)
58         given.(opt_args{i}) = tf(i);
59       end
60
61       if given.file
62         %% TODO: check that file will be in the path. Otherwise fhabdle(0) fails.
63
64         if !isempty(varargin{idx(1)+1})
65
66           [DIR fname] = fileparts(varargin{idx(1)+1});
67
68         else
69
70           [DIR fname] = fileparts (tmpnam (pwd (),"agen_"));
71
72         end
73
74         interp_args(idx(1)+[0 1]) = [];
75       end
76
77       if isempty(interp_args)
78
79         interp_args = {"spline"};
80
81       end
82   end
83
84   pp = interp1 (t, y, interp_args{end}, 'pp');
85
86   if given.file
87     fullfname = fullfile (DIR,[fname ".m"]);
88     save("-binary",[fullfname(1:end-2) ".mat"],"pp");
89
90     bodystr = ["  persistent pp\n" ...
91                    "  if isempty(pp)\n" ...
92                    "    pp = load([mfilename()" ' ".mat"' "]).pp;\n"...
93                    "  end\n\n" ...
94                    "  z = ppval(pp, x);"];
95
96     strfunc = generate_function_str(fname, {"z"}, {"x"}, bodystr);
97
98     fid = fopen ( fullfile (DIR,[fname ".m"]), "w");
99     fprintf (fid, "%s", strfunc);
100     fclose (fid);
101
102     disp(["Function generated: " fullfname ]);
103     fhandle = eval(["@" fname]);
104
105   else
106     fullfname = "";
107     fhandle = @(t_) ppval (pp, t_);
108
109   end
110
111 endfunction
112
113 function str = generate_function_str(name, oargs, iargs, bodystr)
114
115   striargs = cell2mat ( cellfun (@(x) [x ", "], iargs, "UniformOutput", false));
116   striargs = striargs(1:end-2);
117
118   stroargs = cell2mat ( cellfun (@(x) [x ", "], oargs, "UniformOutput", false));
119   stroargs = stroargs(1:end-2);
120
121   if !isempty (stroargs)
122     str = ["function [" stroargs "] = " name "(" striargs ")\n\n" bodystr ...
123            "\n\nendfunction"];
124   else
125     str = ["function " name "(" striargs ")\n\n" bodystr ...
126            "\n\nendfunction"];
127   end
128
129 endfunction
130
131 %!shared t, y
132 %! t = linspace(0,1,10);
133 %! y = t.^2 - 2*t + 1;
134
135 %!test
136 %! fhandle = data2fun(t,y);
137 %! assert(y,fhandle(t));
138
139 %!test
140 %! [fhandle fname] = data2fun(t,y,"file","testdata2fun");
141 %! yt = testdata2fun(t);
142 %!
143 %! assert(y,yt);
144 %! assert(y,fhandle(t));
145 %!
146 %! delete(fname);
147 %! delete([fname(1:end-2) ".mat"]);
148
149 %!test
150 %! [fhandle fname] = data2fun(t,y,"file","");
151 %! yt = testdata2fun(t);
152 %!
153 %! assert(y,yt);
154 %! assert(y,fhandle(t));
155 %!
156 %! delete(fname);
157 %! delete([fname(1:end-2) ".mat"]);
158
159 %!test
160 %! [fhandle fname] = data2fun(t,y,"file","testdata2fun","interp","linear");
161 %! yt = testdata2fun(t);
162 %!
163 %! assert(y,yt);
164 %! assert(y,fhandle(t));
165 %!
166 %! delete(fname);
167 %! delete([fname(1:end-2) ".mat"]);