samer@4: function m=mapwindows(fn,x,L,M) samer@4: % mapwindows - Apply function to windowed signal with causality control samer@4: % samer@4: % mapwindows :: samer@4: % F:([[N]]->[[M]]) ~'function to apply to windowed segments', samer@4: % [[T]] ~'signal of length T', samer@4: % L:natural ~'post-window (ie causal part)', samer@4: % M:natural ~'pre-window (ie anti-causal part)' samer@4: % -> [[M,T]] ~'sequence of results'. samer@4: % samer@4: % This function applies a function to a sequence of segments extracted from samer@4: % the given signal by sliding a window along the signal. The samer@4: % post- and pre- window lengths determine the positioning of the window: samer@4: % samer@4: % .... x1 x2 x3 x4 x5 x6 x7 x8 x9 ... samer@4: % |<--- post --> | now |<----- pre ------> | samer@4: % \________________________________________/ samer@4: % | samer@4: % function samer@4: % | samer@4: % ... y3 y4 y5 ... samer@4: % samer@4: % Note: the function F must make sense when applied to windows of samer@4: % different lengths since the first and last few windows will samer@4: % be shorter than L+M+1. The windows will not be zero padded! samer@4: % samer@4: % EXAMPLES samer@4: % samer@4: % To compute a running mean using only past values (ie causally) samer@4: % >> Y=mapwindows(@mean,X,6,0); samer@4: % samer@4: % To compute a running local median samer@4: % >> Y=mapwindows(@median,X,4,3); samer@4: samer@4: N=length(x); samer@4: samer@4: for i=1:min(L,N) samer@4: k=min(i+M,N); samer@4: MM=feval(fn,x(1:k)); samer@4: m(:,i)=feval(fn,x(1:k)); samer@4: end samer@4: samer@4: if N>(L+M) samer@4: m=[m feval(fn,buffer(x,L+M+1,L+M,'nodelay'))]; samer@4: end samer@4: samer@4: for i=N+(1-M:0) samer@4: j=max(i-L,1); samer@4: m(:,i)=feval(fn,x(j:end)); samer@4: end samer@4: