samer@1: % gather - collect all samples from a finite signal samer@1: % gather :: signal(C,R), options -> [[C,N]]. samer@1: function x=gather(sig,varargin) samer@1: opts=prefs('chunk',256,'init',512,'grow',2,'max',1e9,varargin{:}); samer@1: samer@1: s=construct(sig); samer@1: try % to make sure we dispose of s once opened samer@1: chunk=uint32(opts.chunk); samer@1: n=uint32(0); CHUNK=1:chunk; samer@1: cap=opts.init; % initial capacity of buffer samer@1: x=zeros(channels(sig),cap); % buffer samer@1: r=s.reader(opts.chunk); samer@1: rem=0; samer@1: s.start(); samer@1: while rem==0 samer@1: if n+chunk>cap % need more room samer@1: if n>opts.max, error('maximum capacity exceeded'); end samer@1: cap=opts.grow*cap; samer@1: x=repmat(x,1,opts.grow); samer@1: end samer@1: [x(:,n+CHUNK),rem]=r(); samer@1: n=n+chunk; samer@1: end samer@1: n=n-rem; % remove rem samples from end samer@1: catch ex samer@1: s.dispose(); samer@1: rethrow(ex); samer@1: end samer@1: s.stop(); samer@1: s.dispose(); samer@1: x=x(:,1:n); % grab only valid samples samer@1: end samer@1: