0% found this document useful (0 votes)
17 views9 pages

Assignment 2

This document outlines a Python experiment focused on performing convolution and basic operations on discrete-time sequences, including shifting, folding, addition, and multiplication. It provides code examples for both manual convolution and using an inbuilt function, along with visualizations of the sequences and results. The experiment demonstrates fundamental signal processing concepts through practical coding exercises.

Uploaded by

aditya arya
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)
17 views9 pages

Assignment 2

This document outlines a Python experiment focused on performing convolution and basic operations on discrete-time sequences, including shifting, folding, addition, and multiplication. It provides code examples for both manual convolution and using an inbuilt function, along with visualizations of the sequences and results. The experiment demonstrates fundamental signal processing concepts through practical coding exercises.

Uploaded by

aditya arya
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/ 9

AIM OF EXPERIMENT

Performing convolution using basic functions and inbuilt functions


Q1) Write functions for shifting, folding, addition, and multiplication discrete time
sequence x ( n ).

x ( n1 )= {1 2 3 4 } , n1=−1:2 ;

h ( n 2) ={ 1 21 1 } ,n 2=−2 :1;

Then find x ( n−2 ) , x ( n+2 ) , x (−n ) , x (−n−2 ) , x (−n+2 ), x ( n ) +h(n), x ( n ) h(n)

Solution:
import numpy as np
import matplotlib.pyplot as plt

def shift_sequence(x, n, shift):


return x, n + shift

def fold_sequence(x, n):


return np.flip(x), -np.flip(n)

def add_sequences(x, n1, h, n2):


n_min = min(min(n1), min(n2))
n_max = max(max(n1), max(n2))
n = np.arange(n_min, n_max + 1)
x_new = np.zeros_like(n, dtype=int)
h_new = np.zeros_like(n, dtype=int)

x_new[(n >= min(n1)) & (n <= max(n1))] = x


h_new[(n >= min(n2)) & (n <= max(n2))] = h

return x_new + h_new, n

def multiply_sequences(x, n1, h, n2):


n_min = min(min(n1), min(n2))
n_max = max(max(n1), max(n2))
n = np.arange(n_min, n_max + 1)
x_new = np.zeros_like(n, dtype=int)
h_new = np.zeros_like(n, dtype=int)

x_new[(n >= min(n1)) & (n <= max(n1))] = x


h_new[(n >= min(n2)) & (n <= max(n2))] = h

return x_new * h_new, n

# Given sequences and their index ranges


x = np.array([1, 2, 3, 4])
n1 = np.arange(-1, 3)
h = np.array([1, 2, 1, 1])
n2 = np.arange(-2, 2)

# Perform the operations


x_shifted_minus_2, n_shifted_minus_2 = shift_sequence(x, n1, -2)
x_shifted_plus_2, n_shifted_plus_2 = shift_sequence(x, n1, 2)
x_folded, n_folded = fold_sequence(x, n1)
x_folded_shifted, n_folded_shifted = shift_sequence(x_folded, n_folded,
2)
x_added, n_added = add_sequences(x, n1, h, n2)
x_multiplied, n_multiplied = multiply_sequences(x, n1, h, n2)

# Display results
print("x(n):", x, "n:", n1)
print("h(n):", h, "n:", n2)
print("x(n-2):", x_shifted_minus_2, "n:", n_shifted_minus_2)
print("x(n+2):", x_shifted_plus_2, "n:", n_shifted_plus_2)
print("x(-n):", x_folded, "n:", n_folded)
print("x(-n+2):", x_folded_shifted, "n:", n_folded_shifted)
print("x(n) + h(n):", x_added, "n:", n_added)
print("x(n) * h(n):", x_multiplied, "n:", n_multiplied)

# Plotting all the signals in one window


plt.figure(figsize=(12, 10))

plt.subplot(4, 2, 1)
plt.stem(n1, x)
plt.title('x(n)')
plt.xlabel('n')
plt.ylabel('x(n)')

plt.subplot(4, 2, 2)
plt.stem(n2, h)
plt.title('h(n)')
plt.xlabel('n')
plt.ylabel('h(n)')

plt.subplot(4, 2, 3)
plt.stem(n_shifted_minus_2, x_shifted_minus_2)
plt.title('x(n-2)')
plt.xlabel('n')
plt.ylabel('x(n-2)')

plt.subplot(4, 2, 4)
plt.stem(n_shifted_plus_2, x_shifted_plus_2)
plt.title('x(n+2)')
plt.xlabel('n')
plt.ylabel('x(n+2)')

plt.subplot(4, 2, 5)
plt.stem(n_folded, x_folded)
plt.title('x(-n)')
plt.xlabel('n')
plt.ylabel('x(-n)')

plt.subplot(4, 2, 6)
plt.stem(n_folded_shifted, x_folded_shifted)
plt.title('x(-n+2)')
plt.xlabel('n')
plt.ylabel('x(-n+2)')

plt.subplot(4, 2, 7)
plt.stem(n_added, x_added)
plt.title('x(n) + h(n)')
plt.xlabel('n')
plt.ylabel('x(n) + h(n)')

