# 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 – Stanley Pawlukiewicz Aug 2 '18 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.

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.