changeset 2601:aa6522fae7ef octave-forge

returns scaling coefficients iof pre-whitening; two-pass algorithm used to improve accuracy
author schloegl
date Fri, 06 Oct 2006 08:00:46 +0000
parents 7b052403a860
children 768b25797d18
files extra/NaN/inst/zscore.m
diffstat 1 files changed, 22 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/extra/NaN/inst/zscore.m	Thu Oct 05 16:25:11 2006 +0000
+++ b/extra/NaN/inst/zscore.m	Fri Oct 06 08:00:46 2006 +0000
@@ -1,21 +1,21 @@
-function i = zscore(i,DIM)
+function [i,v,m] = zscore(i,DIM)
 % ZSCORE removes the mean and normalizes the data 
-% to a variance of 1. 
+% to a variance of 1. Can be used for Pre-Whitening of the data, too. 
 %
-% z = zscore(x,DIM)
-%   calculates the z-score of x along dimension DIM
-%   it removes the 
+% [z,r,m] = zscore(x,DIM)
+%   z   z-score of x along dimension DIM
+%   r   is the inverse of the standard deviation
+%   m   is the mean of x
+%
+% The data x can be reconstrated with 
+%     x = z*diag(1./r) + repmat(m,size(z)./size(m))  
+%     z = x*diag(r) - repmat(m.*v,size(z)./size(m))  
 %
 % DIM	dimension
 %	1: STATS of columns
 %	2: STATS of rows
 %	default or []: first DIMENSION, with more than 1 element
 %
-% features:
-% - can deal with NaN's (missing values)
-% - dimension argument 
-% - compatible to Matlab and Octave
-%
 % see also: SUMSKIPNAN, MEAN, STD, DETREND
 %
 % REFERENCE(S):
@@ -43,14 +43,19 @@
 
 if any(size(i)==0); return; end;
 
-if nargin < 2,
-	[S,N,SSQ] = sumskipnan(i);		% sum
-else
-	[S,N,SSQ] = sumskipnan(i,DIM);		% sum
+if nargin<2
+        DIM=[]; 
+end
+if isempty(DIM), 
+        DIM=min(find(size(i)>1));
+        if isempty(DIM), DIM=1; end;
 end;
 
-M = S./N;
-i = i - repmat(M,size(i)./size(S));		% remove mean
-i = i.*repmat(sqrt((SSQ-real(S).*real(M)-imag(S).*imag(M)).\max(N-1,0)),size(i)./size(S));	 % normalize by STD
 
+% pre-whitening
+m = mean(i,DIM);
+i = i-repmat(m,size(i)./size(m));  % remove mean
+v = 1./sqrt(mean(i.^2,DIM));
+i = i.*repmat(v,size(i)./size(v)); % scale to var=1
 
+        
\ No newline at end of file