A mathematical way of combining two signals to form a new signal is known as Convolution. In matlab for convolution ‘conv’ statement is used. The convolution of two vectors, p, and q given as “a = conv( p,q )” which represents that the area of overlap under the points as p slides across q. Convolution is the most important technique in Digital Signal Processing. The direct calculation of the convolution can be difficult so to calculate it easily Fourier transforms and multiplication methods are used. Convolution is used in differential equations, statistics, image and signal processing, probability, language processing and so on.
The syntax for Convolution Matlabisas shown below:-
w = conv(u,v)
w = conv(u,v,shape)
How to Do Convolution Matlab?
For performing a convolution operation on matlab we follow following steps:-
- Step 1: Take an input signal and also define its length
- Step 2: Take an impulse response signal and defined its length
- Step 3: perform a convolution using a conv function on matlab
- Step 4: If we want to plot three signals we use a subplot and stem functions.
Examples of Convolution Matlab
Following are the examples are given below:
This example is about how to calculate the result of the convolution of two different signals in a matlab. For generating time duration we are taking it as 0 to 2 with a difference of 1 and this time duration we take in a t1 variable. Now we generate a frequency of the first signal as a 10 hertz this assign to fr1 and we generate a frequency of the second signal as a 15 hertz this assign to fr2. Now generate a 1st signal as y1 equals to sin of 2 * pi * fr1.* t1, where fr1 is 1st signal frequency and t1 is a time duration. Then we generate a 2nd signal as y2 equals to cos of 2 * pi * fr2.* t1, where fr2 is 2nd signal frequency and t1 is a time duration. Now we convolve both the signals y1 and y2 and we are going to save the result in variable Y convolution can be performed in the matlab using a command conv, convis a abbreviation of convolution that is the 1st 4 words of convolution conv of now place 1st signal name y1 and comma for separated place 2nd signal name y2. We put a clc at a beginning of the code to just clear the command window after running this code.
clc; clear all; close all; t1 = 0 : 1 : 2; fr1 = 10; fr2 = 15; y1 = sin (2 * pi * fr1 .* t1); y2 = cos (2 * pi * fr2 .* t1); conv (y1, y2)
In this example we perform the sum of the two signals, firstly we define an n1 variable as 0 to 7 with a difference of 1. Now we take a first signal in y1 variable as 1 2 3 1 2 3 4 5 this numbers are we take in square bracket and then we take h1, h1 is a impulse response. We take h1 equals to in square brackets 1 1 1 2 1 -1 1 1. Now convolution can be performed in the matlab using a command conv, conv is an abbreviation of convolution that is the 1st 4 words of convolution conv of now place 1st signal name y1 and comma for separated place 2nd signal name h1. And the convolution result we stored in X variable. For plotting a three signals, we 1st plot figure 1 in signal figure we plot a three signals using a subplot function. subplot(3,1,1) so 1st we plot a y1 w.r.t n1, so plotting a signal we use stem function, stem is used to plot a discrete time signal, so we take stem(n1, y1). Subplot(3,1,2) so 2nd we plot a h1 w.r.t n1, so plotting a signal we use stem function take stem(n1, h1). Subplot (3,1,3) so 3rd we plot a X w.r.t n1, so plotting a signal we use stem function take stem (n2, X). Here n2 is a length of convolution signal minus 1 because we start with a 0.
clc; clear all; close all; n1 = 0 : 1 : 7; y1 = [ 1 2 3 1 2 3 4 5 ]; h1 = [ 1 1 1 2 1 -1 1 1 ]; X = conv (y1, h1); n2 = 0 : length(X)-1; figure(1) subplot(3,1,1) stem(n1, y1) title('input (y(n))') subplot(3,1,2) stem(n1, h1) title('impulse response (h(n))'); subplot(3,1,3) stem(n2, X) title('output (x(n))');
Let us seen an example for convolution, 1st we take an x1 is equal to the 5 2 3 4 1 6 2 1 it is an input signal. Then we take impulse response in h1, h1 equals to 2 4 -1 3, then we perform a convolution using a conv function, we take conv(x1, h1, ‘same’), it perform convolution of x1 and h1 signal and stored it in the y1 and y1 has a length of 7 because we use a shape as a same. The full convolution would be of length, length(x1)+length(h1)-1, which in this example would be 11.
clc; clear all; close all; x1 = [5 2 3 4 1 6 2 1];h1 = [2 4 -1 3];y1 = conv(x1, h1,'same')