how do you normalize the correlation values of xcorr for vectors of different lengths?

8 views (last 30 days)
For example, let's say that I have a vector of data that was gathered by sampling at uniform time intervals (let's call this vector 'data'). There is a particular interval of interest within this data, which I isolate to create a shorter vector which is a subset of 'data' (let's call this vector 'marker').
What I am trying to do is cross correlate 'marker' with different vectors of data to see if that signal exists within them, where in the data it appears, and how closely the signals match. Using xcorr takes me most of the way there, except that I would like to scale the correlation value such that if there is a perfect match, it returns a value of 1.
However, when I try the code shown below, which was suggested in http://www.mathworks.com/matlabcentral/answers/5275-algorithm-for-coeff-scaling-of-xcorr, I find the correct location of 'marker', but I get a maximum correlation value of ~0.25 despite what should be a perfect match (since 'marker' was taken from 'data'):
data = data - mean(data);
marker = %some interval in data
[c, lags] = xcorr(data, marker);
c = c / sqrt(sum(abs(a).^2)*sum(abs(b).^2)));
ind = find(c == max(c));
lag = lags(ind);
This may be part of the problem, but the mean was subtracted from 'data' first, and then 'marker' was taken from 'data'.
So how can i get max(c) to be 1 when 'marker' is an exact subset of 'data'?
Thanks in advance!
  1 Comment
Greg Dionne
Greg Dionne on 23 Feb 2017
When you say "perfect match" do you mean matching signals of the form:
marker(1:n) to data(m:m+n-1)
marker(1:n) to A*data(m:m+n-1) [for any non-zero A]
marker(1:n) to A*data(m:m+n-1) + B? [for any non-zero A and B]

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!