How to determine delay between a signal and a truncated shifted version of itself
2 views (last 30 days)
Show older comments
Hi guys,
I'm trying to sync two signals of different length. For now, I just use the same signal, truncate it (make some points zero) and shift it on time. I'd like to find the time delay to be able to sync both, but since signals are not equal anymore (because of truncation) I get a wrong value for the delay when using xcorr. Here we have an example code to sync x and y:
close all
% Create example signals
N2 = 10;
N1 = N2*5;
k = 0:200-1;
z = k.*(k>=0 & k<N1);
x = z+ N1*(k>=N1 & (k<=N1+N2)) - 5*(k-N1).*((k>=N1) & (k<=N1+N2)); % original signal
% plot original signal
subplot(2,1,1);
stem(k,x); hold all
Nshift = 0; % Delay between signals (you can play with this parameter)
y = [zeros(1,Nshift), z(1:end-Nshift)]; % create second delayed and truncated signal
stem(k,y); % and plot it
subplot(2,1,2); %plot also sum of products
stem(k,y.*x); hold all
legend(num2str(sum(y.*x)));
% determine max to detect delay and print in legend
[XC,LAGS] = xcorr(x,y); % Correlation
[maxXC,I] = max(abs(XC)); % index for maxima
figure; stem(LAGS,abs(XC)); legend(num2str(LAGS(I)));
Even in the simplest case you have no delay (Nshift=0) you get the wrong estimation (in that case 5 instead of 0). Does anyone have an idea how to solve this problem to get the correct delay?
Regards, Martin
0 Comments
Answers (1)
Star Strider
on 9 Sep 2016
See if the alignsignals function will do what you want. There is a link to the finddelay function at the end of that page, so consider it as well.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!