I would like to find the exact replica of signal A in time series B. I am working with the simple toy example below, in which A is located starting at the 4th position in B.
I thought that using xcorr
would achieve this but I am clearly failing somewhere. xcorr
doesn't seem to be the right function despite the MATLAB help notes on xcorr
and comments such as:
Cross-correlation is usually the simplest way to determine the time lag between two signals. The position of peak value indicates the time offset at which the two signals are the most similar.
A = [1 2 1];
B = [0 9 8 1 2 1 7 3 1 0];
[c, lags] = xcorr(A,B)
lags(c==max(c))
c =
Columns 1 through 12
0.0000 1.0000 5.0000 14.0000 18.0000 11.0000 6.0000 12.0000 26.0000 26.0000 9.0000 0.0000
Columns 13 through 19
0.0000 0.0000 0.0000 -0.0000 -0.0000 -0.0000 -0.0000
lags =
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
ans =
-1 0
The max correlation values are located at lags of -1 and 0, which doesn't match the offset required to get A to match B. This leads me to ask:
- When is
xcorr
useful for finding the exact signal in another signal? 2. And what function should I be using to find the location of A in B?
Ideally I would like to test this method on more complicated signals as well as look for methods of detecting "similar" signals inside other (longer) signals.
No comments:
Post a Comment