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

122ec0579 DSP Assgn5

The document describes the implementation of first and second-order low-pass and high-pass filter systems using transfer functions in Python. It includes code for calculating and plotting the magnitude and phase responses of these filters, as well as applying difference equations to a sum of harmonic signals and analyzing their output spectra. The cutoff frequency of the filters is influenced by the parameter alpha, with lower values resulting in higher cutoff frequencies.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

122ec0579 DSP Assgn5

The document describes the implementation of first and second-order low-pass and high-pass filter systems using transfer functions in Python. It includes code for calculating and plotting the magnitude and phase responses of these filters, as well as applying difference equations to a sum of harmonic signals and analyzing their output spectra. The cutoff frequency of the filters is influenced by the parameter alpha, with lower values resulting in higher cutoff frequencies.

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 PDF, TXT or read online on Scribd
You are on page 1/ 7

DSP LAB-7

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

# Define parameters
alpha = 0.9
K = 0.25
a = 0.5
b = 0.5

# Define frequency range


w = np.linspace(-np.pi, np.pi, 1024)

# Define transfer functions H1, H2, H3, H4


H1 = ((1 - alpha) / 2) * (1 + np.exp(-1j * w)) / (1 - alpha * np.exp(-1j * w))
H2 = ((1 - alpha) / 2) * (1 - np.exp(-1j * w)) / (1 - alpha * np.exp(-1j * w))
H3 = (K * ((1 + np.exp(-1j * w))**2)) / ((1 - a * np.exp(-1j * w)) * (1 - b * np.exp(-1j * w)))
H4 = (K * ((1 - np.exp(-1j * w))**2)) / ((1 - a * np.exp(-1j * w)) * (1 - b * np.exp(-1j * w)))

# Calculate magnitude and phase for each transfer function


magnitude_H1 = np.abs(H1)
phase_H1 = np.angle(H1)

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)

# Plot the magnitude spectrum for all transfer functions


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

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()

# Show the plots


plt.tight_layout()
plt.show()

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]

# Input signal x(n) as a sum of cosines with the given frequencies


x = sum([np.cos(w * n) for w in omega])

# Initialize output arrays for each difference equation


y1 = np.zeros(N)
y2 = np.zeros(N)
y3 = np.zeros(N)
y4 = np.zeros(N)

# Apply the difference equations


for i in range(1, N):
y1[i] = alpha * y1[i-1] + (1 - alpha) / 2 * (x[i] + x[i-1])
y2[i] = alpha * y2[i-1] + (1 - alpha) / 2 * (x[i] - x[i-1])

# Apply third and fourth equations which require y[i-2]

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]

# Compute FFT of each output signal


Y1 = fft(y1)
Y2 = fft(y2)
Y3 = fft(y3)
Y4 = fft(y4)

# Frequency range for plotting the spectrum


frequencies = fftfreq(N, 1/Fs)

# Plot the output signals


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

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()

# Plot the frequency spectrum (Magnitude of FFT)


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

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy