annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_norm_variable.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [x,sNorm] = som_norm_variable(x, method, operation)
Daniel@0 2
Daniel@0 3 %SOM_NORM_VARIABLE Normalize or denormalize a scalar variable.
Daniel@0 4 %
Daniel@0 5 % [x,sNorm] = som_norm_variable(x, method, operation)
Daniel@0 6 %
Daniel@0 7 % xnew = som_norm_variable(x,'var','do');
Daniel@0 8 % [dummy,sN] = som_norm_variable(x,'log','init');
Daniel@0 9 % [xnew,sN] = som_norm_variable(x,sN,'do');
Daniel@0 10 % xorig = som_norm_variable(xnew,sN,'undo');
Daniel@0 11 %
Daniel@0 12 % Input and output arguments:
Daniel@0 13 % x (vector) a set of values of a scalar variable for
Daniel@0 14 % which the (de)normalization is performed.
Daniel@0 15 % The processed values are returned.
Daniel@0 16 % method (string) identifier for a normalization method: 'var',
Daniel@0 17 % 'range', 'log', 'logistic', 'histD', or 'histC'.
Daniel@0 18 % A normalization struct with default values is created.
Daniel@0 19 % (struct) normalization struct, or an array of such
Daniel@0 20 % (cellstr) first string gives normalization operation, and the
Daniel@0 21 % second gives denormalization operation, with x
Daniel@0 22 % representing the variable, for example:
Daniel@0 23 % {'x+2','x-2}, or {'exp(-x)','-log(x)'} or {'round(x)'}.
Daniel@0 24 % Note that in the last case, no denorm operation is
Daniel@0 25 % defined.
Daniel@0 26 % operation (string) the operation to be performed: 'init', 'do' or 'undo'
Daniel@0 27 %
Daniel@0 28 % sNorm (struct) updated normalization struct/struct array
Daniel@0 29 %
Daniel@0 30 % For more help, try 'type som_norm_variable' or check out online documentation.
Daniel@0 31 % See also SOM_NORMALIZE, SOM_DENORMALIZE.
Daniel@0 32
Daniel@0 33 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 34 %
Daniel@0 35 % som_norm_variable
Daniel@0 36 %
Daniel@0 37 % PURPOSE
Daniel@0 38 %
Daniel@0 39 % Initialize, apply and undo normalizations on a given vector of
Daniel@0 40 % scalar values.
Daniel@0 41 %
Daniel@0 42 % SYNTAX
Daniel@0 43 %
Daniel@0 44 % xnew = som_norm_variable(x,method,operation)
Daniel@0 45 % xnew = som_norm_variable(x,sNorm,operation)
Daniel@0 46 % [xnew,sNorm] = som_norm_variable(...)
Daniel@0 47 %
Daniel@0 48 % DESCRIPTION
Daniel@0 49 %
Daniel@0 50 % This function is used to initialize, apply and undo normalizations
Daniel@0 51 % on scalar variables. It is the low-level function that upper-level
Daniel@0 52 % functions SOM_NORMALIZE and SOM_DENORMALIZE utilize to actually (un)do
Daniel@0 53 % the normalizations.
Daniel@0 54 %
Daniel@0 55 % Normalizations are typically performed to control the variance of
Daniel@0 56 % vector components. If some vector components have variance which is
Daniel@0 57 % significantly higher than the variance of other components, those
Daniel@0 58 % components will dominate the map organization. Normalization of
Daniel@0 59 % the variance of vector components (method 'var') is used to prevent
Daniel@0 60 % that. In addition to variance normalization, other methods have
Daniel@0 61 % been implemented as well (see list below).
Daniel@0 62 %
Daniel@0 63 % Usually normalizations convert the variable values so that they no
Daniel@0 64 % longer make any sense: the values are still ordered, but their range
Daniel@0 65 % may have changed so radically that interpreting the numbers in the
Daniel@0 66 % original context is very hard. For this reason all implemented methods
Daniel@0 67 % are (more or less) revertible. The normalizations are monotonic
Daniel@0 68 % and information is saved so that they can be undone. Also, the saved
Daniel@0 69 % information makes it possible to apply the EXACTLY SAME normalization
Daniel@0 70 % to another set of values. The normalization information is determined
Daniel@0 71 % with 'init' operation, while 'do' and 'undo' operations are used to
Daniel@0 72 % apply or revert the normalization.
Daniel@0 73 %
Daniel@0 74 % The normalization information is saved in a normalization struct,
Daniel@0 75 % which is returned as the second argument of this function. Note that
Daniel@0 76 % normalization operations may be stacked. In this case, normalization
Daniel@0 77 % structs are positioned in a struct array. When applied, the array is
Daniel@0 78 % gone through from start to end, and when undone, in reverse order.
Daniel@0 79 %
Daniel@0 80 % method description
Daniel@0 81 %
Daniel@0 82 % 'var' Variance normalization. A linear transformation which
Daniel@0 83 % scales the values such that their variance=1. This is
Daniel@0 84 % convenient way to use Mahalanobis distance measure without
Daniel@0 85 % actually changing the distance calculation procedure.
Daniel@0 86 %
Daniel@0 87 % 'range' Normalization of range of values. A linear transformation
Daniel@0 88 % which scales the values between [0,1].
Daniel@0 89 %
Daniel@0 90 % 'log' Logarithmic normalization. In many cases the values of
Daniel@0 91 % a vector component are exponentially distributed. This
Daniel@0 92 % normalization is a good way to get more resolution to
Daniel@0 93 % (the low end of) that vector component. What this
Daniel@0 94 % actually does is a non-linear transformation:
Daniel@0 95 % x_new = log(x_old - m + 1)
Daniel@0 96 % where m=min(x_old) and log is the natural logarithm.
Daniel@0 97 % Applying the transformation to a value which is lower
Daniel@0 98 % than m-1 will give problems, as the result is then complex.
Daniel@0 99 % If the minimum for values is known a priori,
Daniel@0 100 % it might be a good idea to initialize the normalization with
Daniel@0 101 % [dummy,sN] = som_norm_variable(minimum,'log','init');
Daniel@0 102 % and normalize only after this:
Daniel@0 103 % x_new = som_norm_variable(x,sN,'do');
Daniel@0 104 %
Daniel@0 105 % 'logistic' or softmax normalization. This normalization ensures
Daniel@0 106 % that all values in the future, too, are within the range
Daniel@0 107 % [0,1]. The transformation is more-or-less linear in the
Daniel@0 108 % middle range (around mean value), and has a smooth
Daniel@0 109 % nonlinearity at both ends which ensures that all values
Daniel@0 110 % are within the range. The data is first scaled as in
Daniel@0 111 % variance normalization:
Daniel@0 112 % x_scaled = (x_old - mean(x_old))/std(x_old)
Daniel@0 113 % and then transformed with the logistic function
Daniel@0 114 % x_new = 1/(1+exp(-x_scaled))
Daniel@0 115 %
Daniel@0 116 % 'histD' Discrete histogram equalization. Non-linear. Orders the
Daniel@0 117 % values and replaces each value by its ordinal number.
Daniel@0 118 % Finally, scales the values such that they are between [0,1].
Daniel@0 119 % Useful for both discrete and continuous variables, but as
Daniel@0 120 % the saved normalization information consists of all
Daniel@0 121 % unique values of the initialization data set, it may use
Daniel@0 122 % considerable amounts of memory. If the variable can get
Daniel@0 123 % more than a few values (say, 20), it might be better to
Daniel@0 124 % use 'histC' method below. Another important note is that
Daniel@0 125 % this method is not exactly revertible if it is applied
Daniel@0 126 % to values which are not part of the original value set.
Daniel@0 127 %
Daniel@0 128 % 'histC' Continuous histogram equalization. Actually, a partially
Daniel@0 129 % linear transformation which tries to do something like
Daniel@0 130 % histogram equalization. The value range is divided to
Daniel@0 131 % a number of bins such that the number of values in each
Daniel@0 132 % bin is (almost) the same. The values are transformed
Daniel@0 133 % linearly in each bin. For example, values in bin number 3
Daniel@0 134 % are scaled between [3,4[. Finally, all values are scaled
Daniel@0 135 % between [0,1]. The number of bins is the square root
Daniel@0 136 % of the number of unique values in the initialization set,
Daniel@0 137 % rounded up. The resulting histogram equalization is not
Daniel@0 138 % as good as the one that 'histD' makes, but the benefit
Daniel@0 139 % is that it is exactly revertible - even outside the
Daniel@0 140 % original value range (although the results may be funny).
Daniel@0 141 %
Daniel@0 142 % 'eval' With this method, freeform normalization operations can be
Daniel@0 143 % specified. The parameter field contains strings to be
Daniel@0 144 % evaluated with 'eval' function, with variable name 'x'
Daniel@0 145 % representing the variable itself. The first string is
Daniel@0 146 % the normalization operation, and the second is a
Daniel@0 147 % denormalization operation. If the denormalization operation
Daniel@0 148 % is empty, it is ignored.
Daniel@0 149 %
Daniel@0 150 % INPUT ARGUMENTS
Daniel@0 151 %
Daniel@0 152 % x (vector) The scalar values to which the normalization
Daniel@0 153 % operation is applied.
Daniel@0 154 %
Daniel@0 155 % method The normalization specification.
Daniel@0 156 % (string) Identifier for a normalization method: 'var',
Daniel@0 157 % 'range', 'log', 'logistic', 'histD' or 'histC'.
Daniel@0 158 % Corresponding default normalization struct is created.
Daniel@0 159 % (struct) normalization struct
Daniel@0 160 % (struct array) of normalization structs, applied to
Daniel@0 161 % x one after the other
Daniel@0 162 % (cellstr) of length
Daniel@0 163 % (cellstr array) first string gives normalization operation, and
Daniel@0 164 % the second gives denormalization operation, with x
Daniel@0 165 % representing the variable, for example:
Daniel@0 166 % {'x+2','x-2}, or {'exp(-x)','-log(x)'} or {'round(x)'}.
Daniel@0 167 % Note that in the last case, no denorm operation is
Daniel@0 168 % defined.
Daniel@0 169 %
Daniel@0 170 % note: if the method is given as struct(s), it is
Daniel@0 171 % applied (done or undone, as specified by operation)
Daniel@0 172 % regardless of what the value of '.status' field
Daniel@0 173 % is in the struct(s). Only if the status is
Daniel@0 174 % 'uninit', the undoing operation is halted.
Daniel@0 175 % Anyhow, the '.status' fields in the returned
Daniel@0 176 % normalization struct(s) is set to approriate value.
Daniel@0 177 %
Daniel@0 178 % operation (string) The operation to perform: 'init' to initialize
Daniel@0 179 % the normalization struct, 'do' to perform the
Daniel@0 180 % normalization, 'undo' to undo the normalization,
Daniel@0 181 % if possible. If operation 'do' is given, but the
Daniel@0 182 % normalization struct has not yet been initialized,
Daniel@0 183 % it is initialized using the given data (x).
Daniel@0 184 %
Daniel@0 185 % OUTPUT ARGUMENTS
Daniel@0 186 %
Daniel@0 187 % x (vector) Appropriately processed values.
Daniel@0 188 %
Daniel@0 189 % sNorm (struct) Updated normalization struct/struct array. If any,
Daniel@0 190 % the '.status' and '.params' fields are updated.
Daniel@0 191 %
Daniel@0 192 % EXAMPLES
Daniel@0 193 %
Daniel@0 194 % To initialize and apply a normalization on a set of scalar values:
Daniel@0 195 %
Daniel@0 196 % [x_new,sN] = som_norm_variable(x_old,'var','do');
Daniel@0 197 %
Daniel@0 198 % To just initialize, use:
Daniel@0 199 %
Daniel@0 200 % [dummy,sN] = som_norm_variable(x_old,'var','init');
Daniel@0 201 %
Daniel@0 202 % To undo the normalization(s):
Daniel@0 203 %
Daniel@0 204 % x_orig = som_norm_variable(x_new,sN,'undo');
Daniel@0 205 %
Daniel@0 206 % Typically, normalizations of data structs/sets are handled using
Daniel@0 207 % functions SOM_NORMALIZE and SOM_DENORMALIZE. However, when only the
Daniel@0 208 % values of a single variable are of interest, SOM_NORM_VARIABLE may
Daniel@0 209 % be useful. For example, assume one wants to apply the normalization
Daniel@0 210 % done on a component (i) of a data struct (sD) to a new set of values
Daniel@0 211 % (x) of that component. With SOM_NORM_VARIABLE this can be done with:
Daniel@0 212 %
Daniel@0 213 % x_new = som_norm_variable(x,sD.comp_norm{i},'do');
Daniel@0 214 %
Daniel@0 215 % Now, as the normalizations in sD.comp_norm{i} have already been
Daniel@0 216 % initialized with the original data set (presumably sD.data),
Daniel@0 217 % the EXACTLY SAME normalization(s) can be applied to the new values.
Daniel@0 218 % The same thing can be done with SOM_NORMALIZE function, too:
Daniel@0 219 %
Daniel@0 220 % x_new = som_normalize(x,sD.comp_norm{i});
Daniel@0 221 %
Daniel@0 222 % Or, if the new data set were in variable D - a matrix of same
Daniel@0 223 % dimension as the original data set:
Daniel@0 224 %
Daniel@0 225 % D_new = som_normalize(D,sD,i);
Daniel@0 226 %
Daniel@0 227 % SEE ALSO
Daniel@0 228 %
Daniel@0 229 % som_normalize Add/apply/redo normalizations for a data struct/set.
Daniel@0 230 % som_denormalize Undo normalizations of a data struct/set.
Daniel@0 231
Daniel@0 232 % Copyright (c) 1998-2000 by the SOM toolbox programming team.
Daniel@0 233 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 234
Daniel@0 235 % Version 2.0beta juuso 151199 170400 150500
Daniel@0 236
Daniel@0 237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 238 %% check arguments
Daniel@0 239
Daniel@0 240 error(nargchk(3, 3, nargin)); % check no. of input arguments is correct
Daniel@0 241
Daniel@0 242 % method
Daniel@0 243 sNorm = [];
Daniel@0 244 if ischar(method)
Daniel@0 245 if any(strcmp(method,{'var','range','log','logistic','histD','histC'})),
Daniel@0 246 sNorm = som_set('som_norm','method',method);
Daniel@0 247 else
Daniel@0 248 method = cellstr(method);
Daniel@0 249 end
Daniel@0 250 end
Daniel@0 251 if iscell(method),
Daniel@0 252 if length(method)==1 & isstruct(method{1}), sNorm = method{1};
Daniel@0 253 else
Daniel@0 254 if length(method)==1 | isempty(method{2}), method{2} = 'x'; end
Daniel@0 255 sNorm = som_set('som_norm','method','eval','params',method);
Daniel@0 256 end
Daniel@0 257 else
Daniel@0 258 sNorm = method;
Daniel@0 259 end
Daniel@0 260
Daniel@0 261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 262 %% action
Daniel@0 263
Daniel@0 264 order = [1:length(sNorm)];
Daniel@0 265 if length(order)>1 & strcmp(operation,'undo'), order = order(end:-1:1); end
Daniel@0 266
Daniel@0 267 for i=order,
Daniel@0 268
Daniel@0 269 % initialize
Daniel@0 270 if strcmp(operation,'init') | ...
Daniel@0 271 (strcmp(operation,'do') & strcmp(sNorm(i).status,'uninit')),
Daniel@0 272
Daniel@0 273 % case method = 'hist'
Daniel@0 274 if strcmp(sNorm(i).method,'hist'),
Daniel@0 275 inds = find(~isnan(x) & ~isinf(x));
Daniel@0 276 if length(unique(x(inds)))>20, sNorm(i).method = 'histC';
Daniel@0 277 else sNorm{i}.method = 'histD'; end
Daniel@0 278 end
Daniel@0 279
Daniel@0 280 switch(sNorm(i).method),
Daniel@0 281 case 'var', params = norm_variance_init(x);
Daniel@0 282 case 'range', params = norm_scale01_init(x);
Daniel@0 283 case 'log', params = norm_log_init(x);
Daniel@0 284 case 'logistic', params = norm_logistic_init(x);
Daniel@0 285 case 'histD', params = norm_histeqD_init(x);
Daniel@0 286 case 'histC', params = norm_histeqC_init(x);
Daniel@0 287 case 'eval', params = sNorm(i).params;
Daniel@0 288 otherwise,
Daniel@0 289 error(['Unrecognized method: ' sNorm(i).method]);
Daniel@0 290 end
Daniel@0 291 sNorm(i).params = params;
Daniel@0 292 sNorm(i).status = 'undone';
Daniel@0 293 end
Daniel@0 294
Daniel@0 295 % do / undo
Daniel@0 296 if strcmp(operation,'do'),
Daniel@0 297 switch(sNorm(i).method),
Daniel@0 298 case 'var', x = norm_scale_do(x,sNorm(i).params);
Daniel@0 299 case 'range', x = norm_scale_do(x,sNorm(i).params);
Daniel@0 300 case 'log', x = norm_log_do(x,sNorm(i).params);
Daniel@0 301 case 'logistic', x = norm_logistic_do(x,sNorm(i).params);
Daniel@0 302 case 'histD', x = norm_histeqD_do(x,sNorm(i).params);
Daniel@0 303 case 'histC', x = norm_histeqC_do(x,sNorm(i).params);
Daniel@0 304 case 'eval', x = norm_eval_do(x,sNorm(i).params);
Daniel@0 305 otherwise,
Daniel@0 306 error(['Unrecognized method: ' sNorm(i).method]);
Daniel@0 307 end
Daniel@0 308 sNorm(i).status = 'done';
Daniel@0 309
Daniel@0 310 elseif strcmp(operation,'undo'),
Daniel@0 311
Daniel@0 312 if strcmp(sNorm(i).status,'uninit'),
Daniel@0 313 warning('Could not undo: uninitialized normalization struct.')
Daniel@0 314 break;
Daniel@0 315 end
Daniel@0 316 switch(sNorm(i).method),
Daniel@0 317 case 'var', x = norm_scale_undo(x,sNorm(i).params);
Daniel@0 318 case 'range', x = norm_scale_undo(x,sNorm(i).params);
Daniel@0 319 case 'log', x = norm_log_undo(x,sNorm(i).params);
Daniel@0 320 case 'logistic', x = norm_logistic_undo(x,sNorm(i).params);
Daniel@0 321 case 'histD', x = norm_histeqD_undo(x,sNorm(i).params);
Daniel@0 322 case 'histC', x = norm_histeqC_undo(x,sNorm(i).params);
Daniel@0 323 case 'eval', x = norm_eval_undo(x,sNorm(i).params);
Daniel@0 324 otherwise,
Daniel@0 325 error(['Unrecognized method: ' sNorm(i).method]);
Daniel@0 326 end
Daniel@0 327 sNorm(i).status = 'undone';
Daniel@0 328
Daniel@0 329 elseif ~strcmp(operation,'init'),
Daniel@0 330
Daniel@0 331 error(['Unrecognized operation: ' operation])
Daniel@0 332
Daniel@0 333 end
Daniel@0 334 end
Daniel@0 335
Daniel@0 336 return;
Daniel@0 337
Daniel@0 338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 339 %% subfunctions
Daniel@0 340
Daniel@0 341 % linear scaling
Daniel@0 342
Daniel@0 343 function p = norm_variance_init(x)
Daniel@0 344 inds = find(~isnan(x) & isfinite(x));
Daniel@0 345 p = [mean(x(inds)), std(x(inds))];
Daniel@0 346 if p(2) == 0, p(2) = 1; end
Daniel@0 347 %end of norm_variance_init
Daniel@0 348
Daniel@0 349 function p = norm_scale01_init(x)
Daniel@0 350 inds = find(~isnan(x) & isfinite(x));
Daniel@0 351 mi = min(x(inds));
Daniel@0 352 ma = max(x(inds));
Daniel@0 353 if mi == ma, p = [mi, 1]; else p = [mi, ma-mi]; end
Daniel@0 354 %end of norm_scale01_init
Daniel@0 355
Daniel@0 356 function x = norm_scale_do(x,p)
Daniel@0 357 x = (x - p(1)) / p(2);
Daniel@0 358 % end of norm_scale_do
Daniel@0 359
Daniel@0 360 function x = norm_scale_undo(x,p)
Daniel@0 361 x = x * p(2) + p(1);
Daniel@0 362 % end of norm_scale_undo
Daniel@0 363
Daniel@0 364 % logarithm
Daniel@0 365
Daniel@0 366 function p = norm_log_init(x)
Daniel@0 367 inds = find(~isnan(x) & isfinite(x));
Daniel@0 368 p = min(x(inds));
Daniel@0 369 % end of norm_log_init
Daniel@0 370
Daniel@0 371 function x = norm_log_do(x,p)
Daniel@0 372 x = log(x - p +1);
Daniel@0 373 % if any(~isreal(x)), ok = 0; end
Daniel@0 374 % end of norm_log_do
Daniel@0 375
Daniel@0 376 function x = norm_log_undo(x,p)
Daniel@0 377 x = exp(x) -1 + p;
Daniel@0 378 % end of norm_log_undo
Daniel@0 379
Daniel@0 380 % logistic
Daniel@0 381
Daniel@0 382 function p = norm_logistic_init(x)
Daniel@0 383 inds = find(~isnan(x) & isfinite(x));
Daniel@0 384 p = [mean(x(inds)), std(x(inds))];
Daniel@0 385 if p(2)==0, p(2) = 1; end
Daniel@0 386 % end of norm_logistic_init
Daniel@0 387
Daniel@0 388 function x = norm_logistic_do(x,p)
Daniel@0 389 x = (x-p(1))/p(2);
Daniel@0 390 x = 1./(1+exp(-x));
Daniel@0 391 % end of norm_logistic_do
Daniel@0 392
Daniel@0 393 function x = norm_logistic_undo(x,p)
Daniel@0 394 x = log(x./(1-x));
Daniel@0 395 x = x*p(2)+p(1);
Daniel@0 396 % end of norm_logistic_undo
Daniel@0 397
Daniel@0 398 % histogram equalization for discrete values
Daniel@0 399
Daniel@0 400 function p = norm_histeqD_init(x)
Daniel@0 401 inds = find(~isnan(x) & ~isinf(x));
Daniel@0 402 p = unique(x(inds));
Daniel@0 403 % end of norm_histeqD_init
Daniel@0 404
Daniel@0 405 function x = norm_histeqD_do(x,p)
Daniel@0 406 bins = length(p);
Daniel@0 407 inds = find(~isnan(x) & ~isinf(x))';
Daniel@0 408 for i = inds,
Daniel@0 409 [dummy ind] = min(abs(x(i) - p));
Daniel@0 410 % data item closer to the left-hand bin wall is indexed after RH wall
Daniel@0 411 if x(i) > p(ind) & ind < bins,
Daniel@0 412 x(i) = ind + 1;
Daniel@0 413 else
Daniel@0 414 x(i) = ind;
Daniel@0 415 end
Daniel@0 416 end
Daniel@0 417 x = (x-1)/(bins-1); % normalization between [0,1]
Daniel@0 418 % end of norm_histeqD_do
Daniel@0 419
Daniel@0 420 function x = norm_histeqD_undo(x,p)
Daniel@0 421 bins = length(p);
Daniel@0 422 x = round(x*(bins-1)+1);
Daniel@0 423 inds = find(~isnan(x) & ~isinf(x));
Daniel@0 424 x(inds) = p(x(inds));
Daniel@0 425 % end of norm_histeqD_undo
Daniel@0 426
Daniel@0 427 % histogram equalization with partially linear functions
Daniel@0 428
Daniel@0 429 function p = norm_histeqC_init(x)
Daniel@0 430 % investigate x
Daniel@0 431 inds = find(~isnan(x) & ~isinf(x));
Daniel@0 432 samples = length(inds);
Daniel@0 433 xs = unique(x(inds));
Daniel@0 434 mi = xs(1);
Daniel@0 435 ma = xs(end);
Daniel@0 436 % decide number of limits
Daniel@0 437 lims = ceil(sqrt(length(xs))); % 2->2,100->10,1000->32,10000->100
Daniel@0 438 % decide limits
Daniel@0 439 if lims==1,
Daniel@0 440 p = [mi, mi+1];
Daniel@0 441 lims = 2;
Daniel@0 442 elseif lims==2,
Daniel@0 443 p = [mi, ma];
Daniel@0 444 else
Daniel@0 445 p = zeros(lims,1);
Daniel@0 446 p(1) = mi;
Daniel@0 447 p(end) = ma;
Daniel@0 448 binsize = zeros(lims-1,1); b = 1; avebinsize = samples/(lims-1);
Daniel@0 449 for i=1:(length(xs)-1),
Daniel@0 450 binsize(b) = binsize(b) + sum(x==xs(i));
Daniel@0 451 if binsize(b) >= avebinsize,
Daniel@0 452 b = b + 1;
Daniel@0 453 p(b) = (xs(i)+xs(i+1))/2;
Daniel@0 454 end
Daniel@0 455 if b==(lims-1),
Daniel@0 456 binsize(b) = samples-sum(binsize); break;
Daniel@0 457 else
Daniel@0 458 avebinsize = (samples-sum(binsize))/(lims-1-b);
Daniel@0 459 end
Daniel@0 460 end
Daniel@0 461 end
Daniel@0 462 % end of norm_histeqC_init
Daniel@0 463
Daniel@0 464 function x = norm_histeqC_do(x,p)
Daniel@0 465 xnew = x;
Daniel@0 466 lims = length(p);
Daniel@0 467 % handle values below minimum
Daniel@0 468 r = p(2)-p(1);
Daniel@0 469 inds = find(x<=p(1) & isfinite(x));
Daniel@0 470 if any(inds), xnew(inds) = 0-(p(1)-x(inds))/r; end
Daniel@0 471 % handle values above maximum
Daniel@0 472 r = p(end)-p(end-1);
Daniel@0 473 inds = find(x>p(end) & isfinite(x));
Daniel@0 474 if any(inds), xnew(inds) = lims-1+(x(inds)-p(end))/r; end
Daniel@0 475 % handle all other values
Daniel@0 476 for i=1:(lims-1),
Daniel@0 477 r0 = p(i); r1 = p(i+1); r = r1-r0;
Daniel@0 478 inds = find(x>r0 & x<=r1);
Daniel@0 479 if any(inds), xnew(inds) = i-1+(x(inds)-r0)/r; end
Daniel@0 480 end
Daniel@0 481 % scale so that minimum and maximum correspond to 0 and 1
Daniel@0 482 x = xnew/(lims-1);
Daniel@0 483 % end of norm_histeqC_do
Daniel@0 484
Daniel@0 485 function x = norm_histeqC_undo(x,p)
Daniel@0 486 xnew = x;
Daniel@0 487 lims = length(p);
Daniel@0 488 % scale so that 0 and 1 correspond to minimum and maximum
Daniel@0 489 x = x*(lims-1);
Daniel@0 490
Daniel@0 491 % handle values below minimum
Daniel@0 492 r = p(2)-p(1);
Daniel@0 493 inds = find(x<=0 & isfinite(x));
Daniel@0 494 if any(inds), xnew(inds) = x(inds)*r + p(1); end
Daniel@0 495 % handle values above maximum
Daniel@0 496 r = p(end)-p(end-1);
Daniel@0 497 inds = find(x>lims-1 & isfinite(x));
Daniel@0 498 if any(inds), xnew(inds) = (x(inds)-(lims-1))*r+p(end); end
Daniel@0 499 % handle all other values
Daniel@0 500 for i=1:(lims-1),
Daniel@0 501 r0 = p(i); r1 = p(i+1); r = r1-r0;
Daniel@0 502 inds = find(x>i-1 & x<=i);
Daniel@0 503 if any(inds), xnew(inds) = (x(inds)-(i-1))*r + r0; end
Daniel@0 504 end
Daniel@0 505 x = xnew;
Daniel@0 506 % end of norm_histeqC_undo
Daniel@0 507
Daniel@0 508 % eval
Daniel@0 509
Daniel@0 510 function p = norm_eval_init(method)
Daniel@0 511 p = method;
Daniel@0 512 %end of norm_eval_init
Daniel@0 513
Daniel@0 514 function x = norm_eval_do(x,p)
Daniel@0 515 x_tmp = eval(p{1});
Daniel@0 516 if size(x_tmp,1)>=1 & size(x,1)>=1 & ...
Daniel@0 517 size(x_tmp,2)==1 & size(x,2)==1,
Daniel@0 518 x = x_tmp;
Daniel@0 519 end
Daniel@0 520 %end of norm_eval_do
Daniel@0 521
Daniel@0 522 function x = norm_eval_undo(x,p)
Daniel@0 523 x_tmp = eval(p{2});
Daniel@0 524 if size(x_tmp,1)>=1 & size(x,1)>=1 & ...
Daniel@0 525 size(x_tmp,2)==1 & size(x,2)==1,
Daniel@0 526 x = x_tmp;
Daniel@0 527 end
Daniel@0 528 %end of norm_eval_undo
Daniel@0 529
Daniel@0 530 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 531
Daniel@0 532
Daniel@0 533