% unbuffer_nu - Non-uniform hop overlap and add
%
% unbuffer_nu :: 
%    seq([[1,_]])  ~'sequence of overlapping frames',
%    seq(natural)  ~'sequence of hop sizes'
% -> seq([[1,_]])  ~'sequence of de-overlapped frames'.

function Y=unbuffer_nu(X,hop)
	Y=zipaccum(@olap,[],hop,X);

	function [y,s1]=olap(hop,x,s)
		ls=length(s);
		lx=length(x);
		if lx>=hop
			if ls>=hop
				% NB: this will fail if ls>lx, but this shouldn't happen since ls<=lx-hop
				y=(x(1:hop)+s(1:hop))';
				s1=[s(hop+1:ls)+x(hop+1:ls);x(ls+1:end)];
			else
				y=[x(1:ls)+s;x(ls+1:hop)]';
				s1=x(hop+1:end);
			end
		else
			if ls>=hop
				y=[s(1:lx)+x;s(lx+1:hop)]';
				s1=s(hop+1:end);
			else
				y=zeros(1,hop);
				y(1:ls)=y(1:ls)+s';
				y(1:lx)=y(1:lx)+x';
			end
		end
	end
end

