DSP Lab Manual
DSP Lab Manual
# LAB 1
import numpy as np
import matplotlib.pyplot as plt
# Generate signals
t_exp, exp_signal = generate_exponential_damped_sinusoid(alpha=2,
freq=5, length=duration, sample_rate=sample_rate)
t_saw, saw_signal = generate_sawtooth(freq=2, length=duration,
sample_rate=sample_rate)
t_tri, tri_signal = generate_triangular_pulse(freq=1, length=duration,
sample_rate=sample_rate)
# Sawtooth waveform
plt.subplot(1, 3, 2)
plt.plot(t_saw, saw_signal)
plt.title('Sawtooth Waveform')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.grid(True)
# Triangular pulse
plt.subplot(1, 3, 3)
plt.plot(t_tri, tri_signal)
plt.title('Triangular Pulse')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.grid(True)
plt.tight_layout()
plt.show()
LAB 2
# LAB 2
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(12, 8))
plt.subplot(2, 1, 1)
plt.plot(t, analog_signal, label='Analog Signal')
plt.stem(sampled_t, sampled_signal, linefmt='r-', markerfmt='ro',
basefmt=' ', label='Sampled Points')
plt.title(f'F = {F} Hz, Fs = {Fs} Hz')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.legend()
plt.grid(True)
plt.subplot(2, 1, 2)
fft_freq = np.fft.fftfreq(len(analog_signal), 1/Fs)
fft_values = np.abs(np.fft.fft(analog_signal))
plt.plot(fft_freq, fft_values)
plt.title('Frequency Spectrum')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude')
plt.grid(True)
plt.xlim(0, 3*F)
plt.tight_layout()
plt.show()
analyze_nyquist(F, Fs)
def checkLinearity(Tr):
x1plusx2 = lambda t: x1(t)+x2(t)
plt.plot(t, Tr(x1)(t)+Tr(x2)(t),'b', label='T(x1)+T(x2)')
plt.plot(t, Tr(x1plusx2)(t),'r.', label='T(x1+x2)')
plt.legend()
plt.title('Linearity Check')
plt.grid()
plt.show()
def Tr(x):
return lambda t: x(t-2) # y(t) = x(t-2)
checkLinearity(Tr)
def Tr(x):
return lambda t: x(t)**2 # y(t) = x(t)²
checkLinearity(Tr)
# LAB 3 Part 2
t = np.linspace(-2, 4, 5)
x = lambda t: np.sin(t)
def showTimeInvariance(Tr):
plt.figure(figsize=(10,4))
plt.subplot(1,2,1)
plt.plot(t, x(t),'r', label='x(t)')
plt.plot(t, x(t-0.5),'b', label='x(t-0.5)')
plt.plot(t, x(t-1),'k', label='x(t-1)')
plt.title('Input Signals')
plt.legend()
plt.grid()
plt.subplot(1,2,2)
y1 = Tr(x)
y2 = Tr(lambda t: x(t-0.5))
y3 = Tr(lambda t: x(t-1))
plt.plot(t, y1(t), 'r', label='T(x(t))')
plt.plot(t, y2(t), 'b', label='T(x(t-0.5))')
plt.plot(t, [y1(ti-0.5) for ti in t], 'b--', label='y(t-0.5)')
plt.plot(t, y3(t), 'k', label='T(x(t-1))')
plt.plot(t, [y1(ti-1) for ti in t], 'k--', label='y(t-1)')
plt.title('Output Responses')
plt.legend()
plt.grid()
plt.show()
def Tr(x):
return lambda t: t*x(t) # y(t) = t·x(t)
showTimeInvariance(Tr)
def Tr(x):
return lambda t: x(t)**2 # y(t) = x(t)²
showTimeInvariance(Tr)
LAB 4
# Lab 4
import numpy as np
import matplotlib.pyplot as plt
plt.subplot(1, 3, 1)
plt.stem(x, markerfmt='bo')
plt.title('x(n)')
plt.grid(True, alpha=0.3)
plt.subplot(1, 3, 2)
plt.stem(h, markerfmt='ro')
plt.title('h(n)')
plt.grid(True, alpha=0.3)
plt.subplot(1, 3, 3)
plt.stem(y, markerfmt='go')
plt.title('y(n) = x(n)*h(n)')
plt.grid(True, alpha=0.3)
plt.suptitle(title, y=1.05) # Adjust title position
plt.tight_layout()
plt.show()
# Case 1
x1 = np.array([-3, -2, -1, 0, 1, 2, 3])
h1 = np.array([1, 1, 1, 1, 1])
y1 = np.convolve(x1, h1, mode='full')
plot_signals(x1, h1, y1, "Case 1: x(n) = { -3,-2,-1,0,1,2,3 }, h(n) =
{1,1,1,1,1}")
# Case 2
x2 = np.array([1, 2, 3, 4, 5])
h2 = np.array([1])
y2 = np.convolve(x2, h2, mode='full')
plot_signals(x2, h2, y2, "Case 2: x(n) = {1,2,3,4,5}, h(n) = {1}")
# Case 3
x3 = np.array([-1, -2, 0, 2, 1])
h3 = x3.copy()
y3 = np.convolve(x3, h3, mode='full')
plot_signals(x3, h3, y3, "Case 3: x(n) = { -1,-2,0,2,1}, h(n) = x(n)")
LAB 5
# Lab 5
import numpy as np
import matplotlib.pyplot as plt
plt.subplot(1, 2, 2)
plt.stem(np.angle(F))
plt.title('Phase (Task 1)')
plt.xlabel('k')
plt.grid(True)
plt.tight_layout()
plt.show()
# Case b
Fb = np.array([30, 1-1j, 1, 1+1j])
fb = np.fft.ifft(Fb)
plt.subplot(1, 2, 2)
plt.stem(np.angle(Fa))
plt.title('Phase (Task 2a)')
plt.xlabel('k')
plt.grid(True)
plt.tight_layout()
plt.show()
plt.subplot(1, 2, 2)
plt.stem(np.angle(Fb))
plt.title('Phase (Task 2b)')
plt.xlabel('k')
plt.grid(True)
plt.tight_layout()
plt.show()
Task 2a - IDFT:
[ 1.+0.j 1.+0.j -1.+0.j -1.+0.j]
Task 2b - IDFT:
[8.25+0.j 7.75+0.j 7.25+0.j 6.75+0.j]
LAB 6
# LAB 6
import numpy as np
import matplotlib.pyplot as plt
import sympy as sp
from sympy import symbols, summation, oo, exp, lambdify
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.stem(n_vals, f_vals)
plt.title(f'Discrete Function: {title}')
plt.xlabel('n')
plt.ylabel('f(n)')
plt.grid(True, alpha=0.3)
# Design filters
bp_b, bp_a = signal.butter(5, [center_freq-bandwidth/2,
center_freq+bandwidth/2],
btype='bandpass', fs=fs)
bs_b, bs_a = signal.butter(5, [center_freq-bandwidth/2,
center_freq+bandwidth/2],
btype='bandstop', fs=fs)
# Frequency Responses
w, h_bp = signal.freqz(bp_b, bp_a, fs=fs)
axs[0,0].plot(w, 20*np.log10(abs(h_bp)), linewidth=1)
axs[0,0].set_title('Band Pass Frequency Response', fontsize=9)
axs[0,0].set_ylim(-60, 5)
# Pole-Zero Plots
def plot_pz(ax, b, a, title):
z, p, k = signal.tf2zpk(b, a)
ax.scatter(np.real(z), np.imag(z), marker='o', facecolors='none',
edgecolors='b', s=40)
ax.scatter(np.real(p), np.imag(p), marker='x', color='r', s=40)
ax.axhline(0, color='black', linewidth=0.5)
ax.axvline(0, color='black', linewidth=0.5)
ax.set_title(title, fontsize=9)
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
# Common formatting
for ax in axs.flat:
ax.tick_params(axis='both', which='major', labelsize=7)
ax.grid(True, alpha=0.2)
LAB 8
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
def plot_filters():
# Common parameters
cutoff = 50 # Cutoff frequency (rad/s)
order = 4
# 1. Butterworth Filter
b_butter, a_butter = signal.butter(order, cutoff, 'low',
analog=True)
w_butter, h_butter = signal.freqs(b_butter, a_butter, worN=w)
# Plot results
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, sharey=True)
# Butterworth plot
ax1.semilogx(w_butter, 20*np.log10(abs(h_butter)))
ax1.set_title('Butterworth (N=4)')
ax1.set_xlabel('Frequency [rad/s]')
ax1.set_ylabel('Amplitude [dB]')
ax1.axvline(cutoff, color='green', linestyle='--')
ax1.set_ylim(-60, 5)
plt.tight_layout()
plt.show()
plot_filters()