xcorr raw time series vs normalized time series
Show older comments
I have two time series which I have interpolated to be on the same dt. I am trying to compute the time lag between the time series. when I use xcorr on the raw time series, I get a lag of 0, whereas when i min/max normalize the time series first, then use xcorr i get a lag of -4. Why are the lags different when all I have done is normalize the data? Which one is correct?
DS(:,2) = -DS(:,2)
c1 = corr(DS(:,2),SS(:,2))
DSnorm = (DS(:,2)-mean(DS(:,2)))/std(DS(:,2))
SSnorm = (SS(:,2)-mean(SS(:,2)))/std(SS(:,2))
c2 = corr(DSnorm,SSnorm)
dt = 4;
[C,L] = xcorr(DS(:,2), SS(:,2),'coeff');
[~, idx] = max(abs(C));
L = L*dt;
Lag = L(idx);
plot(L,C)
hold on
[C,L] = xcorr(DSnorm, SSnorm,'coeff');
[~, idx] = max(abs(C));
L = L*dt;
Lag2 = L(idx);
plot(L,C)
Answers (1)
DS=readmatrix('DS.csv');
SS=readmatrix('SS.csv');
DS(:,2) = -DS(:,2);
%c1 = corr(DS(:,2),SS(:,2))
DSnorm = (DS(:,2)-mean(DS(:,2)))/std(DS(:,2));
all(isnan(DSnorm))
That's a problem -- all your normailzed D are NaN...
std(DS(:,2))
You can try
DSnorm = (DS(:,2)-mean(DS(:,2)))/std(DS(:,2),'omitnan');
SSnorm = (SS(:,2)-mean(SS(:,2)))/std(SS(:,2));
all(isnan(SSnorm))
%c2 = corr(DSnorm,SSnorm)
dt = 4;
[C,L] = xcorr(DS(:,2), SS(:,2),'coeff');
[~, idx] = max(abs(C));
L = L*dt;
Lag = L(idx);
%plot(L,C)
%hold on
[C,L] = xcorr(DSnorm, SSnorm,'coeff');
[~, idx] = max(abs(C));
L = L*dt;
Lag2 = L(idx);
%plot(L,C)
disp([Lag Lag2])
Categories
Find more on Correlation and Convolution in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!