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:

- nt_fold y=fold(x,epochsize) - fold 2D to 3D
- nt_normcol [y,norm]=nt_normcol(x,w) - normalize each column so its weighted msq is 1
- nt_unfold y=nt_fold(x) - unfold 3D to 2D
- nt_vecmult y=nt_vecmult(x,v) - multiply all rows or columns of matrix by vector

This function is called by:

- nt_bias_cluster [c0,c1,A,todss,pwr0,pwr1]=nt_bias_cluster(x,dsr,flags) - cluster covariance
- nt_cca_aad [D,E,R]=nt_cca_match_aad(xA,xB,y,ssize) - calculate metrics for match-mismatch task
- nt_cca_crossvalidate [AA,BB,RR,iBest]=nt_cca_crossvalidate(xx,yy,shifts,ncomp,A0,B0) - CCA with cross-validation
- nt_cca_crossvalidate2 [A,B,RR]=nt_cca_crossvalidate2(xx,yy,shifts) - CCA with cross-validation
- nt_cca_mm [D,E,R]=nt_cca_match_mm3(x,y,ssize) - calculate metrics for match-mismatch task
- nt_cluster_jd [IDX,todss,SCORE,COVS]=nt_cluster_jd(x,dsr,smooth,flags,init,verbose) - cluster with joint diagonalization
- nt_inpaint function y=nt_inpaint(x,w) - weighted interpolation based on correlation structure
- nt_narrowband_scan A=nt_narrowband_scan(x,freqs,sr,Q,plotflag) - scan for narrowband components using DSS
- nt_normcol [y,norm]=nt_normcol(x,w) - normalize each column so its weighted msq is 1
- nt_normpage y=nt_normpage(x,w) - normalize each page so its weighted msq is 1
- nt_normpagecol y=nt_normpagecol(x,w) - normalize each column of each page so its weighted msq is 1
- nt_outliers [w,y]=nt_outliers(x,w,thresh,niter) - detect outliers based on weighted correlation structure
- nt_sns1 y=nt_sns1(x,nneigbors,skip,w,threshold) - sensor noise suppression
- nt_spect_plot2 nt_spect_plot2 - plot power spectrum
- nt_split_jd [idx,score_vector,todss]=nt_split_dss(x,thresh,depth) - segmentation based on joint diagonalization
- nt_star [y,w,ww]=nt_star(x,thresh,closest,depth) - sensor noise suppression
- nt_star2 [y,w,ww]=nt_star2(x,thresh,closest,w) - sensor noise suppression
- nt_tsr [y,idx,w]=nt_tsr(x,ref,shifts,wx,wref,keep,thresh) - time-shift regression (TSPCA)
- nt_tsr_nodemean [y,idx,w]=nt_tsr_nodemean(x,ref,shifts,wx,wref,keep,thresh) - time-shift regression (TSPCA)

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