Can I use "xcorr" function with bootstrp function?

8 views (last 30 days)
Folks,
Assuming I have two column vectors (v1 and v2), I want to compute the cross correlations of new vectors v11 and v22 that have had their rows randomly selected with replacement. If one could inspect the resulting vectors v11 and v22, they would see that some rows are "empty" and others have one value, and others have more than one value. Then I want to run the cross-correlation function on these vectors. What would result would be a new cross-correlation vector that would the length of v1+v2+1. If I repeat this nBoot times, then I will get slightly different offsets to the maximum peak in the cross correlation function. This amounts to a measurement of uncertainty of the optimum shift between both vectors. I've read the instructions for "bootstrp" function. I am expecting this to work, but it doesn't give me what I expect:
se = bootstrp(1000,@xcorr,v1,v2);
I am trying to avoid writing the function myself. Can someone offer a solution to this that uses Matlab's built in functions or a function on the Matlab sharepoint?
Best regards,
Kris

Answers (1)

Manikanta Aditya
Manikanta Aditya on 3 Apr 2025
I see you're trying to use bootstrapping to estimate the uncertainty in the cross-correlation between two vectors. The 'bootstrp' function in MATLAB can indeed be used for this purpose, but it requires a bit of customization to handle the random selection with replacement and the subsequent cross-correlation calculation.
You need to create a function that generates new vectors v11 and v22 by randomly selecting rows from v1 and v2 with replacement. Use the xcorr function to compute the cross-correlation of the new vectors. Use the bootstrp function to repeat the process nBoot times and collect the results.
Refer to the following code to understand better:
function se = bootstrap_xcorr(v1, v2, nBoot)
% Initialize the array to store the cross-correlation results
se = zeros(nBoot, length(v1) + length(v2) - 1);
for i = 1:nBoot
% Randomly select rows with replacement
idx1 = randi(length(v1), length(v1), 1);
idx2 = randi(length(v2), length(v2), 1);
v11 = v1(idx1);
v22 = v2(idx2);
% Compute the cross-correlation
se(i, :) = xcorr(v11, v22);
end
end
% Example usage
v1 = randn(100, 1); % Example vector 1
v2 = randn(100, 1); % Example vector 2
nBoot = 1000; % Number of bootstrap samples
se = bootstrap_xcorr(v1, v2, nBoot);
Refer to the following documentations:
I hope this helps.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!