# Determine filter type (HPF or LPF) by its coefficients

I want to determine optimal initial states for a FIR/IIR filter to get rid of oscillations and shifts at the initial interval.

I've found that these initial states depend on filter type in the following way (up to a constant):

for k=1:order % (Direct Form II Transposed)
z_hp(k) = sum(1*b(1+k:end)- 0*a(1+k:end)); % for HPF:  x=const, y=0
z_lp(k) = sum(1*b(1+k:end)- 1*a(1+k:end)); % for LPF:  x=const, y=const
end


Then I should choose z_hp or z_lp - how can I do this given only filter coefficients?

By now the solution is empirical - passing 10 points of constant value to the filter and compare the shift in output:

x = ones(1,10);
y_hp = filter(b,a,x,z_hp);
y_lp = filter(b,a,x,z_lp);

if (1-y_lp) > y_hp
z = z_hp;
else
z = z_lp;
end


I wonder if I there is a simple formula for this.

• I'm not sure I understand your question correctly, but does it help that the coefficients of high pass FIR filters have a mean value of zero? Oct 6, 2017 at 23:39

I am not sure if my answer will correctly suits your question as it seems a little confusing. Anyway, I understand that you want to eliminate the initial transition of your filter. What I propose is to initialize your filter state so that the output equals the input at $$t=0$$

I had to solve that problem in the past. What I did was to assume that all delays were initialized to the same value. Doing that, you simplify your problem a lot. I don't have a lot of theory to back this technique, but that seems to work quite good on my side.

If we consider that all delays are set to the same value $$K$$, we find this output equation.

$$y = (-a_1k-a_2k+x)b_0+b_1k+b_2k$$ A little algebra : $$y = b_0k(-a_1-a_2-...)+b_0x+k(b_1+b_2+...)$$ $$y = -kb_0\sum_{n=1}^{N_a}{a_n} + b_0x+k\sum_{n=1}^{N_b}{b_n}$$

Now, since we know we want the output to be equal to the input, we can say $$y=x$$

$$x = -kb_0\sum_{n=1}^{N_a}{a_n} + b_0x+k\sum_{n=1}^{N_b}{b_n}$$

Then

$$x-b_0x = -kb_0\sum_{n=1}^{N_a}{a_n}+k\sum_{n=1}^{N_b}{b_n}$$ $$x(1-b_0) = -kb_0\sum_{n=1}^{N_a}{a_n}+k\sum_{n=1}^{N_b}{b_n}$$ $$\frac{x(1-b_0)}{-b_0\sum_{n=1}^{N_a}{a_n}+\sum_{n=1}^{N_b}{b_n}} = k$$

So there you go. Initialize all your delays with $$K$$ and your filter will start with an output equal to the input.

You can do this for other forms as well.

Hope that helps

• Thank you for your answer! There is an error in your assumptions, since for highpass filters y is not equal to x, but y = 0 (e.g. if mean value of x is shifted from zero by some high value, then setting y = x it will have initial transition towards zero). See my comment to the previous answer. In common case, to find y, one should determine filter gain at zero frequency and multiply it with x: y = k0*x. So, how to determine this gain, given only filter coefficients? Nov 2, 2018 at 11:35
• You're right. To get the gain at f=0, replace $z$ by 1 ($e^{j0}$) and evaluate the transfer function. I'll update my answer soon Nov 4, 2018 at 0:32

Your description of "optimal initial states for the FIR/IIR filter" implies to me that the filter will be adaptive. If the filter is not adaptive, the filter coefficients are constant and determined by the filter specifications used to design the filter.

When a static FIR/IIR filter is used to process a signal, the initial transient part of the filtered signal is most often discarded. For an adaptive filter, values for the initial state of the filter can be estimated by selecting a set of the weights from a time after the filter has reached a "steady state" condition. The initial state of the adaptive filter can then be set prior to filtering. However, I think there might be other caveats that you'll have to consider such as the initial alignment of samples with the weights of the filter.

Where did you find the equations to compute the initial filter states based on the filter type (first block of code)?

• Yes, this is about filters with constant coefficients. The other alternative to get closer to "steady state" is to pass a repeated first point, but it can be slow due to unknown stabilization time. Equations to compute initial states are from the assumption that after passing a repeated initial point (constant), the filter should output zero (if gain at 0 Hz = 0, HPF or BPF) or the same value (gain at 0 Hz = 1, LPF). But then I should choose between LPF and HPF. Jun 8, 2017 at 9:11