% tri_filterbank_aa - Bank of anti-aliased triangular filters 
%
% tri_filterbank_aa ::
%    E:[[M+2]->nonneg] ~'array of filter edge frequencies',
%    F:[[N]]           ~'array of bin frequencies for input spectra'
% -> [[M,N]]           ~'matrix to multiply by input spectra'.

function W=tri_filterbank_aa(fb_edges,fmap)
	bins = buffer(edges(fmap),2,1,'nodelay');
	M = length(fb_edges)-2; % total number of filters to prepare
	W = zeros(M,size(bins,2));
	for i=1:M
		W(i,:) = olap_up(bins,fb_edges(i:i+1)) + olap_dn(bins,fb_edges(i+1:i+2));
	end
	W=sparse(W);
end

function y=olap_up(bands,seg), [lx,cx]=intersect(bands,seg); y=lx.*(cx-seg(1))./(seg(2)-seg(1)); end
function y=olap_dn(bands,seg), [lx,cx]=intersect(bands,seg); y=lx.*(seg(2)-cx)./(seg(2)-seg(1)); end
function [lens,centres]=intersect(i1,i2), 
	ivals=[max(i1(1,:),i2(1));min(i1(2,:),i2(2))]; % intersect intervals
	lens=max(ivals(2,:)-ivals(1,:),0); % positive lengths only
	centres=(ivals(2,:)+ivals(1,:))/2; % centres of intervals
end
