Home > NoiseTools > nt_multishift.m

nt_multishift

PURPOSE ^

y=nt_multishift(x,shifts,pad) - apply multiple shifts to matrix

SYNOPSIS ^

function [z,zz]=nt_multishift(x,shifts,pad)

DESCRIPTION ^

y=nt_multishift(x,shifts,pad) - apply multiple shifts to matrix

   y: result

   x: matrix to shift (time X channels)
   shifts: array of shifts (must be nonnegative)
 
 X is shifted column by column (all shifts of 1st column, then all
 shifts of second column, etc).
 
 X may be 1D, 2D or 3D. See also convmtx, nt_multismooth, nt_mfilt.

 NoiseTools

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [z,zz]=nt_multishift(x,shifts,pad)
0002 %y=nt_multishift(x,shifts,pad) - apply multiple shifts to matrix
0003 %
0004 %   y: result
0005 %
0006 %   x: matrix to shift (time X channels)
0007 %   shifts: array of shifts (must be nonnegative)
0008 %
0009 % X is shifted column by column (all shifts of 1st column, then all
0010 % shifts of second column, etc).
0011 %
0012 % X may be 1D, 2D or 3D. See also convmtx, nt_multismooth, nt_mfilt.
0013 %
0014 % NoiseTools
0015 
0016 if nargin<2; error('!'); end
0017 if nargin<3; pad=0; end
0018 
0019 if nargout==2
0020     % return as 3D array (time X lags X channels)
0021     z=nt_multishift(x,shifts,pad);
0022     zz=reshape(z,[size(z,1),numel(shifts),size(x,2),size(x,3)]);
0023     return
0024 end
0025 
0026 if iscell(x)
0027     for iCell=1:length(x);
0028         z{iCell}=nt_multishift(x{iCell},shifts);
0029     end
0030     return;
0031 end
0032 
0033 if size(x,1)<max(shifts); error('shifts should be no larger than nrows'); end
0034 if min(shifts)<0; error('shifts should be nonnegative'); end
0035 shifts=shifts(:)';
0036 nshifts=numel(shifts);
0037 if nshifts==1 && shifts(1)==0; 
0038     z=x;
0039     return
0040 end
0041 
0042 % array of shift indices
0043 N=size(x,1)-max(shifts); 
0044 shiftarray=nt_vecadd(nt_vecmult(ones(N,nshifts),shifts),(1:N)');
0045 [m,n,o]=size(x);
0046 z=zeros(N,n*nshifts,o);
0047 
0048 for k=1:o
0049     for j=0:n-1
0050         y=x(:,j+1,k);
0051         z(:,j*nshifts+1: j*nshifts+nshifts,k)=y(shiftarray);
0052     end
0053 end
0054 
0055 if pad
0056     z(size(x,1),:,:)=0;
0057 end

Generated on Sat 29-Apr-2023 17:15:46 by m2html © 2005