%D_TDER  HOSA Demo: Time-delay estimation using cross-correlation (tder)
%       
echo off 
% A. Swami Oct 18, 1997.
% Copyright (c) 1991-1999 by United Signals & Systems, Inc. and The Mathworks, Inc. All Rights Reserved.
%       $Revision: 1.7 $

%     RESTRICTED RIGHTS LEGEND
% Use, duplication, or disclosure by the Government is subject to
% restrictions as set forth in subparagraph (c) (1) (ii) of the 
% Rights in Technical Data and Computer Software clause of DFARS
% 252.227-7013. 
% Manufacturer: United Signals & Systems, Inc., P.O. Box 2374, 
% Culver City, California 90231. 
%
%  Copyright (c) 1991-1999 by United Signals & Systems, Inc. and The Mathworks, Inc. All Rights Reserved.
%  This material may be reproduced by or for the U.S. Government pursuant 
%  to the copyright license under the clause at DFARS 252.227-7013. 

clear, clc, 

echo on 
%      Time-delay estimation using ML-windowed cross-correlation
%
% TDER uses the ML-windowed cross-correlation method.  Here, the 
% cross-coherence between the two sensor signals is computed; from 
% this an optimal window is estimated; this window is applied to the
% cross-correlation between the two sensor signals;  the peak should 
% occur at the correct delay.
%
% We will test TDER on some data generated by TDEGEN: 
% The data consist of two signals, y1 = s1 + g1, and y2 = s2 + g2. 
% s1 is  an i.i.d. sequence with the single-sided exponential distribution 
%        (mean=0, variance=1, skewness=2). 
% s2 is a delayed version of s1 (delay of 16 samples).
% g1 is a white Gaussian noise sequence with unity variance.
% g2 is obtained by passing g1 through the MA filter, 
%    [1 2 3 4 5 6 6 4 3 2 1].  
% The two noise signals, g1 and g2, have a strong cross-correlation at a delay
%     of 5 samples. 

% Hit any key to continue
pause

load tde1
m   = length(r21);
m   = (m-1)/2;
clf 
subplot(211)
plot(-m:m, r21),title('The cross-correlation between the two signals')
grid on 
set (gcf, 'Name','HOSA TDER ') 

% 
%  The cross-correlation displays two peaks:  
%      a broad peak at n=5 corresponding to the noise 
%      a sharp peak at n=16 corresponding to the signal delay 
%  The cross-correlation by itself cannot be used to distinguish between 
%     these two peaks. 

subplot(212)
d3 = tder (s1, s2, 30, 64, 0, 64); 
a = axis;
axis([-m,m,a(3),a(4)])
set (gcf, 'Name','HOSA TDER') 

% Note that we still have two dominant peaks - one corresponding to the
% signal, and the other to the spatially correlated noise
%
% Hit any key to return to the previous menu 
pause 
echo off
clc
