Home > NoiseTools > nt_demean.m

nt_demean

PURPOSE ^

[y,mn]=nt_demean(x,w) - remove weighted mean over cols

SYNOPSIS ^

function [x,mn]=nt_demean(x,w)

DESCRIPTION ^

[y,mn]=nt_demean(x,w) - remove weighted mean over cols
 
  w is optional

  if w is a vector with fewer samples than size(x,1), it is interpreted as
  a vector of indices to be set to 1, the others being set to 0.

 NoiseTools

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [x,mn]=nt_demean(x,w)
0002 %[y,mn]=nt_demean(x,w) - remove weighted mean over cols
0003 %
0004 %  w is optional
0005 %
0006 %  if w is a vector with fewer samples than size(x,1), it is interpreted as
0007 %  a vector of indices to be set to 1, the others being set to 0.
0008 %
0009 % NoiseTools
0010 
0011 if nargin<2; w=[]; end
0012 if nargin<1; error('!');end
0013 nt_greetings;
0014 
0015 if ~isempty(w) && numel(w)<size(x,1)
0016     w=w(:);
0017     % interpret w as array of indices to set to 1
0018     if min(w)<1 || max(w)>size(x,1); 
0019         error('w interpreted as indices but values are out of range');
0020     end
0021     ww=zeros(size(x,1),1);
0022     ww(w)=1;
0023     w=ww;
0024 end
0025 
0026 
0027 if size(w,3)~=size(x,3);
0028     if size(w,3)==1 && size(x,3)~=1;
0029         w=repmat(w,[1,1,size(x,3)]);
0030     else
0031         error('W should have same npages as X, or else 1');
0032     end
0033 end
0034 
0035 [m,n,o]=size(x);
0036 x=nt_unfold(x);
0037 
0038 if isempty(w);
0039     
0040     mn=mean(double(x),1);
0041     x=nt_vecadd(x,-mn);
0042     
0043 else
0044     
0045     w=nt_unfold(w);
0046     
0047     if size(w,1)~=size(x,1)
0048         error('X and W should have same nrows'); 
0049     end
0050     
0051     
0052     if size(w,2)==1;
0053         mn=sum(nt_vecmult(double(x),w),1) ./ (sum(w,1)+eps);
0054     elseif size(w,2)==n;
0055         mn=sum(x.*w) ./ (sum(w,1)+eps);
0056     else
0057         error('W should have same ncols as X, or else 1');
0058     end
0059 
0060     %y=bsxfun(@minus,x,mn);
0061     x=nt_vecadd(x,-mn);
0062     
0063 end
0064 
0065 x=nt_fold(x,m);

Generated on Mon 10-Nov-2014 14:40:42 by m2html © 2005