Sunteți pe pagina 1din 6

Assignment4_Qns_DTFT

February 19, 2020

0.1 SP Lab Assignment 4: Discrete Time Fourier Transform (DTFT)


Q1. Compute the DTFT (magnitude and phase) of the following (use scipy.signal.freqz). Plot from
ω = 2π to 2π. Observe the symmetries and relations between the spectra.
https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.signal.freqz.html
r [n] = u[n]u[n5]
r [n7]
r [ n + 4]
r [n]
(1) n r [ n ]

In [28]: import matplotlib.pyplot as plt


import numpy
import scipy.signal as sig

w = np.linspace(-2*np.pi, 2*np.pi, 1000)

# DTFT FORMULA

def DTFT(a, x, stp):


X = 0
n = np.arange(stp, stp+len(x))
for i in n:
X+= x[i-stp]*np.exp(-(1j)*a*i)
return abs(X), np.angle(X)

x = np.ones(5)
stp = 0
X_abs = np.zeros(len(w))
X_ang = np.zeros(len(w))
for i in range(len(w)):
X_abs[i], X_ang[i] = DTFT(w[i], x, stp)

In [25]: plt.plot(w, 20*np.log10(X_abs))

Out[25]: [<matplotlib.lines.Line2D at 0x7f8b967bdf28>]

1
In [26]: plt.plot(w,X_ang)

Out[26]: [<matplotlib.lines.Line2D at 0x7f8b9671ce80>]

2
In [29]: stp = 7
X_abs_2 = np.zeros(len(w))
X_ang_2 = np.zeros(len(w))
for i in range(len(w)):
X_abs_2[i], X_ang_2[i] = DTFT(w[i], x, stp)

In [31]: plt.plot(w, 20*np.log10(X_abs_2))

Out[31]: [<matplotlib.lines.Line2D at 0x7f8b966d9c50>]

In [33]: plt.plot(w, X_ang_2)

Out[33]: [<matplotlib.lines.Line2D at 0x7f8b96602d68>]

3
In [35]: stp = -4
X_abs_2 = np.zeros(len(w))
X_ang_2 = np.zeros(len(w))
for i in range(len(w)):
X_abs_2[i], X_ang_2[i] = DTFT(w[i], x, stp)
plt.plot(w, 20*np.log10(X_abs_2))
plt.show()
plt.plot(w, X_ang_2)

4
Out[35]: [<matplotlib.lines.Line2D at 0x7f8b964ab278>]

5
1 1.4 SAME AS ABOVE QUESTION
In [40]: r = np.zeros(len(x))
for i in range(len(x)):
r[i] = (-1)**i*x[i]
plt.stem(r)
plt.show()
stp = 0
X_abs_2 = np.zeros(len(w))
X_ang_2 = np.zeros(len(w))
for i in range(len(w)):
X_abs_2[i], X_ang_2[i] = DTFT(w[i], r, stp)
plt.plot(w, 20*np.log10(X_abs_2))
plt.show()
plt.plot(w, X_ang_2)

S-ar putea să vă placă și