EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab xcorr
 

Matlab xcorr

Priya Pedamkar
Article byPriya Pedamkar

Updated March 3, 2023

Matlab-xcorr

 

 

Introduction to Matlab xcorr

MATLAB’s xcorr method can be used to compute the cross-correlation of 2 time-sequences which are discrete in nature. The main aim of computing cross-correlation is to enable the use of a part of any incoming signal or beam to examine the transient evolution that is being produced by the second part. It can also be used to get the additional information of the incoming signal or beam itself which is otherwise tricky to measure.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

  1. C = xcorr (a, b) is used to return the cross-correlation between two time sequences (discrete in nature).
  2. The method ‘xcorr’ will compute the similarity between vector ‘a’ & lagged copies of vector ‘b’
  3. C = xcorr (___, maximumlag) is used to limit the maximum lag as per the requirement

Let us now understand how to compute the cross-correlation in MATLAB using ‘xcorr’ method:

Examples of Matlab xcorr

Let us discuss examples of Matlab xcorr.

Example #1

In this example, we will use the xcorr method to compute the cross-correlation between 2 vectors. The steps to be followed for this example are:

  1. Create a vector ‘a’
  2. Create another vector ‘b’, with a shift of required units from ‘a’
  3. Pass the above 2 vectors as arguments to the xcorr method
  4. Use the stem method to plot the output

Code:

x = 0 : 20

[Initializing the size of the vector]

a = 0.108.^x;

[Initializing the vector ‘a’]

b = circshift(a, 10);

[Initializing the vector ‘b’. Here we have used ‘circshift’ method to get a vector which is shifted by 10 elements from ‘a’]

[c, lag] = xcorr(a, b);

[Using the xcorr method to get the cross correlation]

stem(lag, c)

[Using stem method to plot the output]

This is how our input and output will look like in Matlab command window:

Input:

Matlab xcorr 1

Output:

Matlab xcorr 2

As we can see in the OUTPUT, largest spike comes at -10; i.e. when the elements of a & b match exactly, which is the same as expected by us.

Example #2

Let us take another example where we will use xcorr method to compute the cross-correlation between 2 vectors. The steps to be followed for this example are:

  1. Create a vector ‘a’
  2. Create another vector ‘b’, with a shift of required units from ‘a’
  3. Pass the above 2 vectors as arguments to the xcorr method
  4. Use the stem method to plot the output

Code:

x = 0 : 40

[Initializing the size of the vector]

a = 0.48.^x;

[Initializing the vector ‘a’]

b = circshift(a, 8);

[Initializing the vector ‘b’. Here we have used ‘circshift’ method to get a vector which is shifted by 8 elements from ‘a’]

[c, lag] = xcorr(a, b);

[Using the xcorr method to get the cross correlation]

stem(lag, c)

[Using stem method to plot the output]

This is how our input and output will look like in Matlab command window:

Input:

Matlab xcorr 3

Output:

Matlab xcorr 4

As we can see in the OUTPUT, largest spike comes at -8; i.e. when the elements of a & b match exactly, which is the same as expected by us.

In the above 2 examples there was no limit on the lag. Next, we will learn how to define a lag in the xcorr method

Example #3

In this example, we will use xcorr method to compute the cross-correlation between 2 vectors and will limit the maximum lag. The steps to be followed for this example are:

  1. Create a vector ‘a’
  2. Create another vector ‘b’, with a shift of required units from ‘a’
  3. Pass the above 2 vectors as arguments to the xcorr method
  4. In addition to the above 2 arguments, pass a 3rd argument which will specify the maximum lag
  5. Use the stem method to plot the output

Code:

x = 0 : 50

[Initializing the size of the vector]

a = 0.90.^x;

[Initializing the vector ‘a’]

b = circshift(a, 5);

[Initializing the vector ‘b’. Here we have used ‘circshift’ method to get a vector which is shifted by 5 elements from ‘a’]

[c, lag] = xcorr(a, b, 15);

[Using the xcorr method to get the cross correlation. Please note that, we have passed a 3rd argument ‘15’, which represents the maximum lag that we need]

stem(lag, c)

[Using stem method to plot the output]

This is how our input and output will look like in Matlab command window:

Input:

example 3

Output:

example 3-1

As we can see in the output, the signal is restricted with a maximum lag of 15 as expected by us.

Example #4

Let us take another example where we will use xcorr method to compute the cross-correlation between 2 vectors and will limit the maximum lag. The steps to be followed for this example are:

  1. Create a vector ‘a’
  2. Create another vector ‘b’, with a shift of required units from ‘a’
  3. Pass the above 2 vectors as arguments to the xcorr method
  4. Pass a 3rd argument which will specify the maximum lag
  5. Use the stem method to plot the output

Code:

x = 0 : 100

[Initializing the size of the vector]

a = 0.200.^x;

[Initializing the vector ‘a’]

b = circshift(a, 10);

[Initializing the vector ‘b’. Here we have used ‘circshift’ method to get a vector which is shifted by 10 elements from ‘a’]

[c, lag] = xcorr(a, b, 20);

[Using the xcorr method to get the cross correlation. Please note that, we have passed a 3rd argument ‘20’, which represents the maximum lag that we need]

stem(lag, c)

[Using stem method to plot the output]

This is how our input and output will look like in Matlab command window:

Input:

example 4

Output:

example 4-1

As we can see in the output, the signal is restricted with a maximum lag of 20 as expected by us

Conclusion

The xcorr method can be used in MATLAB to compute the cross-correlation of 2 time-sequences which are discrete in nature. If required, we can limit the maximum lag as per our requirement.

Recommended Articles

This is a guide to Matlab xcorr. Here we discuss the Introduction, How to compute the cross-correlation in Matlab along with the examples for better understanding. You may also have a look at the following articles to learn more –

  1. Matlab Forms
  2. Matlab plot title
  3. Matlab fplot()
  4. Matlab Syms

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW