Can I get some help plotting an IIR filter frequency response in Python? The problem I'm trying to solve is described here.

In my script, I used Euler's identity to calculate z^-n, such that z^-n = cos(omegan) - jsin(omegan). I find the magnitude of these values from a range of 0 to pi, and then plot them. However, the resulting graph aperiodically oscillates between 0 and ~ -8 dB, and I'm honestly not sure why.

I'd appreciate any help! The code is as follows:

import math 
import matplotlib.pyplot as plt

# define constants 
b0 = .125 a1 = .875 x = [] total = 0

while total &lt;= math.pi: x.append(total) total += math.pi/256

return the magnitude of z-n

def find_z(n, omega):

# z = re^jw # e^jw = cos(w) + jsin(w) # z^-n = cos(wn) - jsin(wn) 
ret = abs(math.cos(omega * n) - (1j * math.sin(omega * n))) return ret

h = [] 
for i in x: h.append(b0 / (1 - (a1 * find_z(-1, i))))

for i in range(len(h)): h[i] = 20 * math.log10(h[i])  plt.plot(x, h, color='blue', marker='o', linestyle='-',  linewidth=2, markersize=12) 
plt.xscale("log") fig = plt.figure(figsize=(12, 6)) plt.show()

print(h)
We can't replace the learning experience of doing the actual homework, and coding questions should be posted on Stack Overflow, so below is not a complete solution but some hints in case this part wasn't clear: 

The frequency response is the magnitude and phase of the complex result for $H(z)$ when you use $z = e^{j\Omega}$, where $\Omega$ represents normalized radian frequency (normalized as it is the radian frequency divided by the sampling rate). This is the unit circle on the z-plane. 

That said, given the transfer function 

$$H(z) = \frac{0.125}{1-0.875z^{-1}}$$

To get the frequency response simply $z$ with $e^{j\Omega}$ and solve for the magnitude and phase as you sweep $\Omega$ over the frequencies of interest (such as from $0$ to $\pi$ as given in the linked problem, which is the equivalent of sweeping from DC to half the sampling rate.

These links may help if the above is confusing:

$e^{j\omega}$ on unit circle

How/why are the $\mathcal Z$-transform and unit delays related?