% unbuffer - Opposite of buffer using overlap and add (for sequences)
%
% unbuffer :: 
%    seq([[N]])  ~'sequence of overlapping frames',
%    M:natural   ~'hop size'
% -> seq([[1,M]])~'sequence of de-overlapped frames'.
%
% NB. what about windowing function?

function Y=unbuffer(X,hop)
	N=max(size(X));
	ol=N-hop;
	if ol<=hop
		I=1:hop; J=1:ol; K=hop+1:N;
		Y=mapaccum(@olap1,zeros(ol,1),X);
	else
		I=1:hop; J=hop+1:ol; K=ol+1:N;
		Y=mapaccum(@olap3,zeros(ol,1),X);
	end
%		Y=zipaccum(@olap2,[],windowdata(repeat(hop)),X);

	function [y,s1]=olap1(x,s)
		y=x(I)';
		y(J)=y(J)+s';
		s1=x(K);
	end

	function [y,s1]=olap3(x,s)
		y=(s(I)+x(I))';
		s1=[s(J)+x(J);x(K)];
	end
end

