% additive_split - Arrow to process data with adaptive zero mean
%
% additive_split ::
%    arrow( {[N,M]}, {[N,M]}, S1) ~'arrow to process N channel zero mean data',
%    arrow( {[N]}, {[N]}, S2)     ~'arrow to process estimated mean',
%    options {}                   ~'options passed to azeromean'
% -> arrow( {[N,M]}, {[N,M]}, pair( pair([N],pair(S1,S2)), empty)).
%
% This arrow uses the azeromean arrow to adaptively estimate the mean of
% the N channels of input data, using a gaussian data model. The mean is
% subtracted out. The two components are then processed independently by
% the two supplied arrows and added together to create the output.

function o=additive_split(a1,a2,varargin)
	opts=prefs(varargin{:});

	o = states(azeromean(gaussian,opts))*(a1+a2)*aplus;
end

function o=aplus, o=arrf(@plusfn,2,1); end
function f=plusfn(sz)
	if all(sz{1}==sz{2}), f=@plus;
	elseif sz{1}(1)==sz{2}(1) && sz{1}(2)>sz{2}(2)
		f=@(a,b)a+repmat(b,1,size(a,2));
	elseif sz{1}(1)==sz{2}(1) && sz{1}(2)<sz{2}(2)
		f=@(a,b)b+repmat(a,1,size(b,2));
	else
		error('additive_split:incompatible sizes');
	end
end
