122ec0579 DSP Assgn5
122ec0579 DSP Assgn5
CODE:
import numpy as np
import matplotlib.pyplot as plt
# Define parameters
alpha = 0.9
K = 0.25
a = 0.5
b = 0.5
magnitude_H2 = np.abs(H2)
phase_H2 = np.angle(H2)
magnitude_H3 = np.abs(H3)
phase_H3 = np.angle(H3)
magnitude_H4 = np.abs(H4)
122EC0579
phase_H4 = np.angle(H4)
plt.subplot(2, 2, 1)
plt.plot(w, magnitude_H1)
plt.title('Magnitude Spectrum of H1')
plt.xlabel('Frequency (radians)')
plt.ylabel('|H1(e^jw)|')
plt.grid()
plt.subplot(2, 2, 2)
plt.plot(w, phase_H1)
plt.title('Phase Spectrum of H1')
plt.xlabel('Frequency (radians)')
plt.ylabel('Phase (radians)')
plt.grid()
plt.subplot(2, 2, 3)
plt.plot(w, magnitude_H2)
plt.title('Magnitude Spectrum of H2')
plt.xlabel('Frequency (radians)')
plt.ylabel('|H2(e^jw)|')
plt.grid()
plt.subplot(2, 2, 4)
plt.plot(w, phase_H2)
plt.title('Phase Spectrum of H2')
plt.xlabel('Frequency (radians)')
plt.ylabel('Phase (radians)')
plt.grid()
plt.tight_layout()
plt.show()
plt.figure(figsize=(12, 10))
plt.subplot(2, 2, 1)
plt.plot(w, magnitude_H3)
plt.title('Magnitude Spectrum of H3')
plt.xlabel('Frequency (radians)')
plt.ylabel('|H3(e^jw)|')
plt.grid()
plt.subplot(2, 2, 2)
plt.plot(w, phase_H3)
plt.title('Phase Spectrum of H3')
plt.xlabel('Frequency (radians)')
plt.ylabel('Phase (radians)')
plt.grid()
plt.subplot(2, 2, 3)
plt.plot(w, magnitude_H4)
122EC0579
plt.title('Magnitude Spectrum of H4')
plt.xlabel('Frequency (radians)')
plt.ylabel('|H4(e^jw)|')
plt.grid()
plt.subplot(2, 2, 4)
plt.plot(w, phase_H4)
plt.title('Phase Spectrum of H4')
plt.xlabel('Frequency (radians)')
plt.ylabel('Phase (radians)')
plt.grid()
PLOTS:
122EC0579
SUMMARY:
In first four ques ons, we are required to design first order low pass and high pass systems along with their
second order systems. This was done by first defining the transfer func on in the z-domain and then plo ng
their magnitude and phase response. The cutoff frequency of each of the filter depends on the constant α.
As α decreases, the cutoff frequency increases. The cutoff frequency(ω) is related to α as:
ω = 𝑐𝑜𝑠
CODE:
import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import fft, fftfreq
# Parameters
Fs = 2000 # Sampling frequency in Hz
N = 1024 # Number of samples
alpha = 0.9 # Alpha value for the first two equations
n = np.arange(N) # Time index
frequencies = [0, 200, 400, 600, 800, 1000] # Frequencies in Hz
# Angular frequencies
omega = [2 * np.pi * f / Fs for f in frequencies]
122EC0579
for i in range(2, N):
y3[i] = y3[i-1] - 0.25 * y3[i-2] + 0.25 * x[i] + 0.5 * x[i-1] + 0.25 * x[i-2]
y4[i] = y4[i-1] - 0.25 * y4[i-2] + 0.25 * x[i] - 0.5 * x[i-1] + 0.25 * x[i-2]
plt.subplot(4, 1, 1)
plt.plot(n, y1)
plt.title('y1[n] = alpha * y[n-1] + (1 - alpha) / 2 * (x[n] + x[n-1])')
plt.grid()
plt.subplot(4, 1, 2)
plt.plot(n, y2)
plt.title('y2[n] = alpha * y[n-1] + (1 - alpha) / 2 * (x[n] - x[n-1])')
plt.grid()
plt.subplot(4, 1, 3)
plt.plot(n, y3)
plt.title('y3[n] = y[n-1] - 0.25 * y[n-2] + 0.25 * x[n] + 0.5 * x[n-1] + 0.25 * x[n-2]')
plt.grid()
plt.subplot(4, 1, 4)
plt.plot(n, y4)
plt.title('y4[n] = y[n-1] - 0.25 * y[n-2] + 0.25 * x[n] - 0.5 * x[n-1] + 0.25 * x[n-2]')
plt.grid()
plt.tight_layout()
plt.show()
plt.subplot(4, 1, 1)
plt.plot(frequencies[:N//2], np.abs(Y1[:N//2]))
plt.title('Magnitude Spectrum of y1[n]')
plt.grid()
plt.subplot(4, 1, 2)
plt.plot(frequencies[:N//2], np.abs(Y2[:N//2]))
plt.title('Magnitude Spectrum of y2[n]')
plt.grid()
plt.subplot(4, 1, 3)
plt.plot(frequencies[:N//2], np.abs(Y3[:N//2]))
122EC0579
plt.title('Magnitude Spectrum of y3[n]')
plt.grid()
plt.subplot(4, 1, 4)
plt.plot(frequencies[:N//2], np.abs(Y4[:N//2]))
plt.title('Magnitude Spectrum of y4[n]')
plt.grid()
plt.tight_layout()
plt.show()
PLOTS:
122EC0579
SUMMARY:
In this ques on, we passed the sum of harmonics to the filter equa ons defined by the first four ques ons.
So, we first found the fourier transform of the signal, mul plied it by the filter transfer func on and then
took its inverse fourier transform to obtain the difference equa on defining the system. A er passing the
input to the system, we took the of the output and analysed the spectrum. We found the output
spectrum as desired.
NAME-ADITYA ARYA
ROLL-122EC0579
122EC0579