Home > fcn > sample_window.m

sample_window

PURPOSE ^

SAMPLE_FUNCTION Signal windowing

SYNOPSIS ^

function [Y,idx_X]=Sample_Window(X, Wlength, Nlength, Type, Extent);

DESCRIPTION ^

 SAMPLE_FUNCTION Signal windowing
 Y=Sample_Window(X, WINDOW, NOVERLAP, Type, Extent);
     X        :  Column vector (or N-dimensional array of column vectors)
     WINDOW   :  # samples of window
     NOVERLAP :  # overlapping samples
     Type     :  Windowing function (['none'],'hamming','hanning','boxcar')
     Extent   :  Border options (['valid'],'same')
 returns matrix Y (or N+1 dimensional array) resulting from splitting
 the signal X into overlapping windowed segments.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [Y,idx_X]=Sample_Window(X, Wlength, Nlength, Type, Extent);
0002 
0003 % SAMPLE_FUNCTION Signal windowing
0004 % Y=Sample_Window(X, WINDOW, NOVERLAP, Type, Extent);
0005 %     X        :  Column vector (or N-dimensional array of column vectors)
0006 %     WINDOW   :  # samples of window
0007 %     NOVERLAP :  # overlapping samples
0008 %     Type     :  Windowing function (['none'],'hamming','hanning','boxcar')
0009 %     Extent   :  Border options (['valid'],'same')
0010 % returns matrix Y (or N+1 dimensional array) resulting from splitting
0011 % the signal X into overlapping windowed segments.
0012 %
0013 
0014 % alfnie@bu.edu
0015 % ?/99
0016 
0017 
0018 sX=[size(X),1];
0019 if nargin<2 | isempty(Wlength), Wlength=sX(1); end 
0020 if nargin<3 | isempty(Nlength), Nlength=floor(Wlength/2); end
0021 if nargin<4 | isempty(Type), Type='none'; end
0022 if nargin<5 | isempty(Extent), Extent='tight'; end
0023 if lower(Type(1))=='c', docentering=1; Type=Type(2:end); else, docentering=0; end
0024 
0025 X=[zeros([Nlength,sX(2:end)]); X; zeros([Nlength,sX(2:end)])];
0026 sY=1+floor((size(X,1)-Wlength)/(Wlength-Nlength));
0027 
0028 idx_W=repmat((1:Wlength)',[1,sY]); 
0029 idx_X=idx_W + ones(Wlength,1)*(Wlength-Nlength)*(0:sY-1);
0030 
0031 switch(lower(Type)),
0032 case 'hamming',
0033     W=hamming(Wlength);
0034 case 'hanning',
0035     W=hanning(Wlength);
0036 case 'boxcar',
0037     W=boxcar(Wlength);
0038 case 'none',
0039     W=ones(Wlength,1);
0040 end
0041 
0042 if docentering,
0043     e=convn(X.^2,hamming(Wlength-Nlength+1),'same');
0044     E=e(idx_X(:),:).*W(idx_W(:),ones(1,prod(sX(2:end))));
0045     E=reshape(E,[Wlength,sY,sX(2:end)]);
0046     [nill,idxE]=max(E,[],1);
0047     idx_X=max(1,min(size(X,1),idx_X+repmat(idxE-floor(Wlength/2),[Wlength,1])));
0048 end
0049 
0050 if strcmp(lower(Type),'none'), Y=X(idx_X(:),:); else, Y=X(idx_X(:),:).*W(idx_W(:),ones(1,prod(sX(2:end)))); end
0051 Y=reshape(Y,[Wlength,sY,sX(2:end)]);
0052 
0053 idx_X=idx_X-Nlength*sX(2);
0054 idx_X(idx_X<=0 | idx_X>prod(sX))=nan;
0055 
0056 switch(lower(Extent)),
0057 case {'valid','tight'},
0058     idx=all(~isnan(idx_X),1);
0059     Y=reshape(Y(:,idx,:),[Wlength,sum(idx),sX(2:end)]);
0060     idx_X=idx_X(:,idx);
0061 case 'same',
0062     idx=~isnan(idx_X(floor(end/2),:));
0063     Y=reshape(Y(:,idx,:),[Wlength,sum(idx),sX(2:end)]);
0064     idx_X=idx_X(:,idx);
0065 end

Generated on Tue 27-Mar-2007 12:06:24 by m2html © 2003