Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.



Measuring Time Delay Between Two Signals, Xcorr
Posted:
Oct 9, 2008 9:53 AM


Hey everyone. So I've created two simple sine wave signals, with the same frequency, just one is the delayed version of the other by 4s (Yes I know 4s is an unrealistically high amount for my application, but for now I'm just trying to get concepts down...).
THE EXPERIMENT I'M SIMULATING: Pretend you are sending out a simple sine wave from a transmitter and receiving the signal through two different microphones, one close, and one far away and recording the data from both microphones for 10 seconds. Now, figure out the time delay between the first signal recieved by the close microphone and the signal recieved by the far microphone.
THE PROBLEM: Anyway, so I'm trying to use cross correlation to determine how much time delay there is in my second signal by finding the peak correlation point, which should (I think) occur when the two signals align. For some reason my code is giving me a weird result, so this is where you all come in :)
 THE CODE: 
clc; clear all;
f = 1; % 1 Hz frequency t = 0:.01:10; % Creates a 10 second time span for samples to be taken in
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % CREATE & PLOT SIGNAL 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% signal1 = sin(2*pi*f*t); % Create signal 1 figure(1); plot(t, signal1); title('Signal From Close Receiver/Transmitter Pair'); xlabel('Time (s)'); ylabel('Amplitude');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % CREATE & PLOT SIGNAL 2, SAME AS SIGNAL 1 % JUST TIME DELAYED 4s. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% signal2=[zeros(1,400), signal1(1,1:length(signal1)400)]; % Create a signal2 with 4 second delay
figure(2); plot(t, signal2); title('Signal From Far Receiver/Transmitter Pair'); xlabel('Time (s)'); ylabel('Amplitude');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % CORRELATE SIGNAL 1 TO SIGNAL 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% signal1 = signal1'; signal2 = signal2'; xcorrValues = xcorr(signal2,signal1) xCorrMax = max(abs(xcorrValues));

Thanks in advance for your help/support.
Zac



