0
0
RosHow-ToBeginner · 3 min read

How to Perform Convolution of Two Signals: Simple Guide

To perform convolution of two signals, use the convolve operation which slides one signal over the other and sums the product at each step. This combines the signals to produce a third signal showing how one modifies the other.
📐

Syntax

The convolution of two discrete signals x[n] and h[n] is defined as:

y[n] = (x * h)[n] = Σ x[k] · h[n - k]

Here, y[n] is the output signal, x[k] is the first input signal, and h[n - k] is the second signal flipped and shifted. The summation runs over all valid k.

In Python with NumPy, you can use:

numpy.convolve(x, h, mode='full')

where x and h are arrays representing signals, and mode controls the size of the output.

python
import numpy as np

# Syntax for convolution
x = np.array([1, 2, 3])
h = np.array([0, 1, 0.5])
y = np.convolve(x, h, mode='full')
print(y)
Output
[0. 1. 2.5 4. 1.5]
💻

Example

This example shows how to convolve two simple signals using NumPy. The first signal x is [1, 2, 3], and the second signal h is [0, 1, 0.5]. The output is the combined signal after convolution.

python
import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2, 3])
h = np.array([0, 1, 0.5])

y = np.convolve(x, h, mode='full')

print('Convolved signal:', y)

# Plotting the signals
plt.stem(x, linefmt='b-', markerfmt='bo', basefmt='k-', label='x[n]')
plt.stem(h, linefmt='r-', markerfmt='ro', basefmt='k-', label='h[n]', linefmt_offset=0.2)
plt.stem(y, linefmt='g-', markerfmt='go', basefmt='k-', label='y[n] = x*h')
plt.legend()
plt.title('Convolution of Two Signals')
plt.xlabel('n')
plt.ylabel('Amplitude')
plt.show()
Output
Convolved signal: [0. 1. 2.5 4. 1.5]
⚠️

Common Pitfalls

  • Incorrect signal length: Using signals of very different lengths without understanding mode can cause confusion about output size.
  • Wrong mode choice: mode='full' returns the complete convolution, 'same' returns output the same size as the first signal, and 'valid' returns only points where signals fully overlap.
  • Signal orientation: Forgetting that convolution flips one signal can lead to errors; cross-correlation does not flip.

Example of wrong and right usage:

python
import numpy as np

x = np.array([1, 2, 3])
h = np.array([0, 1, 0.5])

# Wrong: expecting same length output but using 'full'
y_full = np.convolve(x, h, mode='full')
print('Full mode output length:', len(y_full))  # length 5

# Right: use 'same' to get output length same as x
y_same = np.convolve(x, h, mode='same')
print('Same mode output length:', len(y_same))  # length 3
print('Same mode output:', y_same)
Output
Full mode output length: 5 Same mode output length: 3 Same mode output: [1. 2.5 4. ]
📊

Quick Reference

TermDescription
x[n]First input signal (array)
h[n]Second input signal (array), flipped and shifted in convolution
y[n]Output signal after convolution
mode='full'Returns complete convolution, length = len(x) + len(h) - 1
mode='same'Output length same as first signal x
mode='valid'Only points where signals fully overlap, shorter output

Key Takeaways

Convolution combines two signals by flipping and sliding one over the other, summing products at each step.
Use numpy.convolve(x, h, mode) in Python to perform convolution easily.
Choose the correct mode ('full', 'same', 'valid') based on desired output length.
Remember convolution flips one signal; cross-correlation does not.
Check signal lengths and mode to avoid unexpected output sizes.