0
$\begingroup$

Calling scipy.signal.sosfreqz with a sos-matrix describing an ordinary butterworth bandpass returns an array of h coefficients, of which the one at index 0 is of value 0.0+j0.0. A problem arises when the filter needs to be scaled to 0dB plotted via

plt.semilogx(f, np.array([20 * m.log10(abs(value)) for value in h]), label=None)

which runs into a ValueError: math domain error because the first h-value causes log10(0) which does not exist. Matlab somehow handles this implicitly. How could i modify/ignore h[0] in a scientifically correct manner?

Python code to reproduce:

sample_f = 48000
nyquist = sample_f / 2
_sos = signal.butter(6, [18245 / nyquist , 22988 / nyquist], btype='band', output='sos')
f, h = signal.sosfreqz(_sos,worN=sample_f)
print(abs(h[0])) # is zero
plt.semilogx(f, np.array([20 * m.log10(abs(value)) for value in h]), label=None)
$\endgroup$

1 Answer 1

1
$\begingroup$

The correct answer for at these frequencies is $-\infty$ which of course you can't plot.

Two ways of dealing with it:

  1. Add a small offset that's related to the dynamic range you want to see. Something like h[0] = 1e-10 would cap the plot at -200dB.
  2. Just exclude the offending frequency from your plot. Something like semilogx(f[1:],h[1:])
$\endgroup$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.