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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                        %
% [thetaest,err,rerr,wrongd] = ...                       %
%     use_svdesprit(X1dat,X2dat,dtru,duse,n,tol, ...     %
%                   theta_tru,nsamp)                     %
%                                                        %
%  This program uses the SVD-based Esprit                %
%  algorithm to estimate the directions of arrival       %
%  (DOAs) of some signals using rectangular 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.         %
%  n:      the window size for rectangular 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 errors in the DOA       %
%             estimates.                                 %
%  rerr:    the average relative errors 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;

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

for j=n:nsamp
   X1 = X1dat(:,j-n+1:j);
   X2 = X2dat(:,j-n+1:j);
   [theta,d] = svd_esprit (X1,X2,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-n+1-wrongd));
rerr = rerr/(2*(nsamp-n+1-wrongd));

