40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
|
|
omega_c = 0.4*np.pi
|
|
omega = np.linspace(-np.pi, np.pi, 300)
|
|
|
|
# Inverse DTFT of Ideal Lowpass: $x[n] = {\omega_c \over \pi} \sinc(\omega_c n)$
|
|
|
|
# Plot $x[n]$ for values of $n$ between -50 and 50
|
|
n = np.arange(-50,51)
|
|
x = np.sinc(omega_c*n/np.pi)*omega_c/np.pi
|
|
plt.stem(n,x)
|
|
plt.ylabel("$x[n]$")
|
|
plt.xlabel("$n$")
|
|
plt.show()
|
|
|
|
# Plot the frequency response of $X(e^{j\omega})$
|
|
f_response = np.compress(np.where(omega >= 0, True, False), omega)
|
|
X_f_response = np.where(np.abs(f_response) <= omega_c, 1, 0)
|
|
plt.plot(f_response, X_f_response)
|
|
plt.ylabel("Amplitude")
|
|
plt.xlabel("Frequency [rad/sample]")
|
|
plt.show()
|
|
|
|
# Calculate truncated DTFTs of $x[n]$
|
|
plt.plot(f_response, X_f_response, label="Actual DTFT of $x[n]$")
|
|
for K in (10, 20, 30):
|
|
n_K = np.arange(-K, K+1)
|
|
X_K = np.zeros(f_response.shape, np.complex128)
|
|
for n in n_K:
|
|
x_n = np.sinc(omega_c*n/np.pi)*omega_c/np.pi
|
|
X_K += x_n * np.exp(-1j*f_response*n)
|
|
plt.plot(f_response, X_K, label=f"Truncated DTFT (K = {K})")
|
|
|
|
# Plot truncated DTFTs of $x[n]$
|
|
plt.ylabel("Amplitude")
|
|
plt.xlabel("Frequency [rad/sample]")
|
|
plt.legend()
|
|
plt.show()
|