Time delay Calculation using FFT-Based Algorithm

조회 수: 31 (최근 30일)
Emmanuel Mendez Alicea
Emmanuel Mendez Alicea 2017년 4월 8일
댓글: Ilker CIBLAK 2020년 11월 8일
Hello,
I'm implementing in MATLAB a cross-correlation algorithm based on the FFT. I don't want to use the built in function "xcorr". I'm doing the cross-correlation to determine the time delay of two generated sine signals. This is what I have done in coding so far:
close all;
clear all;
%%Signal Generation
Fs = 1000;
Ts = 1/Fs;
fc = 60;
V = 0.1; % Window Stop Time
ActTD = 0.008; % Actual Time Delay
t = 0:Ts:V-Ts;
x = sin(2*pi*fc*t);
y = sin(2*pi*fc*(t-ActTD));
%%Spectra of Input Signals
%Correlation Length
corrLength=length(x)+length(y)-1;
%Input Signal-1 Spectra
X=fft(x,corrLength);
%Input Signal-2 Spectra
Y=fft(y,corrLength);
%%Cross Correlation in Frequency Domain
%Hadamard Product
Z=X.*(conj(Y));
%Inverse Fast Fourier Transform
z=fftshift(ifft(Z));
%Time Axis
Ly=length(z);
tz=0:Ts:(Ly-1)*Ts;
TD=max(abs(z))*Ts % Calculated Time Delay
%%Plot Section
%Original and delayed signal
plot(t,x,'r',t,y,'b');
legend('Original','Delayed');
% Cross Correlated Signal
figure
plot(tz,z);
xlabel('Time in seconds');
ylabel('Magnitude');
title('Cross Correlated Signal');
grid
As you can see, I'm adding a delay to a generated signal. The actual time delay is in line 10, but when I run the code, I'm not getting the actual time delay. It should be at least close to that value. The algorithm is based on the circular cross-correlation method. Any idea what I could be doing wrong? Maybe that is not the proper way to add delay to a signal?
There are few examples online, but most of them are by adding random delay. I want something I could control so I can know that this is actually working how is supposed to.
Any help or suggestion will be appreciated, Thanks!

채택된 답변

Honglei Chen
Honglei Chen 2017년 4월 10일
You are using max(z) in your delay computation. But that gives you the maximum correlation, yet what you want is the location of where the maximum correlation. Therefore you may want to use the index instead, e.g.
tz=(-(Ly-1)/2:(Ly-1)/2)*Ts;
[~,idx] = max(z);
TD=tz(idx) % Calculated Time Delay
I have two more remarks:
1. You are using a sinusoidal signal. Note that the signal is periodic so if you choose a delay that are larger than one period, it will aliased back to a number that is less than one period. Your current number corresponds to about half cycle, so it works ok.
2. Because your delay is about half cycle, the signal is opposite phase. Note that that corresponds to a correlation of -1. In most correlation analysis, we use abs(z). But notice that I used max(z) instead of max(abs(z)) in my snippet so it does not give you the negative peak, which is 0. This being said, you should think about whether that's what you want.
HTH
  댓글 수: 4
Mohammadmahdi Sayadi
Mohammadmahdi Sayadi 2020년 3월 31일
hello hongley chen
your answer does not work for NFFT less than offset
Ilker CIBLAK
Ilker CIBLAK 2020년 11월 8일
tz=(-(Ly-1)/2:(Ly-1)/2)*Ts;
Sir, I'm an undergraduate mechanical engineering student so donot have enough knowledge on signal processing, my apologies. However, I have a couple questions about this line of code.
  • Is it possible to converte lag value into time delay in seconds just multiplying it with period? I'm asking this question because I think this line of code doing this.
  • Secondly, why do you do Ly-1/2 ?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ilker CIBLAK
Ilker CIBLAK 2020년 11월 8일
Hi there,
I am a mechanical engineering student which has no knowledge on signal processing. Dispite this, I need to build an algorithm on matlab that computes time delay between two data arrays (or signals). So, can anyone explain me what the variables fc and V ,@ signal generation part, correspond to ?

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by