May 10, 2024

Clear and Graphical Explanation of Signal Convolution with MATLAB Implementation – Digital Signal Processing Tutorial


In this digital signal processing and control engineering tutorial, we provide a clear and graphical explanation of the convolution operator which is also known as the convolution sum or simply as convolution. Signal convolution is a fundamental operation in signal processing, control theory, machine learning, and time series prediction. Consequently, it is of paramount importance to understand the mechanism behind signal convolution and to properly understand how to compute signal convolution in MATLAB. All this is explained in this tutorial. The YouTube page accompanying this tutorial is given below.

Definition of Convolution Sum Operator

Let x[n] and h[n] be two scalar discrete-time signals, and let n be a discrete-time instant. Then, convolution of x[n] and h[n] is defined as follows

(1)   \begin{align*}y[n]=\sum_{k=-\infty}^{+\infty}x[k]h[n-k]\end{align*}

where y[n] is another discrete-time signal that is determined by the convolution. Often in literature, the convolution (convolution sum operator) is denoted by the start operator “\ast“. Consequently, convolution can be written like this

(2)   \begin{align*}y[n]=x[n]\ast h[n]= \sum_{k=-\infty}^{+\infty}x[k]h[n-k]\end{align*}

In the theory of discrete dynamical systems, the signal h[n] is often the impulse response of a linear dynamical time-invariant system, and x[n] is the discrete-time input signal. In that case, convolution (2) defines a response of the discrete-time system to the input x[n]. That is, the response of the system is completely determined by the impulse response and the input signal. This is a very important fact in linear system theory.

In many cases, the signal x[n] is zero for negative values of n. In that case, we can expand the sum as follows

(3)   \begin{align*}y[n] & =x[0]h[n]+x[1]h[n-1]+x[2]h[n-2]+x[3]h[n-3]+\ldots  \end{align*}

Let us write this sum until the term x[5]

(4)   \begin{align*}y_{5}[n] & =x[0]h[n]+x[1]h[n-1]+x[2]h[n-2]+ \\& x[3]h[n-3]+x[4]h[n-4]+x[5]h[n-5]\end{align*}

The best approach for understanding convolution is to consider an example. Consequently, let us consider the following example.

Example demonstrating convolution: Let the signals h[n] and x[n] be defined as follows

