Assignment 2
Assignment 2
x ( n1 )= {1 2 3 4 } , n1=−1:2 ;
h ( n 2) ={ 1 21 1 } ,n 2=−2 :1;
Solution:
import numpy as np
import matplotlib.pyplot as plt
# 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)
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=−∞
h ( n 2) ={ 1 21 2 } , n2=−1 :2;
SOLUTION:
Import numpy as np
import matplotlib.pyplot as plt
# 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
# 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.