From: Rohit on
My task seems to be simple yet I am unable to figure what the hell is going wrong in my code!
My problem involves inputing the audio signal recorded by 2 mics to the MATLAB code and finding the time lag between them. But before going to experiment I wanted to check my code by performing a simulation. For this, I generated two audio signals myself using MATLAB and lagged them by a known time period. The sampling rate is 125 milliseconds . I performed corsscorrelation between both the signals to find the no. of lags at which the maximum value occurs. This I used to find the time lag (which would be time of flight between 2 mics in my case). Using a periodic signal like sine resulted in incorrect values. Hence, I tried to employ chirp signal and check out how it works out.
This is I what got using the below code:

clear all
time_lag=0.125e-3; % time in sec.
time=0:1/8000:1; a=sin(2*pi*(1000+100*time).*(time));wavwrite(a,'mic1');
time=0:1/8000:1; b=sin(2*pi*(1000+100*time).*(time-time_lag));wavwrite(b,'mic2');%
[mic1,Fs]=wavread('mic1');
[mic2,Fs]=wavread('mic2');
plot(mic2(1:500));hold on;plot(mic1(1:500),'r')
[xcf,lags]=xcorr(mic1,mic2);
[max_xcf,max_lags]=max(xcorr(mic1,mic2));
max_lags
1000*abs((max_lags-Fs-1))/Fs % time of flight in millisecs
%figure(2);plot(lags,xcf)
% with Chirp signal

If time of flight or time_lag can be represented in the form of w.d millisecs where w stands for the whole number/ integer part and d for decimal part; then the time of flight I am trying to findout by cross-correlation is [w/2].d millisecs.
'[ ]' symbol for gratest integer function
Inn short - I am getting the decimal part correctly but not the integer part.
Please provide inputs on what might went wrong.