Difference between Convolution and Correlation in MATLAB

In mathematics, there are two operations namely, convolution and correlation used to manipulate two functions to produce a third function. Both operations play a vital role in various fields such as digital signal processing, digital image analysis processing, solving complex mathematical problems and many more.

Convolution and correlation are also used to extract information and patterns in the field of data science. However, there are various significant differences between convolution and correlation. The most fundamental difference between convolution and correlation is that convolution is a mathematical operation used to combine two functions to produce a new function, while correlation is a mathematical operation used to measure similarity between two functions.

This tutorial is primarily meant for explaining the significant differences between convolution and correlation in MATLAB. But before going to discuss about the differences, let us first get an overview of convolution and correlation in MATLAB individually.

What is Convolution?

Convolution is a mathematical operation used to combine two functions or signals to produce a third function or signal. It is a widely used operation in the field of signal processing, signal filtering, image analysis, computer vision. It is also used to extract information from an image.

Convolution of two signals or functions can be performed by using MATLAB. MATLAB provides a built-in function 'conv' to perform convolution. The 'conv' function accepts two input vectors as arguments and gives their linear convolution as the result.

Syntax

The 'conv' function uses the following syntax in MATLAB,

C = conv(a, b);


Example

The following MATLAB program demonstrates how to use the 'conv' function to compute convolution of two functions.

% MATLAB code to perform convolution
% Define two sample functions
a = [1, 2, 3, 4, 5, 6];
b = [1, 0.75, 0.5, 0.5, 0.25, 0.5];

% Perform convolution of a and b
C = conv(a, b);

% Display the output
disp('Convolution of a and b is:');
disp(C);


Output

Convolution of a and b is:
Columns 1 through 8:

1.0000    2.7500    5.0000    7.7500   10.7500   14.2500   10.7500    8.0000

Columns 9 through 11:

6.2500    4.0000    3.0000


Hence, this is all about basics of convolution in MATLAB. Now, let us briefly discuss about the correlation.

What is Correlation?

Correlation is a mathematical operation used to determine the measure of similarity between two mathematical functions or signals. This operation compares two signals at their different value point and provide a pattern or similarity between the signals.

Syntax

Similar to convolution, correlation can be also performed using MATLAB. For this, the 'xcorr' function, a built-in function in MATLAB used. The 'xcorr' function calculates the cross-correlation or auto-correlation of two input functions or signals.

C = xcorr(a, b);


Example

The following example program in MATLAB demonstrates the use of 'xcorr' function to compute cross-correlation.

% MATLAB code to calculate correlation
% Define two sample signals
a = [1, 2, 3, 4, 5, 6];
b = [1, 0.75, 0.5, 0.5, 0.25, 0.5];

% Calculate correlation between a and b
C = xcorr(a, b);

% Display the output
disp('Correlation between a and b is:');
disp(C);


Output

Correlation between a and b is:
Columns 1 through 9

0.5000    1.2500    2.5000    4.2500    6.7500   10.2500   10.2500   11.5000   10.7500

Columns 10 through 11

9.5000    6.0000


After getting a brief overview of convolution and correlation, let us now discuss the differences between them.

Difference Between Convolution and Correlation

The following table highlights all the significant differences between convolution and correlation −

Parameter

Convolution

Correlation

Definition

Convolution is a mathematical operation used to combine two signals or function to produce a third signal or function.

Correlation is a mathematical operation used to measure or quantify the similarities and relationship between two signals.

MATLAB function used

Convolution is performed by using the 'conv ()' function in MATLAB.

Correlation is calculated by using the 'xcorr' function in MATLAB.

Purpose of operation

The primary purpose of convolution is to perform tasks, such as signal transformation and modification.

The main purpose of correlation is to quantify the similarities between two functions.

Output

The output of convolution of two functions or signals is a new function or signal which is a combination of input functions or signals.

The output of correlation is a value that represents the degree of similarity between two functions or signals.

Range of output value

In the case of convolution, the range of value of output depends on the input signals or functions.

The range of value of output of correlation is between -1 and 1. If the output of correlation is -1, it indicates a perfect negative correlation, if it 1, the correlation is perfect positive correlation, and if it is 0, there is no correlation between functions.

Size of output

The size of convolution output is equal to the sum of sizes of inputs minus 1.

The size of the correlation output is double of the sizes of inputs minus 1.

Symmetry preservation

Convolution preserves the symmetry.

Correlation does not preserve symmetry.

Suitable for template matching

Convolution cannot be used to match template of the signals due to its symmetry preservation property.

Correlation can be used for template matching.

Mathematical operation involved

Convolution involves the integration of the product of the input functions with one function shifted.

Correlation simply involves the integration of the product of the input functions.

Applications

Convolution is widely used in the field of digital image processing, computer vision, data science, signal transformation, and more.

Correlation is mainly used for similarity measurement between two signals, signal comparison, template matching, pattern and relationship recognition, data analysis, etc.

Conclusion

Both convolution and correlation are significant mathematical operations play a vital role in the various fields, such as data analysis, digital signal processing, pattern recognition, etc. We can perform convolution and correlation of two function by using MATLAB.

In MATLAB, there are two built-in functions namely, 'conv' and 'xcorr' used to calculate convolution and correlation respectively. The most significant difference between convolution and correlation is that the convolution combines two input signals and produces a third signal as output, while the correlation measures the similarity between two input signals.

Updated on: 06-Sep-2023

110 Views