I'm having a lot of fun writing signal processing code in Python / numpy, and I'm resisting the urge to pre-optimize the code. But my biquad implementation is slower than I want. Here's the inner loop -- it uses afor
loop: is there a way to vectorize it? Alternatively, could I use a scipy
filter, and if so, how do I translate a0
, a1
, a2
, b0
, b1
, b2
?
# src_frames is an ndarray containing source samples
# dst_frames is an equal size ndarray to receive the filtered samples
# self._x[] and self._y[] are the feedforward and feedback delay elements
for i, x in enumerate(src_frames):
# compute one output sample
y = (b0 * x + b1 * self._x[1] + b2 * self._x[2] -
a1 * self._y[1] - a2 * self._y[2])
# shift delay elements
self._x[2] = self._x[1]
self._x[1] = x
self._y[2] = self._y[1]
self._y[1] = y
dst_frames[i] = y
(Yes, I know I'm not using _x[0]
and _y[0]
, but it seemed clearer to keep the indices numbered with the coefficients.)