Convolution Shape (full/same/valid) in MATLAB
In digital signal processing and image processing, the convolution is a crucial mathematical operation. The primary function of the convolution is to combine two signals to produce a resulting signal. The resulting signal specifies the interaction between the two signals that we combined.
In MATLAB, we can perform the convolution of two signals by using a built−in function ‘conv()’. This MATLAB function performs the convolution by using a discrete approximation of the integration. For this, it uses discrete samples of the input signals.
语法
The following is the general syntax for the ‘conv()’ function,
a = conv(x, y, shape);
Here, ‘x’ and ‘y’ are the two input signals on which the convolution will be performed, ‘a’ stores the result of the convolution, i.e. the resulting signal, and the parameter ‘shape’ specifies the shape of the convoluted output signal.
In MATLAB, we can specify three shapes of convolution namely, ‘full’, ‘same’, and ‘valid’.
Let us now discuss these three convolution shapes of the output signal in detail.
Convolution Shapes
Full Convolution
To perform the full convolution, ‘full’ is specified as the shape parameter. This option is used when we want to find the convolution of the entire signal.
The important point to note about the output signal obtained by full convolution is that it is generally longer than both input signals.
In general, the length of the output signal is given by,
$$\mathrm{l_{out}\:=\:(l_1+l_2)− 1}$$
Where, lout is the length of the output signal, l1 and l2 are lengths of the input signals.
The general syntax for the ‘conv’ function to perform full convolution is as follows:
a = conv(x, y, 'full');
The following MATLAB program demonstrates the implementation of full convolution ("full" option).
示例
% MATLAB program to demonstrate the full convolution % Define two input signals x = [2 4 6 8]; % Input signal h = [0.7 0.7]; % Impulse response of a system "y = h" % Perform full convolution C = conv(x, h, 'full'); % Display the result disp('Full Convolution of Signals x and h is:'); disp(C);
输出
Full Convolution of Signals x and h is: 1.4000 4.2000 7.0000 9.8000 5.6000
Explanation
In this MATLAB code, we start by defining two input signals namely, an input signal ‘x’ and an impulse response of a system ‘h’. Then, we perform full convolution of ‘x’ and ‘h’ by using the ‘conv()’ function with ‘full’ option. Finally, we use the ‘disp()’ function to display the output signal.
From the output signal, it can be observed that the length of the output signal is longer that that of the input signal.
Same Convolution
The same convolution of two signals is calculate by using the ‘same’ option in the ‘conv’ function as the shape parameter.
In the case of same convolution, the length of the output signal remains same as that of the input signal. However, the output signal is centered with respect to the input signal.
The syntax of the ‘conv()’ function with ‘same’ option to perform the same convolution is as follows:
a = conv(x, y, 'same');
Now, let us consider a MATLAB example to understand the implementation of the same convolution.
示例
% MATLAB program to demonstrate the same convolution % Define two input signals x = [2 4 6 8]; % Input signal h = [0.7 0.7]; % Impulse response of a system % Perform the same convolution C = conv(x, h, 'same'); % Display the result disp('Same Convolution of Signals x and h is:'); disp(C);
输出
Same Convolution of Signals x and h is: 4.2000 7.0000 9.8000 5.6000
Explanation
In the above MATLAB program, first we define two input signals namely, an input signal ‘x’ and an impulse response of a system ‘h’. Then, we perform same convolution of ‘x’ and ‘h’ by using the ‘conv()’ function with ‘same’ option as shape parameter. Finally, we use the ‘disp()’ function to display the output signal.
From the output signal, it can be observed that the length of the output signal is same as that of the input signal.
Valid Convolution
In MATLAB, the valid convolution of two signals is performed by using ‘valid’ option as the shape parameter in the ‘conv()’ function.
The conv() function with ‘valid’ option performs the convolution of two input signals only when they completely overlap each other, i.e. have no zero padding.
In this case, the length of the output signal is usually shorter than that of the input signals.
The following is the general syntax of the ‘conv()’ function with ‘valid’ option to perform the valid convolution:
a = conv(x, y, 'valid');
The following MATLAB program illustrates the implementation of code to perform valid convolution of signals.
示例
% MATLAB program to demonstrate the valid convolution % Define two input signals x = [2 4 6 8]; % Input signal h = [0.7 0.7]; % Impulse response of a system % Perform the valid convolution C = conv(x, h, 'valid'); % Display the result disp('Valid Convolution of Signals x and h is:'); disp(C);
输出
Valid Convolution of Signals x and h is: 4.2000 7.0000 9.8000
Explanation
In this MATLAB program, we start by defining two input signals namely, an input signal ‘x’ and an impulse response of a system ‘h’. Next, we perform valid convolution of ‘x’ and ‘h’ by using the ‘conv()’ function with ‘valid’ option as the shape parameter. Finally, we use the ‘disp()’ function to display the output signal.
From the output signal, it can see that the length of the output signal is shorter than that of the input signal.
Conclusion
This is how, we can perform different types of convolutions on signals with different shapes. The above sections of this article explain the three convolution shapes namely ‘full’, ‘same’, and ‘valid’ with the help of example MATLAB programs.