# Convolution in Matlab With Code Examples

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:

- Continues convolution.
- Discrete convolution.
- 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:

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

## Matlab Code for Convolution

**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.

## Recommended for you

**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

**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:

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

## Matlab Code for Circular Convolution

**© 2015 StormsHalted**