xcorr question - better align two signals

1 回表示 (過去 30 日間)
Fan Mei
Fan Mei 2017 年 1 月 6 日
コメント済み: Fan Mei 2017 年 1 月 10 日
I tried to align two signal better using the below code. The s1 and s2 is in the attached txt file. However, td I got is -24482. I am wondering if there is a way to improve this function. Please help!!
function [td,ny]=align_signal(s1,s2)
ny=ones(size(s1))*NaN;
[C21,lag21] = xcorr(s2,s1);
C21 = C21/nanmax(C21);
[M21,I21] = nanmax(C21);
t21 = lag21(I21);
if t21<0
ny(-t21:end) = s1(-t21:end);
else
ny(t21:end)=s1(t21:end);
end
td=abs(t21);

採用された回答

Jordan Ross
Jordan Ross 2017 年 1 月 10 日
Hello Fan,
Unfortunately, the MATLAB functions that compute the sample cross-correlation from two provided sequences (such as "xcorr" in the Signal Processing Toolbox) cannot deal with incomplete data.
One possible solution is to find the NaN value's in the signals and to ignore the measurements (at that time value) for both sequences. The following code shows how you can remove those NaN's:
% Remove NaNs
s2NanLoc = find(isnan(s2));
s1NanLoc = find(isnan(s1));
nanLocs = unique([s1NanLoc; s2NanLoc]);
s1(nanLocs) = [];
s2(nanLocs) = [];
When I do this with your dataset I am able to get a "td" of 4.
  1 件のコメント
Fan Mei
Fan Mei 2017 年 1 月 10 日
Thank you so much, Jordan.

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

その他の回答 (0 件)

カテゴリ

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