I am using xcorr to synchronize two signals (S1, S2; from two different devices) of the same event. The function works well and signals get aligned. However, given that signals' length are not consistently equal (sometimes numel(S1) > numel(S2) and viceversa), I am struggling to efficiently clean the longest signal (at the beginning and / or at the end) and have the two signals aligned, with equal lengths.
Here is the code I am using:
[C22, lag22] = xcorr(S1, S2);
[M22,I22] = max(C22);
t22 = lag22(I22);
figure(1), plot(1:length(S1), S1, 'b', 1+t22:length(S2)+t22, S2, 'r'), title('S1 and S2 "Synch"')
How could I follow to make both S1 and S2 lengths equal, having into account that I am not aware of which signal will be longer at each particular case?
I really appreciate any help on this.