samer@4
|
1 function m=mapwindows(fn,x,L,M)
|
samer@4
|
2 % mapwindows - Apply function to windowed signal with causality control
|
samer@4
|
3 %
|
samer@4
|
4 % mapwindows ::
|
samer@4
|
5 % F:([[N]]->[[M]]) ~'function to apply to windowed segments',
|
samer@4
|
6 % [[T]] ~'signal of length T',
|
samer@4
|
7 % L:natural ~'post-window (ie causal part)',
|
samer@4
|
8 % M:natural ~'pre-window (ie anti-causal part)'
|
samer@4
|
9 % -> [[M,T]] ~'sequence of results'.
|
samer@4
|
10 %
|
samer@4
|
11 % This function applies a function to a sequence of segments extracted from
|
samer@4
|
12 % the given signal by sliding a window along the signal. The
|
samer@4
|
13 % post- and pre- window lengths determine the positioning of the window:
|
samer@4
|
14 %
|
samer@4
|
15 % .... x1 x2 x3 x4 x5 x6 x7 x8 x9 ...
|
samer@4
|
16 % |<--- post --> | now |<----- pre ------> |
|
samer@4
|
17 % \________________________________________/
|
samer@4
|
18 % |
|
samer@4
|
19 % function
|
samer@4
|
20 % |
|
samer@4
|
21 % ... y3 y4 y5 ...
|
samer@4
|
22 %
|
samer@4
|
23 % Note: the function F must make sense when applied to windows of
|
samer@4
|
24 % different lengths since the first and last few windows will
|
samer@4
|
25 % be shorter than L+M+1. The windows will not be zero padded!
|
samer@4
|
26 %
|
samer@4
|
27 % EXAMPLES
|
samer@4
|
28 %
|
samer@4
|
29 % To compute a running mean using only past values (ie causally)
|
samer@4
|
30 % >> Y=mapwindows(@mean,X,6,0);
|
samer@4
|
31 %
|
samer@4
|
32 % To compute a running local median
|
samer@4
|
33 % >> Y=mapwindows(@median,X,4,3);
|
samer@4
|
34
|
samer@4
|
35 N=length(x);
|
samer@4
|
36
|
samer@4
|
37 for i=1:min(L,N)
|
samer@4
|
38 k=min(i+M,N);
|
samer@4
|
39 MM=feval(fn,x(1:k));
|
samer@4
|
40 m(:,i)=feval(fn,x(1:k));
|
samer@4
|
41 end
|
samer@4
|
42
|
samer@4
|
43 if N>(L+M)
|
samer@4
|
44 m=[m feval(fn,buffer(x,L+M+1,L+M,'nodelay'))];
|
samer@4
|
45 end
|
samer@4
|
46
|
samer@4
|
47 for i=N+(1-M:0)
|
samer@4
|
48 j=max(i-L,1);
|
samer@4
|
49 m(:,i)=feval(fn,x(j:end));
|
samer@4
|
50 end
|
samer@4
|
51
|