(5)   \begin{align*}h[n] = \left\{ \begin{array}{ll} 3 & , \;\; n=0 \\  2 &, \;\; n=1 \\ 1 &, \;\; n=2 \\ 0 &, \;\; \text{for all other integer} \;\; n \end{array} \right.\end{align*}

(6)   \begin{align*}x[n] = \left\{ \begin{array}{ll} 1 & , \;\; n=0 \\  2 &, \;\; n=1 \\ 3 &, \;\; n=2 \\ 0 &, \;\; \text{for all other integer} \;\; n  \end{array} \right.\end{align*}

Compute the convolution sum

(7)   \begin{align*}y[n]=\sum_{k=-\infty}^{+\infty}x[k]h[n-k]\end{align*}

and give a graphical interpretation and derive a graphical method for computing the convolution sum.

Solution:

The figure below shows a graphical representation of two signals

Figure 1: Graphical representation of two signals.

First, taking into account that x[n]=0 for negative values of n, the sum (7) takes the following form

(8)   \begin{align*}y[n] & =\sum_{k=0}^{+\infty}x[k]h[n-k]  \\y[n] & =x[0]h[n]+x[1]h[n-1]+x[2]h[n-2]+x[3]h[n-3]+\ldots  \end{align*}

Now, taking into account that x[n]\ne 0 only for n=0,1,2, the convolution sum (8) takes the following form

(9)   \begin{align*}y[n]=x[0]h[n]+x[1]h[n-1]+x[2]h[n-2]\end{align*}

Let us now represent this sum for different values of n. For n=0, we have

(10)   \begin{align*}y[0] &=x[0]h[0]+x[1]h[-1]+x[2]h[-2] \\& = x[0]h[0]+x[1]0+x[2]0 \\& =x[0]h[0] =1\cdot 3=3\end{align*}

For n=1, we have

(11)   \begin{align*}y[1]& =x[0]h[1]+x[1]h[0]+x[2]h[-1] \\& =x[0]h[1]+x[1]h[0]+x[2]0 \\& =x[0]h[1]+x[1]h[0] =1\cdot 2 +2\cdot 3 = 8 \end{align*}

For n=2, we have

(12)   \begin{align*}y[2]& =x[0]h[2]+x[1]h[1]+x[2]h[0]\\& =1 \cdot 1 +2 \cdot 2+3 \cdot 3 = 14\end{align*}

For n=3, we have

(13)   \begin{align*}y[3]& =x[0]h[3]+x[1]h[2]+x[2]h[1]\\& =x[0]\cdot 0 +x[1]h[2]+x[2]h[1] \\& = x[1]h[2]+x[2]h[1]  =2 \cdot  1+ 3 \cdot 2 = 8 \end{align*}

For n=4, we have

(14)   \begin{align*}y[4] & =x[0]h[4]+x[1]h[3]+x[2]h[2] \\& =x[0]\cdot 0+x[1]\cdot 0+x[2]h[2] \\& = x[2]h[2] =3\cdot 1 = 3\end{align*}

To give a graphical interpretation of the convolution sum, let us analyze again the derived equations

(15)   \begin{align*}& n=0,\;\; y[0]=x[0]h[0]+x[1]h[-1]+x[2]h[-2]  \\& n=1,\;\; y[1]=x[0]h[1]+x[1]h[0]+x[2]h[-1]  \\& n=2,\;\; y[2]=x[0]h[2]+x[1]h[1]+x[2]h[0]  \\& n=3,\;\; y[3]=x[0]h[3]+x[1]h[2]+x[2]h[1] \\& n=4,\;\; y[4]=x[0]h[4]+x[1]h[3]+x[2]h[2]\end{align*}

We can observe the following

  1. In the sum terms, the position of the sequence x[n] stays the same as n is increased.
  2. In the sum terms, the sequence h[n] is shifted in time at corresponding positions in the sum.

This means that in the graphical interpretation of the convolution sum, the sequence x[n] is fixed, and the sequence h[n] is shifted. However, the sequence h[n] has to be reversed or reflected with respect to the vertical axis. This is explained in the sequel. For that purpose, and for clarity, let us again write the general formula (9) as follows

(16)   \begin{align*}y[n]=x[0]h[n]+x[1]h[n-1]+x[2]h[n-2]=\sum_{k=0}^{2}x[k]h[n-k]\end{align*}

From this formula, we have

(17)   \begin{align*}& n=0,\;\; y[0]=x[0]h[0-0]+x[1]h[0-1]+x[2]h[0-2]=\sum_{k=0}^{2}x[k]h[0-k]  \\& n=1,\;\; y[1]=x[0]h[1-0]+x[1]h[1-1]+x[2]h[1-2]=\sum_{k=0}^{2}x[k]h[1-k]   \\& n=2,\;\; y[2]=x[0]h[2-0]+x[1]h[2-1]+x[2]h[2-2]=\sum_{k=0}^{2}x[k]h[2-k]  \\& n=3,\;\; y[3]=x[0]h[3-0]+x[1]h[3-1]+x[2]h[3-2]=\sum_{k=0}^{2}x[k]h[3-k]  \\& n=4,\;\; y[4]=x[0]h[4-0]+x[1]h[4-1]+x[2]h[4-2]=\sum_{k=0}^{2}x[k]h[4-k]\end{align*}

In these sums, we can think of the sequence h[n-k], n=0,1,2,3,4, as the sequence h[-k] that is shifted (delayed) for n time steps.

Let us further elaborate on how the sequence h[-k] is constructed from the sequence h[k] or h[n]. Let us denote the sequence h[-k] by h_{1}[k]. That is,

(18)   \begin{align*}h_{1}[k]=h[-k]\end{align*}

Taking into account the definition of the sequence h[n], for n=-k, the sequence h_{1}[k] is

(19)   \begin{align*}k=0,\; => \; h_{1}[0]=h[0]=3 \\k=-1,\; => \;h_{1}[-1]= h[-(-1)]=h[1]=2 \\k=-2,\; => \; h_{1}[-2]= h[-(-2)]=h[2]=1 \\\text{for all other values of} \;\; k \;\; =>\; h_{1}[k]=h[-k]=0\end{align*}

This is actually the original sequence h[n] that is mirrored or reflected with respect to the vertical axis n=0. This sequence is illustrated in the figure below.

Figure 2: Sequence h[-k].

If we delay or shift this sequence in time, we will obtain the following sequences for different values of the time delay:

Figure 3: Sequence h[-k] delayed for 0, 1, 2, and 3 time steps.

These shifted or delayed sequences constructed on the basis of h[-k], will help us to graphically interpret the convolution sum.

Let us start with n=0

(20)   \begin{align*}n=0,\;\; y[0]=x[0]h[0-0]+x[1]h[0-1]+x[2]h[0-2] =x[0]h[0]\end{align*}

This sum has the graphical interpretation given in the figure below.

Figure 4: Graphical interpretation of y[0].

We only multiply the terms of the sequences x[k] and h[-k] that are in the dashed rectangle.

For n=1, we have

(21)   \begin{align*} n=1,\;\; y[1]=x[0]h[1-0]+x[1]h[1-1]+x[2]h[1-2]= x[0]h[1]+x[1]h[0] \end{align*}

This sum has the graphical interpretation given in the figure below.

Figure 5: Graphical interpretation of y[1].

We delay the sequence h[-k] to obtain h[1-k], and we only multiply the terms of the sequences x[k] and h[1-k] that are in the dashed rectangle.

For n=2, we have

(22)   \begin{align*} n=2,\;\; y[2]=x[0]h[2-0]+x[1]h[2-1]+x[2]h[2-2]=x[0]h[2]+x[1]h[1]+x[2]h[0]\end{align*}

This sum has the graphical interpretation given in the figure below.

Figure 6: Graphical interpretation of y[2].

We delay the sequence h[-k] to obtain h[2-k], and we only multiply the terms of the sequences x[k] and h[2-k] that are in the dashed rectangle.

For n=3, we have

(23)   \begin{align*} n=3,\;\; y[3]=x[0]h[3-0]+x[1]h[3-1]+x[2]h[3-2]=x[1]h[2]+x[2]h[1]\end{align*}

This sum has the graphical interpretation given in the figure below.

Figure 7: Graphical interpretation of y[3].

We delay the sequence h[-k] to obtain h[3-k], and we only multiply the terms of the sequences x[k] and h[3-k] that are in the dashed rectangle.

For n=4, we have

(24)   \begin{align*} n=4,\;\; y[4]=x[0]h[4-0]+x[1]h[4-1]+x[2]h[4-2]=x[2]h[2]\end{align*}

This sum has the graphical interpretation given in the figure below.

Figure 8: Graphical interpretation of y[4].

We delay the sequence h[-k] to obtain h[4-k], and we only multiply the terms of the sequences x[k] and h[4-k] that are in the dashed rectangle.

Convolution in MATLAB

The following MATLAB script will compute convolution

x = [1 2 3];
h = [3 2 1];
y = conv(x,h,'full')

The output is

y =

     3     8    14     8     3

We used the MATLAB function conv() to compute the convolution. The MATLAB script is self-explanatory.