1 ## Copyright (C) 2009 Luiz Angelo Daros de Luca <luizluca@gmail.com>
4 ## This program is free software; you can redistribute it and/or modify it
5 ## under the terms of the GNU General Public License as published by
6 ## the Free Software Foundation; either version 2, or (at your option)
9 ## This program is distributed in the hope that it will be useful, but
10 ## WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ## General Public License for more details.
14 ## You should have received a copy of the GNU General Public License
15 ## along with this program; see the file COPYING. If not, see
16 ## <http://www.gnu.org/licenses/>.
19 ## @deftypefn {Function File} [@var{YY},@var{PS}] = mapstd (@var{XX},@var{ymean},@var{ystd})
20 ## Map values to mean 0 and standard derivation to 1.
23 ## [YY,PS] = mapstd(XX,ymean,ystd)
25 ## Apply the conversion and returns YY as (YY-ymean)/ystd.
27 ## [YY,PS] = mapstd(XX,FP)
29 ## Apply the conversion but using an struct to inform target mean/stddev.
30 ## This is the same of [YY,PS]=mapstd(XX,FP.ymean, FP.ystd).
32 ## YY = mapstd('apply',XX,PS)
34 ## Reapply the conversion based on a previous operation data.
35 ## PS stores the mean and stddev of the first XX used.
37 ## XX = mapstd('reverse',YY,PS)
39 ## Reverse a conversion of a previous applied operation.
41 ## dx_dy = mapstd('dx',XX,YY,PS)
43 ## Returns the derivative of Y with respect to X.
45 ## dx_dy = mapstd('dx',XX,[],PS)
47 ## Returns the derivative (less efficient).
49 ## name = mapstd('name');
51 ## Returns the name of this convesion process.
53 ## FP = mapstd('pdefaults');
55 ## Returns the default process parameters.
57 ## names = mapstd('pnames');
59 ## Returns the description of the process parameters.
61 ## mapstd('pcheck',FP);
63 ## Raises an error if FP has some inconsistent.
68 function [out1,out2]=mapstd(in1,in2,in3,in4)
70 # Map values to mean 0 and standard derivation to 1.
72 # [YY,PS] = mapstd(XX,ymean,ystd)
74 # Apply the conversion and returns YY as (YY-ymean)/ystd.
76 # [YY,PS] = mapstd(XX,FP)
78 # Apply the conversion but using an struct to inform target mean/stddev.
79 # This is the same of [YY,PS]=mapstd(XX,FP.ymean, FP.ystd).
81 # YY = mapstd('apply',XX,PS)
83 # Reapply the conversion based on a previous operation data.
84 # PS stores the mean and stddev of the first XX used.
86 # XX = mapstd('reverse',YY,PS)
88 # Reverse a conversion of a previous applied operation.
90 # dx_dy = mapstd('dx',XX,YY,PS)
92 # Returns the derivative of Y with respect to X.
94 # dx_dy = mapstd('dx',XX,[],PS)
96 # Returns the derivative (less efficient).
98 # name = mapstd('name');
100 # Returns the name of this convesion process.
102 # FP = mapstd('pdefaults');
104 # Returns the default process parameters.
106 # names = mapstd('pnames');
108 # Returns the description of the process parameters.
110 # mapstd('pcheck',FP);
112 # Raises an error if FP has some inconsistent.
116 error("Not enough arguments.")
128 error("Too many output arguments");
131 error("Too many input arguments");
133 out1="Map Mean and Standard Deviation";
137 error("Too many output arguments");
140 error("Too many input arguments");
145 error("Too many output arguments");
148 error("Not enough input arguments");
151 error("Too many input arguments");
156 error("FP must be a struct")
157 elseif ~isfield(fp,"ymean")
158 error("FP must include ymean field")
159 elseif ~isfield(fp,"ystd")
160 error("FP must include ystd field")
161 elseif isdouble(fp.ymean)
162 error("FP.ymean must be a real scalar value")
163 elseif isdouble(fp.ystd)
164 error("FP.ystd must be a real scalar value")
169 # MATLAB uses pnames but documents as pdesc (that does not work)
172 error("Too many output arguments");
175 error("Too many input arguments");
177 # MATLAB seems to be buggy in the second element
178 #out1={'Mean value for each row of Y.','Maximum value for each
180 out1={"Mean value for each row of Y.","Standart deviation value for each row of Y."};
183 error("Not enough input arguments");
186 error("Too many input arguments");
189 error("Too many output arguments");
199 error("Not enough input arguments");
202 error("Too many input arguments");
205 error("Too many output arguments");
215 error("Not enough input arguments");
218 error("Too many input arguments");
221 error("Too many output arguments");
226 xx_yy=derivate(xx,yy,ps);
250 error("Too many input arguments");
258 function checkargs(values,ps)
260 if ~isnumeric(values)
261 error("Just numeric values are accepted")
265 error("PS should be a struct")
268 if ~isa(ps.ymean,"double")
269 error("PS.ymean should be a double")
271 if ~isa(ps.ystd,"double")
272 error("PS.ystd should be a double")
274 if ~all(size(ps.ymean)==[1 1])
275 error("PS.ymean should be a scalar")
277 if ~all(size(ps.ystd)==[1 1])
278 error("PS.ystd should be a scalar")
281 if ~isnumeric(ps.xmean)
282 error("PS.xmean should be a numeric")
284 if ~isnumeric(ps.xstd)
285 error("PS.xstd should be a numeric")
287 if ~all(size(ps.xmean)==size(ps.xstd))
288 error("Size of PS.xmean and PS.xstd must match")
292 # Apply the mapping operation
293 function [yy]=apply(xx,ps)
296 if ~all(size(xx,1)==size(ps.xmean,1))
297 error("Size of XX rows should match PS.xmean and PS.xstd")
299 # Avoid multiply/division by zero
300 ps.xstd(ps.xstd == 0) = 1;
301 yy=(xx - (ps.xmean*ones(1,size(xx,2)))) ./ (ps.xstd*ones(1,size(xx,2)));
302 yy=(yy + ps.ymean) .* ps.ystd;
305 # Reverse the mapping operation
306 function [xx]=reverse(yy,ps)
308 if ~all(size(yy,1)==size(ps.xmean,1))
309 error("Size of YY rows should match PS.xmean and PS.xstd")
311 # Avoid multiply/division by zero
312 ps.xstd(ps.xstd == 0) = 1;
313 yy=(yy ./ ps.ystd) - ps.ymean;
314 xx=(yy .* (ps.xstd*ones(1,size(yy,2)))) + (ps.xmean*ones(1,size(yy,2)));
317 # I don't know why this exists but matlab implements it
318 function [dy_dx]=derivate(xx,yy,ps)
323 diagonal = diag(ps.ystd ./ ps.xstd);
324 dy_dx = diagonal(:,:,ones(1,cols));