# How do you initialize the output of a second-order-section filter?

I've got a 4 stage second-order-section filter. When I turn this filter on, it assumes it is starting from zero and takes forever to converge on the actual signal.

For example, let's say I have a position sensor that is reading 20mm. I would like to filter this sensor with a low-pass filter that has a low cut-off frequency of 0.001Hz.

Under typical, operating procedures this filter works great. However, it takes forever to reach the initial value of ~20mm. I'm curious how I would go about initialzing the filter output to 20mm instead of having to wait for it to go from 0mm to 20mm.

My current technique has been to simply write "20" into the filters memory, however, this seems to initialize the filter incorrectly such that it overshoots to a very very large number. Any thoughts?

• Group delay is part of the trade offs of filter design. If you make things too tight in the frequency domain, it’s reflected in the time domain. I would work on a more suitable filter rather than faking out a filter
– user28715
Aug 2, 2018 at 15:04

Assuming a single Direct Form 2 biquad filter, you can calculate the steady-state internal state and output for a constant input.

Using the notation in the linked Wikipedia article, if the input is $k$ (e.g. your 20mm), then in steady state, the delay line will contain the value

$$w = \frac{k}{1 + a_1 + a_2}$$

$$y = w (b_0 + b_1 + b_2)$$

So if you know that your input will consist of small variations around $k$, you may preload the delay line with $w$.

In the case of several biquads in cascade, calculate the steady-state output $y$ of the first biquad as input for the second filter, and so on.

To add to @Juancho's answer above. In the case of a Transposed Direct Form 2 biquad filter, with multiple stages, the answer is a bit more complicated.

According to wikipedia, the difference equation for stage i is:

$$y^{(i)}(n) = w_1^{(i)}(n-1) + b_0 \cdot x^{(i)}(n) \\ w_1^{(i)}(n) = w_2^{(i)}(n-1) + b_1 \cdot x^{(i)}(n) - a_1 \cdot y^{(i)}(n) \\ w_2^{(i)}(n) = b_2 \cdot x^{(i)} - a_2 \cdot y^{(i)}$$

At steady state, obviously $$w_1^{(i)}(n-1) = w_1^{(i)}(n)$$ and $$w_2^{(i)}(n-1) = w_2^{(i)}(n)$$. HOWEVER, $$y^{(i)} \neq x^{(i)}$$ and $$w_1^{(i)} \neq w_2^{(i)}$$. (Except for single stage, i.e. 2nd order filter). To find the steady state value, one needs to use above equations and solves $$w_1$$, $$w_2$$ and $$y$$ in terms of $$x$$.

Here is my result: First, find $$k_1$$ and $$k_2$$ from the following equation: $$k_1 = \frac{b_1 -a_1 \cdot b_0 + b_2 -a_2 \cdot b_0}{1 + a_1 + a_2} \\ k_2 = b_2 - a_2 \cdot (k_1 + b_0)$$

Then find $$w_1$$ and $$w_2$$ for each stage from the following equation:

$$w_1^{(i)} = k_1 \cdot x^{(i)} \\ w_2^{(i)} = k_2 \cdot x^{(i)} \\ x^{(i+1)} = y^{(i)} = w_1^{(i)} + b_0 \cdot x^{(i)}$$

To set a filter with a initial output of $$k$$, set $$x^{(1)} = k$$ and calculate all the internal states $$w_1^{(i)}$$ and $$w_2^{(i)}$$ accordingly.

I think that the reason for the slow convergence is the extremely low cut-off frequency of your filter. First of all determine the amount of noise you are reading and then try to apply a filter with a desired fc. This is also very important if you try to control a system, using the reading from the sensor. Such strict filters can lead to instability, because they do the task of the control much harder.

Also, based on the desired resolution, you can skip the filter and apply a quantizing function. For example:

read sensor -> if new_value - old value > ε -> value = new_value

Assuming you are utilizing a digital filter, the initial condition has to be in the range of 0 to 4095 (for 12bit ADC). For example, if the sensor measures distances from 0 to 40, 20 is equal to 4095/2 ~= 2047.