0% found this document useful (0 votes)
11 views8 pages

Experiment 4

The document outlines an experiment to perform linear and circular convolution using MATLAB and Python on specified sequences. It includes detailed calculations for linear convolution of A and B, circular convolution of x and y, and circular convolution with zero padding for p and q. The document also provides code snippets for both MATLAB and Python to visualize the results of these convolutions.

Uploaded by

anshvermaiit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Experiment 4

The document outlines an experiment to perform linear and circular convolution using MATLAB and Python on specified sequences. It includes detailed calculations for linear convolution of A and B, circular convolution of x and y, and circular convolution with zero padding for p and q. The document also provides code snippets for both MATLAB and Python to visualize the results of these convolutions.

Uploaded by

anshvermaiit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

EXPERIMENT-4

Objective:

Perform convolution for given sequences using MATLAB and Python:


a) Linear Convolution for A = [1, 2, 3, 4] and B = [4, 3, 2, 1]
b) Circular Convolution for x = [5, 6, 7, 8] and y = [8, 7, 6, 5]
c) Circular convolution with zero padding for p = [2, 4, 6, 8] and q = [8, 6, 4]

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=−∞ }

 x[k] is the input signal.

 h[n] is the impulse response of the system.

 y[n] is the output signal.

For practical cases where sequences are finite, the formula simplifies to:
{ M −1} x [ k ]
y [ n]= ∑ x [k ]⋅h [ n−k ]
{k=0 }

where M is the length of the input sequence x.

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.

For sequences A and B, the linear convolution y[n] is given by:


{ M −1} A [ k ]
y [ n]= ∑ A[k ]⋅ B [ n−k ]
{k=0 }

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

Thus, the linear convolution of A and B is:

[4,11,20,30,20,11,4][4, 11, 20, 30, 20, 11, 4][4,11,20,30,20,11,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.

For sequences x and y of length N, the circular convolution z[n] is:


{ N−1} x [ k ]
z [n ]= ∑ x [k ]⋅ y [ ( n−k ) ¿ N ]
{k=0}
Given sequences:

x = [5, 6, 7, 8] and y = [8, 7, 6, 5]

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

Thus, the circular convolution of x and y is: [168,166,168,174]

Circular Convolution with Zero Padding:

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:

p = [2, 4, 6, 8] and q = [8, 6, 4], with q_padded as q = [8, 6, 4, 0]

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

Thus, the circular convolution of p and q with zero padding is:[88,76,80,116]


MATLAB CODE / OUTPUT
% Given Sequences

A = [1, 2, 3, 4];

B = [4, 3, 2, 1];

% Linear Convolution

lin_conv = conv(A, B);

figure;

stem(lin_conv, 'filled');

title('Linear Convolution');

xlabel('n');

ylabel('Amplitude');

grid on;

% Given Sequences for Circular Convolution

X = [5, 6, 7, 8];

Y = [8, 7, 6, 5];

% Circular Convolution

circ_conv = ifft(fft(X, N) .* fft(Y, N));

figure;

stem(circ_conv, 'filled');

title('Circular Convolution');

xlabel('n');

ylabel('Amplitude');

grid on;
PYTHON CODE / OUTPUT
import numpy as np

import matplotlib.pyplot as plt

# 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]

def circular_convolution(x, y):

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)

# Circular Convolution with Zero Padding

p = [2, 4, 6, 8]

q = [8, 6, 4]

q_padded = q + [0] * (len(p) - len(q))

circular_conv_padded = circular_convolution(p, q_padded)

# Print Results

print("Linear Convolution of A and B:", linear_conv)

print("Circular Convolution of x and y:", circular_conv)

print("Circular Convolution of p and q with zero-padding:", circular_conv_padded)


# Plotting

plt.figure(figsize=(15, 5))

# a) Linear Convolution Plot

plt.subplot(1, 3, 1)

plt.stem(range(len(linear_conv)), linear_conv, basefmt=" ")

plt.title("Linear Convolution")

plt.xlabel("n")

plt.ylabel("Amplitude")

# b) Circular Convolution Plot

plt.subplot(1, 3, 2)

plt.stem(range(len(circular_conv)), circular_conv, basefmt=" ")

plt.title("Circular Convolution")

plt.xlabel("n")

# c) Circular Convolution with Zero Padding Plot

plt.subplot(1, 3, 3)

plt.stem(range(len(circular_conv_padded)), circular_conv_padded, basefmt=" ")

plt.title("Circular Conv with Zero Padding")

plt.xlabel("n")

plt.tight_layout()

plt.show()

You might also like