Experiment 4
Experiment 4
Objective:
Software Used:
MATLAB, Python.
Theory:
Convolution is a mathematical operation used to combine two sequences or signals to produce a third
sequence. It expresses the way in which the shape of one signal is modified by another. In signal
processing, convolution helps in understanding how the output of a system (or filter) is affected by an
input signal.
The convolution operation for discrete-time signals can be described by the following formula:
where:
{∞ } x [ k ]
y [ n]= ∑ x [k ]⋅h [ n−k ]
{ k=−∞ }
For practical cases where sequences are finite, the formula simplifies to:
{ M −1} x [ k ]
y [ n]= ∑ x [k ]⋅h [ n−k ]
{k=0 }
Linear Convolution:
Linear convolution combines two sequences by summing the products of one sequence and a shifted
version of the other. The output sequence length is N + M - 1, where N and M are the lengths of the two
sequences.
Given sequences:
A = [1, 2, 3, 4] and B = [4, 3, 2, 1]
Calculations:
y[0]=1⋅4=4
y[1]=1⋅3+2⋅4=11
y[2]=1⋅2+2⋅3+3⋅4=20
y[3]=1⋅1+2⋅2+3⋅3+4⋅4=30
y[4]=2⋅1+3⋅2+4⋅3=20
y[5]=3⋅1+4⋅2=11
y[6]=4⋅1=4
Circular Convolution:
Circular convolution is used for periodic sequences where both sequences are considered to repeat
indefinitely. It wraps around the indices when summing the products, and the length of the output
sequence is the same as the input sequence length.
Calculations:
z[0]=5⋅8+6⋅5+7⋅6+8⋅7=168
z[1]=5⋅7+6⋅8+7⋅5+8⋅6=166
z[2]=5⋅6+6⋅7+7⋅8+8⋅5=168
z[3]=5⋅5+6⋅6+7⋅7+8⋅8=174
When the sequences have different lengths, zero padding is used to extend the shorter sequence to
match the length of the longer sequence. This avoids wrap-around effects in circular convolution and
allows for a more accurate representation.
Steps:
1. Zero-Pad: Extend the shorter sequence with zeros to match the length of the longer sequence.
2. Calculate Circular Convolution: Apply the circular convolution formula on the zero-padded
sequences.
Given sequences:
Calculations:
r[0]=2⋅8+4⋅0+6⋅4+8⋅6=88
r[1]=2⋅6+4⋅8+6⋅0+8⋅4=76
r[2]=2⋅4+4⋅6+6⋅8+8⋅0=80
r[3]=2⋅0+4⋅4+6⋅6+8⋅8=116
A = [1, 2, 3, 4];
B = [4, 3, 2, 1];
% Linear Convolution
figure;
stem(lin_conv, 'filled');
title('Linear Convolution');
xlabel('n');
ylabel('Amplitude');
grid on;
X = [5, 6, 7, 8];
Y = [8, 7, 6, 5];
% Circular Convolution
figure;
stem(circ_conv, 'filled');
title('Circular Convolution');
xlabel('n');
ylabel('Amplitude');
grid on;
PYTHON CODE / OUTPUT
import numpy as np
# Linear Convolution
A = [1, 2, 3, 4]
B = [4, 3, 2, 1]
linear_conv = np.convolve(A, B)
# Circular Convolution
x = [5, 6, 7, 8]
y = [8, 7, 6, 5]
N = len(x)
X = np.fft.fft(x)
Y = np.fft.fft(y)
result = np.fft.ifft(X * Y)
return np.real_if_close(result)
circular_conv = circular_convolution(x, y)
p = [2, 4, 6, 8]
q = [8, 6, 4]
# Print Results
plt.figure(figsize=(15, 5))
plt.subplot(1, 3, 1)
plt.title("Linear Convolution")
plt.xlabel("n")
plt.ylabel("Amplitude")
plt.subplot(1, 3, 2)
plt.title("Circular Convolution")
plt.xlabel("n")
plt.subplot(1, 3, 3)
plt.xlabel("n")
plt.tight_layout()
plt.show()