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

16 ビュー (過去 30 日間)
Antonio Morales
Antonio Morales 2016 年 12 月 28 日
コメント済み: Antonio Morales 2016 年 12 月 28 日
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 件のコメント
Antonio Morales
Antonio Morales 2016 年 12 月 28 日
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?
Antonio Morales
Antonio Morales 2016 年 12 月 28 日
(Attachments just added in the comment above)

サインインしてコメントする。

回答 (1 件)

Star Strider
Star Strider 2016 年 12 月 28 日
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.

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by