%D_TDE  HOSA Demo: Time Delay Estimation using cross-cumulant method (tde)
echo off

% A. Swami April 15, 1993
% 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 cross-cumulant method 
%
%    TDE estimates the time delay between two signals, using the parametric 
%    third-order cross cumulant (bispectrum) method. The signals may be 
%    contaminated by noises which are correlated with one another.  The noise 
%    sequences may be symmetrically distributed, such as Gaussian. 
%
% We will test TDE 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 TDE ') 

% 
%  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. 

%  TDE models the signal s2(n) as the result of a MA (FIR) filtering
%  operation on the signal s1(n);  hence, ideally, the coefficients of 
%  the FIR filter are zero except at the sample (index) corresponding to
%  the true delay.   TDE estimates the FIR filter coefficients using
%  third-order cumulants. 
%  Let us assume that the maximum delay is 30, and use TDE. 

% Hit any key to continue
pause 
subplot(212) 
[delay,avec] = tde(s1,s2,30,128);
subplot(211)
a = axis;
axis([-30 30 a(3) a(4)])

disp(['estimated delay is ',int2str(delay),' samples']) 

% Note that the TDE estimate is blind to the spatially correlated noise 
%      and shows a single sharp peak at the true delay of 16 samples. 
 
% Hit any key to return to the previous menu
pause
echo off
clc 
return