plt.subplot(4, 2, 8)
plt.stem(n_multiplied, x_multiplied)
plt.title('x(n) * h(n)')
plt.xlabel('n')
plt.ylabel('x(n) * h(n)')

plt.tight_layout()
plt.show()
OUTPUT:
PLOT:

SUMMARY:
This Python code performs basic operations on discrete-time sequences, including shifting,
folding, addition, and multiplication. The sequences x(n) and h(n) are manipulated to
demonstrate these operations. The results are displayed through both printed outputs and plots,
showcasing the original sequences, their shifted versions, the folded sequence, the folded and
shifted sequence, and the results of addition and multiplication of the sequences. The
visualizations help in understanding these fundamental signal processing operations.
Q2). Write a code for the convolution of two given DTSs with and without using an inbuilt
function. The output function is y ( n ) which is given by

y ( n )=x ( n )∗h ( n ) = ∑ x (k )h(n−k )
k=−∞

Step 1: Folding h (−k )


Step 2: Shiftingh ( n 0−k )
Step 3: Multiplication v n 0 ( k )=x ( k ) h ( n0−k )
Step 4: Addition sum of all the values of the product sequences
Test the program using different sequences
x ( n1 )= {1 5 3 11 } , n1=−2 :2 ;

h ( n 2) ={ 1 21 2 } , n2=−1 :2;

SOLUTION:
Import numpy as np
import matplotlib.pyplot as plt

def shift_sequence(x, n, shift):


return x, n + shift

def fold_sequence(x, n):


return np.flip(x), -np.flip(n)

def multiply_sequences(x, h):


return x * h

def add_sequences(x, h):


return x + h

def convolution_manual(x, n1, h, n2):


len_x = len(x)
len_h = len(h)

# Folding h(n) to get h(-k)


h_folded, n_h_folded = fold_sequence(h, n2)

# Output sequence index range


n_y_min = min(n1) + min(n_h_folded)+1
n_y_max = max(n1) + max(n_h_folded)+1
n_y = np.arange(n_y_min, n_y_max + 1)
# Initialize y(n) with zeros
y = np.zeros(len(n_y), dtype=int)

# Prepare for plotting


all_shifted_h = []

# Perform convolution
for i in range(len(n_y)):
h_shifted, n_shifted_h = shift_sequence(h_folded, n_h_folded,
n_y[i])
all_shifted_h.append((n_shifted_h.copy(), h_shifted.copy())) #
Store the shifted h for plotting

# Overlap x(n) and h_shifted(n-k) and sum the products


for j in range(len_x):
if n1[j] in n_shifted_h:
product = multiply_sequences(x[j],
h_shifted[np.where(n_shifted_h == n1[j])[0][0]])
y[i] = add_sequences(y[i], product)

return y, n_y, h_folded, n_h_folded, all_shifted_h

# Given sequences and their index ranges


x = np.array([1, 5, 3, 1, 1])
n1 = np.arange(-2, 3)
h = np.array([1, 2, 1, 2])
n2 = np.arange(-1, 3)

# Perform the convolution manually


y_manual, n_y_manual, h_folded, n_h_folded, all_shifted_h =
convolution_manual(x, n1, h, n2)

# Perform the convolution using the inbuilt function


y_inbuilt = np.convolve(x, h)
n_y_inbuilt = np.arange(n1[0] + n2[0], n1[-1] + n2[-1] + 1)

# Display the result


print("Manual Convolution y(n):", y_manual)
print("n:", n_y_manual)
print("Using Inbuilt Function:", y_inbuilt)

# Plotting
plt.figure(figsize=(12, 12))

plt.subplot(3, 2, 1)
plt.stem(n1, x)
plt.title('x(n)')
plt.xlabel('n')
plt.ylabel('x(n)')

plt.subplot(3, 2, 2)
plt.stem(n2, h)
plt.title('h(n)')
plt.xlabel('n')
plt.ylabel('h(n)')

plt.subplot(3, 2, 3)
plt.stem(n_h_folded, h_folded)
plt.title('Folded h(n) - h(-k)')
plt.xlabel('k')
plt.ylabel('h(-k)')

plt.subplot(3, 2, 4)
plt.stem(all_shifted_h[0][0], all_shifted_h[0][1])
plt.title('Shifted h(-k) at n=0')
plt.xlabel('n')
plt.ylabel('h(n)')

plt.subplot(3, 2, 5)
plt.stem(n_y_manual, y_manual)
plt.title('y(n) - Manual Convolution')
plt.xlabel('n')
plt.ylabel('y(n)')

plt.subplot(3, 2, 6)
plt.stem(n_y_inbuilt, y_inbuilt)
plt.title('y(n) - Inbuilt Convolution')
plt.xlabel('n')
plt.ylabel('y(n)')

plt.tight_layout()
plt.show()

OUTPUT:
PLOT:

SUMMARY:

This Python code manually computes the convolution of two discrete-time sequences and
compares it with the built-in np.convolve function. It involves folding, shifting, and summing the
sequences to produce the output sequence y(n). The code also plots the original sequences, the
folded sequence, an example of a shifted sequence, and the results of both the manual and inbuilt
convolutions for visual comparison.

You might also like