function [thetaest,err,rerr,wrongd] = ...
        use_eigesprit(X1dat,X2dat,dtru,duse,forget,tol,theta_tru,nsamp);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                        %
% [thetaest,err,rerr,wrongd] = ...                       %
%     use_eigesprit(X1dat,X2dat,dtru,duse,forget,tol,... %
%                   theta_tru,nsamp);                    %
%                                                        %
%  This program uses the eigenvalue-based Esprit         %
%  algorithm to estimate the directions of arrival       %
%  (DOAs) of some signals using exponential windowing.   %
%                                                        %
%  X1dat:  the columns are the time series data from     %
%          the first sensor in each pair.                %
%  X2dat:  the columns are the time series data from     %
%          the second sensor in each pair.               %
%  dtru:   the true number of signals.                   %
%  duse:   the number of signals to be used in the       %
%          algorithm.  If duse=0, then the               %
%          algorithm should estimate this value.         %
%  forget: the forgetting factor for windowing           %
%  tol:    the tolerance for deciding that the remaining %
%          signals are noise.                            %
%  theta_tru: row j contains the true DOAs for the       %
%            jth time.                                   %
%  nsamp:  number of estimates to compute.               %
%                                                        %
%  thetaest: row j contains the estimated DOAs for the   %
%             jth time.                                  %
%  err:     the average absolute error in the DOA        %
%             estimates.                                 %
%  rerr:    the average relative error in the DOA        %
%             estimates.                                 %
%  wrongd:  the number of times that the number of       %
%              signals was estimated incorrectly.        %
%              (=0 if dtru nonzero)                      %
%                                                        %
%  From: Your Homework Assignment Project 4              %
%  The DOA Problem: Coming at You                        %
%  Dianne P. O'Leary 07/03                               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Initialization                                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

wrongd = 0;
err = 0;
rerr = 0;
[n1,n2]=size(X1dat);
nn = 2*n1;

XX = zeros(nn,nn);
d = max(dtru,2);
for j=1:d-1
   t = [X1dat(:,j);X2dat(:,j)];
   XX = (forget*forget)*XX + t*t';
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Compute the estimates and assess the performance.     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for j=2:nsamp
   t = [X1dat(:,j);X2dat(:,j)];
   XX = (forget*forget)*XX + t*t';
   [theta,d] = eig_esprit (XX,duse,tol);
   if (d == dtru)
      thetaest(j,:) = theta;
      err = err + sum(abs(theta-theta_tru(j,:)));
      rerr = rerr + sum(abs((theta-theta_tru(j,:))./theta_tru(j,:)));
   else
      thetaest(j,:) = [0 0];
      wrongd = wrongd + 1;
   end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Normalize the average errors.                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

err = err/(2*(nsamp-1-wrongd));
rerr = rerr/(2*(nsamp-1-wrongd));

