Calculating cross correlation for non-consecutive lags

2 views (last 30 days)
I have two very large vectors, T and h, each element spaced 0.01 months apart from the previous one, for a total period of 100 years, which makes a length of 120000 elements. I am interested in calculating the cross-correlation up to 10 years (120 months), but I only need to calculate it for lags multiple of whole months (i.e.,
lags=-12000,-11900,...,-100,0,100,...,11900,12000). So far I have tried to use the code scorr=crosscorr(T,h,'NumLags',12000), and then scorrred=scorr(1:100:24001) but it requires a lot of computation time due to the large number of lags. Is there a way to calculate cross-correlation for non-consecutive lags (i.e. lags=-12000,-11900,...,-100,0,100,...,11900,12000) thus optimizing the time required?
Thank you very much in advance

Answers (1)

Abhimenyu
Abhimenyu on 1 Dec 2023
Hi Rodrigo,
I understand that you want to calculate cross-correlation for non-consecutive lags and optimize the time required. This can be done by directly calculating the cross-correlation for the specific lags of interest. Since the crosscorr function in MATLAB does not inherently support non-consecutive lags, the cross-correlation for the specified lags can be manually calculated.
Please follow the example code below as a suggested approach to calculate the cross-correlation for non-consecutive lags:
% Define the lags of interest
lagsOfInterest = -12000:100:12000;
% Pre-allocate array to store cross-correlation values
crossCorrelationValues = zeros(size(lagsOfInterest));
% Get the length of the vectors T and h
n = length(T);
% Calculate the cross-correlation for each lag of interest
for i = 1:length(lagsOfInterest)
lag = lagsOfInterest(i);
% Calculate the cross-correlation for the current lag
if lag < 0
crossCorrelationValues(i) = T(1:end+lag) .* h(1-lag:end);
else
crossCorrelationValues(i) = T(1+lag:end) .* h(1:end-lag);
end
end
% Normalize the cross-correlation values by the autocorrelations at zero lag
crossCorrelationValues = crossCorrelationValues / sqrt(corrcoef(T,T) * corrcoef(h,h));
The “lagsOfInterestin the above code contains the specific lags to calculate the cross-correlation. The array, “crossCorrelationValues” is pre-allocated to store the cross-correlation values for each lag. Using a loop, the cross-correlation for each lag can be calculated by summing the products of the appropriately lagged elements of T and h. Normalizing will help to achieve results very similar to the “crosscorr” function results.
This approach avoids the need to calculate the cross-correlation for all lags and should significantly optimize the computation time for the specific requirements.
I hope this helps to resolve the query.
Thanks,
Abhimenyu

Products

Community Treasure Hunt

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

Start Hunting!