Home > NoiseTools > nt_normcol.m

nt_normcol

PURPOSE ^

[y,norm]=nt_normcol(x,w) - normalize each column so its weighted msq is 1

SYNOPSIS ^

function [y,norm]=nt_normcol(x,w)

DESCRIPTION ^

 [y,norm]=nt_normcol(x,w) - normalize each column so its weighted msq is 1
 
   y: normalized data
   norm: vector of norms

   x: data to normalize
   w: weight
 
 If x is 3D, pages are concatenated vertically before calculating the
 norm. If x is 4D, apply normcol to each book.
 
 Weight should be either a column vector, or a matrix (2D or 3D) of same
 size as data.

 See nt_normrow, nt_normpage, nt_normpagecol.

 NoiseTools

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [y,norm]=nt_normcol(x,w)
0002 % [y,norm]=nt_normcol(x,w) - normalize each column so its weighted msq is 1
0003 %
0004 %   y: normalized data
0005 %   norm: vector of norms
0006 %
0007 %   x: data to normalize
0008 %   w: weight
0009 %
0010 % If x is 3D, pages are concatenated vertically before calculating the
0011 % norm. If x is 4D, apply normcol to each book.
0012 %
0013 % Weight should be either a column vector, or a matrix (2D or 3D) of same
0014 % size as data.
0015 %
0016 % See nt_normrow, nt_normpage, nt_normpagecol.
0017 %
0018 % NoiseTools
0019 
0020 if nargin<2; w=[]; end
0021 
0022 if isempty(x); error('empty x'); end
0023 
0024 if ndims(x)==4;
0025     if nargin>1; error('weights not supported for 4D data'); end
0026     [m,n,o,p]=size(x);
0027     y=zeros(size(x));
0028     N=zeros(1,n);
0029     for k=1:p
0030         [y(:,:,:,k),NN]=nt_normcol(x(:,:,:,k));
0031         N=N+NN.^2;
0032     end
0033     return
0034 end
0035 
0036 if ndims(x)==3;
0037     
0038     % 3D: unfold, apply normcol on 2D, fold
0039     [m,n,o]=size(x);
0040     x=nt_unfold(x);
0041     if isempty(w);
0042         % no weight
0043         [y,NN]=nt_normcol(x);
0044         N=NN.^2;
0045         y=nt_fold(y,m);
0046     else
0047         % weight
0048         if size(w,1)~=m; error('weight matrix should have same nrows as data'); end 
0049         if ndims(w)==2 && size(w,2)==1; 
0050             w=repmat(w,[1,m,o]);
0051         end
0052         if size(w)~=size(w); error('weight should have same size as data'); end
0053         w=nt_unfold(w);
0054         [y,NN]=nt_normcol(x,w);
0055         N=NN.^2;
0056         y=nt_fold(y,m);
0057     end
0058 
0059 else
0060     
0061     % 2D
0062     [m,n]=size(x);
0063     if isempty(w)
0064 
0065         % no weight
0066         %N=sqrt(sum(x.^2)/m);
0067         %y=vecmult(x,1./N);
0068         N=(sum(x.^2)/m);
0069         NN=N.^-0.5;
0070         NN(find(N==0))=0;
0071         y=nt_vecmult(x,NN);
0072        
0073     else
0074 
0075         % weight
0076         if size(w,1)~=size(x,1); error('weight matrix should have same ncols as data'); end 
0077         if ndims(w)==2 && size(w,2)==1; 
0078             w=repmat(w,1,n);
0079         end
0080         if size(w)~=size(w); error('weight should have same size as data'); end
0081         if size(w,2)==1; w=repmat(w,1,n);end
0082         %N=sqrt(sum((x.^2).*w)./sum(w));
0083         %y=vecmult(x,1./N);
0084         N=(sum((x.^2).*w)./sum(w));
0085         NN=N.^-0.5;
0086         NN(find(N==0))=0;
0087         y=nt_vecmult(x, NN);
0088         
0089     end
0090 end
0091 
0092 norm=N.^0.5;

Generated on Tue 09-Oct-2018 10:58:04 by m2html © 2005