MATLAB Answers

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

28 views (last 30 days)
Antonio Morales
Antonio Morales on 28 Dec 2016
Commented: Antonio Morales on 28 Dec 2016
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

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!