I need to align 3 signals by cross correlation

5 views (last 30 days)
I have 3 signals. I am using the code in https://uk.mathworks.com/help/signal/ug/align-signals-using-cross-correlation.html. My problem is the following. In my case I cannot represent the graph with the three signals, so I need to generate a code that can align them without knowing a priory which one lags or leads.
I have been trying different options and I cannot seem able to find the way. The following code seems to work sometimes :S
%compare 2-1// 3-1
[C21,lag21] = xcorr(s2,s1);
[M21,I21] = max(C21);
t21 = lag21(I21);
[C31,lag31] = xcorr(s3,s1);
[M31,I31] = max(C31);
t31 = lag31(I31);
% compare 3-2 1-2
[C32,lag32] = xcorr(s3,s2);
[M32,I32] = max(C32);
t32 = lag32(I32);
[C12,lag12] = xcorr(s1,s2);
[M12,I12] = max(C12);
t12 = lag12(I12);
%compare 1-3//2-3
[C13,lag13] = xcorr(s1,s3);
[M13,I13] = max(C13);
t13 = lag13(I13);
[C23,lag23] = xcorr(s2,s3);
[M23,I23] = max(C23);
t23 = lag23(I23);
if t23<0
s3 = s3(-t23+1:end);
if t21>0
s2 = s2(t21+1:end);
else
s1 = s1(-t21+1:end);
end
else
s2 = s2(t23:end);
if t21<0
s1 = s1(-t31+1:end);
else
s2 = s2(t21+1:end);
end
end

Answers (1)

Star Strider
Star Strider on 21 Nov 2022
The alignsignals function (or simlar functions such as findsignal) could do what you want. With 3 signals, it would be necessary to run it 1 time for each pair of signals (e.g. 1,1; 1,2; 1,3; 2,3) and then check the results, and repeat until you are satisfied with the result.
I am not certain if it would be possible to align all 3 signals at the same time.

Community Treasure Hunt

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

Start Hunting!