# Keep filter state for frame-wise processing

**URL:** <https://discourse.julialang.org/t/keep-filter-state-for-frame-wise-processing/34867>\
**Category:** Signal and Image Processing\
**Tags:** question, dsp\
**Created:** [February 19, 2020, 5:20pm UTC](https://discourse.julialang.org/t/keep-filter-state-for-frame-wise-processing/34867 "2020-02-19T17:20:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hagi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hagi/32/3678_2.png) [@hagi](https://discourse.julialang.org/u/hagi)\
**Post date:** [February 19, 2020, 5:20pm UTC](https://discourse.julialang.org/t/keep-filter-state-for-frame-wise-processing/34867/1 "2020-02-19T17:20:53Z")

</div>

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:

```julia
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

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [February 19, 2020, 6:23pm UTC](https://discourse.julialang.org/t/keep-filter-state-for-frame-wise-processing/34867/2 "2020-02-19T18:23:31Z")

</div>

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`](https://juliadsp.github.io/DSP.jl/stable/filters/#DSP.Filters.DF2TFilter) object that will store the filter state across `filt` calls. Something like this:

```julia
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!)

---

<div class="post-metadata">

**Author:** ![hagi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hagi/32/3678_2.png) [@hagi](https://discourse.julialang.org/u/hagi)\
**Post date:** [February 20, 2020, 4:19pm UTC](https://discourse.julialang.org/t/keep-filter-state-for-frame-wise-processing/34867/3 "2020-02-20T16:19:35Z")

</div>

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.

```julia
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:

```julia
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

```julia
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?

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [February 20, 2020, 6:16pm UTC](https://discourse.julialang.org/t/keep-filter-state-for-frame-wise-processing/34867/4 "2020-02-20T18:16:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hagi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hagi/32/3678_2.png) [@hagi](https://discourse.julialang.org/u/hagi)\
**Post date:** [February 26, 2020, 8:53am UTC](https://discourse.julialang.org/t/keep-filter-state-for-frame-wise-processing/34867/5 "2020-02-26T08:53:46Z")

</div>

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.
