After aligning two signals using xcorr, how to make both signals (varying) lengths equal?

9 views (last 30 days)
Hello,
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.
Thanks Antonio
  3 Comments
Antonio Morales
Antonio Morales on 28 Dec 2016
Thank you for your answer.
You can see attached an S1 and S2 signals example.
xcorr actually gives me what visually I would expect it to be (both are acceleration signals of the same event: a jump from an athlete).
[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"')
After finding out the lag between both signals, I want to make them the same length by cutting the longest for further processing (that is what I meant by "clean").
This may be a very simple step, but I'm stuck. In the multiple signals I have, lengths of S1 and S2 are never the same, and the "lag" is sometimes negative and other positive.
How could I write an "if" condition that efficiently "cuts" or "eliminates" both signals edges, so that both S1 and S2 are aligned and the same length?

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 28 Dec 2016
You can also use the alignsignals function. It is a recent introduction (I believe one of the R2015 releases). It give several options for the output.

Community Treasure Hunt

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

Start Hunting!