Keep filter state for frame-wise processing

When I want to do frame-wise filtering of a longer signal, I want to use the last filter states for processing the next frame. While it is clear how to pass the argument, I don’t get how the states are returned.
If I use:

y, si = DSP.filt(b,a,x,si) 

it seems that si = y[1], which is not what I want.
How can I achieve this?
Thanks,
Martin

Can you post a more complete example of what you’re trying to do? For the following I’m assuming a and b are just vectors.

I don’t think filt returns the state the way you’re using it here. what you want to do is first create a DFT2Filter object that will store the filter state across filt calls. Something like this:

df = digitalfilter(Lowpass(0.1), Butterworth(4))
f = DF2TFilter(df)
x = randn(1000)
# apply the filter all in one go
y1 = filt(df, x)
# apply the filter in chunks
y2 = reduce(vcat, [filt(f, x[(1:250).+os]) for os in [0,250,500,750]])
@assert y1 ≈ y2

The docs don’t have a good example of this usage (would be a good PR!)

2 Likes

Thanks for you fast response.
I want to do LPC filtering. Both inverse and reconstruction.
Based on your example this would look something like this.

using DSP
u = randn(1000)
x = filt([1], [0.9, 0.7, 0.5, 0.2], u)
a, e = DSP.LPC.lpc(u, 10, LPCLevinson())
b = [1]
df = PolynomialRatio([1; a], b)
f = DF2TFilter(df) 
# apply the filter all in one go
y1 = filt([1; a], b, x)
# apply the filter in chunks
y2 = reduce(vcat, [filt(f, x[(1:250).+os]) for os in [0,250,500,750]])
@assert y1 ≈ y2

Unfortunately, when creating the DFT2Filter object, I get the following error:

ArgumentError: length of state vector must match filter order

Shouldn’t the state vector be set to zeros at the first call. Even, when I manually set the state vector to

si = zeros(T, max(length([1; a]), length([1]))-1))
f = DF2TFilter(df, si) 

I still get the same error. What am I getting wrong?

I’m not really familiar with the LPC functionality in DSP.jl so I’m not much help here. Could be a bug.

Maybe use the debugger to step through to where that comparison gets made to see what it’s expecting? You could also go to the line indicated in the error backtrace and it might be clear from there.

I think there is a bug. The check for the size of the filter is wrong.
There might be an additional bug, when applying filters with a DFT2Filter object created from PolynomialRatio objects. But I have to further check this.