I would like to apply a FIR or IIR filter (example: lowpass filter) on successive blocks/time-frames of 1024 samples each.
Possible applications:
As mentioned by @sobek in a comment, it's of course needed to specify the initial conditions to allow continuity. This is done with the zi
parameter of lfilter
.
The problem is solved by changing the main loop by:
while True:
b, a = butter(2, 2.0 * f / sr, btype='low')
if pos == 0:
zi = lfilter_zi(b, a)
y[pos:pos+N], zi = lfilter(b, a, x[pos:pos+N], zi=zi)
pos += N
f -= 1
if pos+N > len(x):
break
This seems to work even if the filter's cutoff (and thus the a
and b
) is modified at each iteration.