Convolution in Matlab with Code Examples

Updated on May 9, 2016 Discrete, Continues and Circular convolutions can be performed within seconds in Matlab® provided that you get hold of the code involved and a few other basic things.

This tutorial aims to:

• Demonstrate the necessary components of the code used to perform convolution in Matlab in a simplified manner.
• Visualize the results by plotting graphs of convoluted functions.

This Tutorial is arranged in the following sequence:

1. Continues convolution.
2. Discrete convolution.
3. Circular convolution.

Logic:

The simple concept behind your coding should be to:

1. Define two discrete or contineus functions.

2. Convolve them using the Matlab function 'conv()'

3. Plot the results using 'subplot()'.

Continues Convolution in Matlab

We will demonstrate this by using two continues functions:

1. A Pulse Waveform. Denoted by the 'square()' function. This is the input x(t) to the system.
2. Transient response of a capacitor. Denoted by equation of the form a*e1/RC*t. This could be considered the impulse response h(t) of the system.

Matlab Code for Convolution

tint = 0;
tfinal = 0.05;
tstep = 0.0005;

t = tint : tstep : tfinal;

x = 4 * square( 500*t, 50 ); //Use a function of your choice here.

subplot( 3, 1, 1);
plot( t, x);

h = 400 * exp( (-400*t) ); //Use a function of your choice.

subplot( 3, 1, 2);
plot( t, h);

t2 = 2*tint : tstep : tfinal*2; //Convoluted function requires a wider    range to be plotted completely.
y = conv( x, h) * tstep;

subplot( 3, 1, 3);
plot( t2, y);

Line 1-5: Define the range of values for the time axis.

Line 7: A square wave is initialized by using the Matlab function 'square()' it has an amplitude of 4, ω = 500 rad/s, and duty cycle of 50%.

Line 9: Subplot() partitions the output window to accommodate 3 plots on a single screen i.e It creates a table of 3 rows and 1 column(s) and then the last argument in subplot() selects 1st plot for further working.

Line 10: plots x(t).

Line 12-15: Impulse response of the system h(t) is defined. Subplot() selects 2nd graph and plots h(t) on it.

Line 18: A new range is defined for the resulting convoluted function.

Line 19-22: x(t) is finally convoluted with h(t) and then plotted over this new range. *tstep was added to adjust the x and y axis accordingly.

Discrete Convolution in Matlab

Performing discrete convolution in matlab is very simple and straightforward. You just have to define the discrete values of each function and then apply the function 'conv()' on both of these functions.

Matlab Code for Discrete Convolution

a = -1; //This is the starting value of time for x(t).
b = 0;  //Starting value of time for h(t).

x = [ 2 -1 1];
h = [ 3 2 1];

subplot( 3, 1, 1);
t = a : a+length(x)-1; //tstep is not required here.
stem( t, x);

subplot( 3, 1, 2);
t = b : b+length(h)-1;
stem( t, h);

y = conv( x, h);

subplot( 3, 1, 3);
t = a+b : a+b+length(y)-1;
stem( t, y);

Line 4-5: Define discrete values for x(t) and h(t).

Line 8, 12 & 18: Define the range for the time axis in the graphs to be plotted subsequently using stem() function (if plotting is not required you may omit these steps).

Circular Convolution in Matlab

Circular convolution can be performed in the following steps:

1. Take Discrete Fourier transform of two vectors.
2. Multiply the two transforms.
3. Take Inverse Discrete Fourier transform of the product and the result is the circular convolution of two vectors.

Matlab Code for Circular Convolution

// Define two vectors for circular convolution

x = [2 5 4 1];
y = [1 4 3];

// Zeropad vectrs uptill 4+3-1

xpad = [x zeros( 1, 6-length(x) )];
ypad = [y zeros( 1, 6-length(y) )];

// Multiply ffts of both vectors and take idft of product

ccirc = ifft( fft( xpad ).*fft( ypad ) );

// Now plot result

stem( ccirc, 'filled' )
ylim( [0 35] )