Home > NoiseTools > nt_smooth.m

# nt_smooth

## PURPOSE

y=nt_smooth(x,T,nIterations,nodelayflag) - smooth by convolution with square window

## SYNOPSIS

function x=nt_smooth(x,T,nIterations,nodelayflag)

## DESCRIPTION

    y=nt_smooth(x,T,nIterations,nodelayflag) - smooth by convolution with square window

      y: smoothed data

      x: data to smooth
      T: samples, size of window (can be fractionary)
      nIterations: number of iterations of smoothing operation (large --> gaussian kernel)
      nodelayflag: if true, compensate for delay [default:false]

## CROSS-REFERENCE INFORMATION

This function calls:

- nt_greetings nt_greetings - display message the first time the toolbox is used
- nt_smooth y=nt_smooth(x,T,nIterations,nodelayflag) - smooth by convolution with square window

This function is called by:

- nt_cluster_jd [IDX,todss,SCORE,COVS]=nt_cluster_jd(x,dsr,smooth,flags,init,verbose) - cluster with joint diagonalization
- nt_multismooth z=nt_multismooth(x,smooth,alignment,diff_flag) - apply multiple smoothing kernels
- nt_multismooth_old z=nt_multismooth(x,smooth,alignment,diff_flag) - apply multiple smoothing kernels
- nt_smooth y=nt_smooth(x,T,nIterations,nodelayflag) - smooth by convolution with square window
- nt_zapline [y,yy]=nt_zapline(x,fline,nremove,p,plotflag) - remove power line artifact

## SOURCE CODE

    0001 function x=nt_smooth(x,T,nIterations,nodelayflag)
    0002 %y=nt_smooth(x,T,nIterations,nodelayflag) - smooth by convolution with square window
    0003 %
    0004 %  y: smoothed data
    0005 %
    0006 %  x: data to smooth
    0007 %  T: samples, size of window (can be fractionary)
    0008 %  nIterations: number of iterations of smoothing operation (large --> gaussian kernel)
    0009 %  nodelayflag: if true, compensate for delay [default:false]
    0010 %
    0011 nt_greetings;
    0012
    0013 if nargin<4||isempty(nodelayflag); nodelayflag=0; end
    0014 if nargin<3||isempty(nIterations); nIterations=1; end
    0015 if nargin<2; help nt_smooth ; error('!'); end
    0016
    0017 if ndims(x)>4; error('!'); end
    0018
    0019 integ=floor(T);
    0020 frac=T-integ;
    0021
    0022 if integ>=size(x,1);
    0023     x=repmat(mean(x),[size(x,1),1,1,1]);
    0024     return;
    0025 end
    0026
    0027 % remove onset step
    0028 mn=mean(x(1:(integ+1),:,:),1);
    0029 x=bsxfun(@minus,x,mn);
    0030
    0031 if nIterations==1 && frac==0;
    0032     % faster
    0033     x=cumsum(x);
    0034     x(T+1:end,:)=x(T+1:end,:)-x(1:end-T,:);
    0035     x=x/T;
    0036 else
    0037     % filter kernel
    0038     B=[ones(integ,1);frac]/T;
    0039     for k=1:nIterations-1
    0040         B=conv(B,[ones(integ,1);frac]/T);
    0041     end
    0042     x=filter(B,1,x);
    0043 end
    0044
    0045 if nodelayflag
    0046     shift=round(T/2*nIterations); %[shift n*T]
    0047     x=[x(shift+1:end,:,:,:); zeros(shift,size(x,2),size(x,3),size(x,4))];
    0048 end
    0049
    0050
    0051 % restore DC
    0052 x=bsxfun(@plus,x,mn);