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.

 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 % NoiseTools
0017 
0018 if nargin<2; w=[]; end
0019 
0020 if isempty(x); error('empty x'); end
0021 
0022 if ndims(x)==4;
0023     if nargin>1; error('weights not supported for 4D data'); end
0024     [m,n,o,p]=size(x);
0025     y=zeros(size(x));
0026     N=zeros(1,n);
0027     for k=1:p
0028         [y(:,:,:,k),NN]=nt_normcol(x(:,:,:,k));
0029         N=N+NN.^2;
0030     end
0031     return
0032 end
0033 
0034 if ndims(x)==3;
0035     
0036     % 3D: unfold, apply normcol on 2D, fold
0037     [m,n,o]=size(x);
0038     x=nt_unfold(x);
0039     if isempty(w);
0040         % no weight
0041         [y,NN]=nt_normcol(x);
0042         N=NN.^2;
0043         y=nt_fold(y,m);
0044     else
0045         % weight
0046         if size(w,1)~=m; error('weight matrix should have same nrows as data'); end 
0047         if ndims(w)==2 && size(w,2)==1; 
0048             w=repmat(w,[1,m,o]);
0049         end
0050         if size(w)~=size(w); error('weight should have same size as data'); end
0051         w=nt_unfold(w);
0052         [y,NN]=nt_normcol(x,w);
0053         N=NN.^2;
0054         y=nt_fold(y,m);
0055     end
0056 
0057 else
0058     
0059     % 2D
0060     [m,n]=size(x);
0061     if isempty(w)
0062 
0063         % no weight
0064         %N=sqrt(sum(x.^2)/m);
0065         %y=vecmult(x,1./N);
0066         N=(sum(x.^2)/m);
0067         NN=N.^-0.5;
0068         NN(find(N==0))=0;
0069         y=nt_vecmult(x,NN);
0070        
0071     else
0072 
0073         % weight
0074         if size(w,1)~=size(x,1); error('weight matrix should have same ncols as data'); end 
0075         if ndims(w)==2 && size(w,2)==1; 
0076             w=repmat(w,1,n);
0077         end
0078         if size(w)~=size(w); error('weight should have same size as data'); end
0079         if size(w,2)==1; w=repmat(w,1,n);end
0080         %N=sqrt(sum((x.^2).*w)./sum(w));
0081         %y=vecmult(x,1./N);
0082         N=(sum((x.^2).*w)./sum(w));
0083         NN=N.^-0.5;
0084         NN(find(N==0))=0;
0085         y=nt_vecmult(x, NN);
0086         
0087     end
0088 end
0089 
0090 norm=N.^0.5;

Generated on Sun 11-Dec-2016 18:36:17 by m2html © 2005