How can I use cross-correlation as a tool to align two signals in MATLAB?

M
Maximiliano · Mar 5, 2021 · 2.7K views
Question
I have two signals that have a similar behavior but are not aligned. I would like to use cross-correlation to align them.  
Expert Answer
Profile picture of Kshitij Singh
Kshitij Singh PhD Expert
Answered Aug 29, 2026
Correlation reaches a maximum value at the point where two  signals are maximally correlated. You may therefore use this value to align them. The following code provides an example of how this can be done using the XCORR function in the Signal Processing Toolbox (see attached file variable_file.mat):
 
 
load variable_file

X1=xcorr(s1,s2);   %compute cross-correlation between vectors s1 and s2

[m,d]=max(X1);      %find value and index of maximum value of cross-correlation amplitude

delay=d-max(length(s1),length(s2));   %shift index d, as length(X1)=2*N-1; where N is the length of the signals

figure,plot(s1)                                     %Plot signal s1

hold,plot([delay+1:length(s2)+delay],s2,'r');   %Delay signal s2 by delay in order to align them

grid on
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!