EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • 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
  • Login
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.

ADVERTISEMENT
Popular Course in this category
MATLAB Course Bundle - 5 Courses in 1 | 3 Mock Tests

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
ADVERTISEMENT
SPSS Course Bundle - 14 Courses in 1 | 5 Mock Tests
34+ Hours of HD Videos
14 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
63+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
HADOOP Course Bundle - 32 Courses in 1 | 4 Mock Tests
125+ Hour of HD Videos
32 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
INFORMATICA Course Bundle - 7 Courses in 1
47+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

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

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW