% 2-D image group parameter estimator
%
% estimates parameters of the translation group
%
% Use: translation = est_trans2(E1,E2);           % 1 output argument; complex
% or : [trans_m, trans_n] = est_trans(E1,E2);    % 2 real output arguments
%
%      translation = est_trans2(E1,E2,m0,n0);    % starting guess...
%
%     1 0 0          0 0 0
% A = 0 0 0      B = 0 1 0        would ideally give translation = -1 -i
%     0 0 0          0 0 0
%
%     1 0 0          0 0 0
% A = 0 0 0      B = 1 0 0        would ideally give translation = -1
%     0 0 0          0 0 0
%
% sign convention: real down, imag to right

function [trans_m, trans_n] = two_parameter_function(E1,E2,m0,n0);

if nargin==3
  disp('only 1 initial guess given; assuming that''s a vector of trans')
  n0=m0(2);
  m0=m0(1);
end%if

if (nargin==3)|(nargin==4)
  E2=pchirp2([1 0 -m0 0 1 -n0 0 0],E2);
end%if

[El,Em,En] = derivatives(E1,E2);
ElEm = sum(sum(El.*Em));
ElEn = sum(sum(El.*En));
EmEm = sum(sum(Em.*Em));
EmEn = sum(sum(Em.*En));            % used twice; e.g. EmEn = EnEm
EnEn = sum(sum(En.*En));

DERIVATIVES = [EmEm EmEn; EmEn EnEn];  % matrix of derivatives
Derivatives = [ElEm; ElEn];            % vector of derivatives

translation = DERIVATIVES\Derivatives;

trans_m = translation(1);
trans_n = translation(2);

disp(sprintf('nargout=%g',nargout));

if nargout >= 3
  disp(sprintf('You have %g output arguments: 2 is the maximum allowed', ...
  nargout))
end%if

if nargout == 1
  trans_m = trans_m + i*trans_n;
end%if

if nargout == 0
  disp('zero output arguments were given, so I am displaying to screen:')
  disp(sprintf('trans_m=%g pixels; trans_n=%g pixels', ...
  trans_m, trans_n))
  trans_m = trans_m + i*trans_n;  % Matlab still shows first argument
end%if

