How overlap time window in cross-correlation signal process?

9 views (last 30 days)
Dears,
I am processing some data and obtaing results from the time delay between the two signals, using a cross-correlation signal code prepared by myself from some editions on example give in: https://www.mathworks.com/help/signal/ug/align-signals-using-cross-correlation.html
I use the following part to process the cross correlatio:
%calculating total number of segments
total_segments_st1=size(segments_selected__st1,2);
%calculate cross correlation
k=0;
for i=1:total_segments_st1
k=k+1;
%calculating delay using cross-correlation
[C_n12(:,k),lag_n12(:,k)]=xcorr(segments_selected__st1(:,i),segments_selected__st2(:,i));
C_n12(:,k) = C_n12(:,k)/max(C_n12(:,k));
%identifying the max. amplitude of the two correlations
[M_n12(:,k),I_n12(:,k)] = max(C_n12(:,k));
t_n12(:,k) = abs(lag_n12(I_n12(:,k)));
end
difference_n12=int2str(t_n12);
delay_n12=abs(str2num(difference_n12)*delta_st2); %final arrival time delay in seconds
segments_selected__st1 and segments_selected__st2 are the two signals containing six windows of 20 seconds. In other words, they are 5000x6 matrix (5000 = 20*250 samples per second). total_segments_st1 have the total number of windows (6), then I run the cross-correlation separately one by one of the 20 s time windows.
My question is: How can I do if I wanna apply a 5 seconds (5*250 = 1200 points) overlap between each time windows? For example, the process start the cross correlation in the first time windows of 5 seconds, the, the next cross correlation will start in the last 5 seconds of the first time window. The third cross correlation will start at the last 5 of that second time window, and from then on it continues until complete all total_segments.
Any suggestion will be welcomed!
thanks,
Guilherme

Answers (1)

Paras Gupta
Paras Gupta on 31 Aug 2023
Hi Guilherme,
I understand that you would like to cross-correlate two signals with an overlap of 5 seconds between the six windows of the signal. The overlap can be considered by slicing the signal windows into the new segments using array indexing.
Please refer the code below to achieve the same:
% Assign example values to variables
segments_selected__st1 = randn(5000, 6);
segments_selected__st2 = randn(5000, 6);
delta_st2 = 0.004; % Example value for delta_st2
% Calculate total number of segments
total_segments_st1 = size(segments_selected__st1, 2);
window_size = 5000;
overlap = 1200;
% New number of segments after considering the overlap
new_total_segments = 7;
C_n12 = zeros(2 * size(segments_selected__st1, 1) - 1, new_total_segments);
lag_n12 = zeros(size(C_n12));
M_n12 = zeros(1, new_total_segments);
I_n12 = zeros(1, new_total_segments);
t_n12 = zeros(1, new_total_segments);
% index to slice the signal windows
index = window_size - overlap;
% Calculate cross-correlation and delay for each segment
for i = 1:new_total_segments
if i == 1 % first segment
[C_n12(1:2*overlap-1, i), lag_n12(1:2*overlap-1, i)] = xcorr(segments_selected__st1(1:overlap, i), segments_selected__st2(1:overlap, i));
elseif i == new_total_segments % last segment
[C_n12(1:2*overlap-1, i), lag_n12(1:2*overlap-1, i)] = xcorr(segments_selected__st1(index + 1:end, i-1), segments_selected__st2(index + 1:end, i-1));
else % rest of the segments
temp1 = [segments_selected__st1(index+1:end, i-1); segments_selected__st1(1:index, i)];
temp2 = [segments_selected__st2(index+1:end, i-1); segments_selected__st2(1:index, i)];
[C_n12(:, i), lag_n12(:, i)] = xcorr(temp1, temp2);
end
C_n12(:, i) = C_n12(:, i) / max(C_n12(:, i));
% Identifying the max. amplitude of the two correlations
[M_n12(:, i), I_n12(:, i)] = max(C_n12(:, i));
t_n12(:, i) = abs(lag_n12(I_n12(:, i), i));
end
difference_n12 = int2str(t_n12);
delay_n12 = abs(str2num(difference_n12) * delta_st2);
The segments other than the first and the last will consist of equal number of points and can be indexed similarly. The first and the last segment will consist of lesser number of points and thus have to be indexed separately.
Hope this helps.

Community Treasure Hunt

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

Start Hunting!