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:

- nt_demean [y,mn]=nt_demean(x,w) - remove weighted mean over cols
- nt_fold y=fold(x,epochsize) - fold 2D to 3D
- nt_greetings nt_greetings - display message the first time the toolbox is used
- nt_unfold y=nt_fold(x) - unfold 3D to 2D
- nt_vecadd y=nt_vecadd(x,v) - add vector to all rows or columns of matrix
- nt_vecmult y=nt_vecmult(x,v) - multiply all rows or columns of matrix by vector

This function is called by:

- nt_cca [A,B,R]=nt_cca(x,y,shifts,C,m,thresh,demeanflag) - canonical correlation
- 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_cov_lags [C,tw,m]=nt_cov_lags(x,y,shifts,nodemeanflag) - covariance of [x,y] with lags
- nt_demean [y,mn]=nt_demean(x,w) - remove weighted mean over cols
- nt_demean2 y=nt_demean2(x,w) - remove mean of each row and page
- nt_destep [y,stepList]=nt_destep(x,thresh,guard,depth,minstep) - remove step glitch from MEG data
- nt_detrend [y,w,r]=nt_detrend(x,order,w,basis,thresh,niter,wsize) - robustly remove trend
- nt_dprime [d,e]=nt_dprime(x,y,jd_flag) - calculate d' (discriminability) of two distributions
- nt_dprime_old [d,err_rate,perm]=nt_dprime(x,y,jd_flag) - calculate d' (discriminability) of two distributions
- nt_idx_disp nt_idx_disp(name,field,explainflag) - display contents of index file
- nt_inpaint function y=nt_inpaint(x,w) - weighted interpolation based on correlation structure
- nt_iplot [hh,ii]=nt_iplot(fname) - plot data file based on index
- nt_outliers [w,y]=nt_outliers(x,w,thresh,niter) - detect outliers based on weighted correlation structure
- nt_regw [b,z]=nt_regw(y,x,w) - weighted regression
- nt_sgram [s,f,t]=nt_sgram(x,window,noverlap,nfft,sr,flags) - spectrogram
- nt_sns y=nt_sns(x,nneigbors,skip,w) - sensor noise suppression
- nt_split [idx,score_vector,score]=nt_split(x,depth,thresh,guard,minstep) - split time series into intervals
- 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_tsregress [z,idx]=nt_tsregress(x,y,shifts,xw,yw,keep,threshold) - time-shift regression
- nt_zapline [y,yy]=nt_zapline(x,fline,nremove,p,plotflag) - remove power line artifact

## 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 iscell(x)
    0016     if ~isempty(w); error('!'); end; % not implemented
    0017     for iCell=1:numel(x)
    0018         [x{iCell},mn{iCell}]=nt_demean(x{iCell});
    0019     end
    0020     return;
    0021 end
    0022
    0023 if ~isempty(w) && numel(w)<size(x,1)
    0024     w=w(:);
    0025     % interpret w as array of indices to set to 1
    0026     if min(w)<1 || max(w)>size(x,1);
    0027         error('w interpreted as indices but values are out of range');
    0028     end
    0029     ww=zeros(size(x,1),1);
    0030     ww(w)=1;
    0031     w=ww;
    0032 end
    0033
    0034
    0035 if size(w,3)~=size(x,3);
    0036     if size(w,3)==1 && size(x,3)~=1;
    0037         w=repmat(w,[1,1,size(x,3)]);
    0038     else
    0039         error('W should have same npages as X, or else 1');
    0040     end
    0041 end
    0042
    0043 [m,n,o]=size(x);
    0044 x=nt_unfold(x);
    0045
    0046 if isempty(w);
    0047
    0048     mn=mean(double(x),1);
    0049     x=nt_vecadd(x,-mn);
    0050
    0051 else
    0052
    0053     w=nt_unfold(w);
    0054
    0055     if size(w,1)~=size(x,1)
    0056         error('X and W should have same nrows');
    0057     end
    0058
    0059
    0060     if size(w,2)==1;
    0061         mn=sum(nt_vecmult(double(x),w),1) ./ (sum(w,1)+eps);
    0062     elseif size(w,2)==n;
    0063         mn=sum(x.*w) ./ (sum(w,1)+eps);
    0064     else
    0065         error('W should have same ncols as X, or else 1');
    0066     end
    0067
    0068     %y=bsxfun(@minus,x,mn);
    0069     x=nt_vecadd(x,-mn);
    0070
    0071 end
    0072
    0073 x=nt_fold(x,m);
    0074