samer@3
|
1 % framedata - return abstract data object for buffering signal frames
|
samer@0
|
2 %
|
samer@0
|
3 % framedata ::
|
samer@3
|
4 % ( s:seq [C,T] ~'C-channel signal of length T',
|
samer@0
|
5 % N:natural ~'frame size',
|
samer@0
|
6 % M:natural ~'hop size',
|
samer@0
|
7 % L:natural ~'frames per buffer' | ([] => whole file)
|
samer@0
|
8 % options... )
|
samer@3
|
9 % -> seq [N,L].
|
samer@0
|
10 %
|
samer@0
|
11 % Options:
|
samer@0
|
12 % wrap/0 if 1, then buffer samples as if one continuous circular signal
|
samer@0
|
13 % otherwise, don't return buffers that wrap round from end to start
|
samer@0
|
14 % random/0: if [], return buffers sequentially, if number or random state
|
samer@0
|
15 % vector, return buffers in random order, using value as seed.
|
samer@0
|
16 % filter/[] optional filter function to apply to long windows before buffering
|
samer@0
|
17 %
|
samer@0
|
18 % Note: if the signal is a multichannel signal, ie of size CxT with C>1,
|
samer@0
|
19 % the samples for each channel are interleaved.
|
samer@0
|
20
|
samer@3
|
21 function a=framedata(signal,frame,hop,width,varargin)
|
samer@0
|
22 if nargin<4, width = []; end
|
samer@37
|
23 opts=options('wrap',0,'random',[],'dim',2,'filter',@id,varargin{:});
|
samer@0
|
24 if isempty(width), opts.truncate=0; end
|
samer@0
|
25
|
samer@0
|
26 [span,jump]=windowparams(size(signal),frame,hop,width,opts);
|
samer@0
|
27 if isempty(opts.random),
|
samer@42
|
28 wd=window(signal,span,jump,'wrap',opts.wrap,'dim',opts.dim);
|
samer@0
|
29 else
|
samer@0
|
30 wd=rndwindow(signal,span,opts.dim);
|
samer@0
|
31 end
|
samer@0
|
32
|
samer@3
|
33 wd=opts.filter(wd);
|
samer@3
|
34 if size(wd,1)>1, wd=map(@flatten,wd); end
|
samer@3
|
35 olap=frame-hop;
|
samer@3
|
36 if hop<frame, buffn=@(x)buffer(x,frame,olap,'nodelay');
|
samer@3
|
37 else buffn=@(x)buffer(x,frame,olap);
|
samer@3
|
38 end
|
samer@0
|
39
|
samer@0
|
40 % NB: makes sense to cache large windows to save repeated calls
|
samer@0
|
41 % extract current large window from data source while processing
|
samer@0
|
42 % the buffered window.
|
samer@3
|
43 a=map(buffn,cache(wd));
|
samer@3
|
44 end
|