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

• Did you notice the 1e-15 on your vertical axis? – Dan Boschen Dec 7 '19 at 12:26
• Looks like you are doing everything just fine- you haven’t solved for your frequency response yet but I assume you were just plotting the unit circle first to confirm your processing results in a 0 dB response- which it does; pay attention to that scaling indicated on the vertical axis in your graph – Dan Boschen Dec 7 '19 at 12:40
• @DanBoschen can you please help me with implementing correctly Python IIR and FIR Filters? Thanks! – Robinson Chera Apr 17 at 20:47
• @RobinsonChera Was this your question or did you have a related question? This was a homework exercise or quiz question so is done the "long way". To implement directly in Python one should use the scipy.signal package and specifically the commands lfilter to filter and freqz to get the frequency response directly from the b and a coefficient vectors (similar to the filter and freqz commands in MATLAB/Octave). – Dan Boschen Apr 17 at 21:14
• @DanBoschen nope, it wasn't... I ve just added a questions on my profile regarding this topic, maybe you can help me... – Robinson Chera Apr 17 at 21:17

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?