Каково наилучшее приближение первого порядка IIR (AR-фильтр) к фильтру скользящего среднего (FIR-фильтр)?


24

Предположим, что первый БИХ фильтр первого порядка:

y[n]=αx[n]+(1α)y[n1]

Как я могу выбрать параметр IIR как можно лучше приближает FIR, который является средним арифметическим последних выборок:αk

z[n]=1kx[n]+1kx[n1]++1kx[nk+1]

Где , что означает, что вход для IIR может быть длиннее, чем и все же я хотел бы получить наилучшее приближение среднего значения последних входов.n[k,)kk

Я знаю, что БИХ имеет бесконечную импульсную характеристику, поэтому я ищу лучшее приближение. Я был бы рад за аналитическое решение, будь то для функции стоимости L2 или L1 .

Как можно решить эту проблему оптимизации, учитывая только 1-го порядка БИХ.

Спасибо.


Имеет ли она следовать y[n]=αx[n]+(1α)y[n1] точно]?
Фонон

1
Это должно стать очень плохим приближением. Разве вы не можете позволить себе что-то большее, чем БИХ первого порядка?
оставил около

Возможно, вы захотите отредактировать свой вопрос так, чтобы вы не использовали y[n] для обозначения двух разных вещей, например, второе отображаемое уравнение может иметь вид z[n]=1kx[n]++1kx[nk+1], и вы можете сказать, что именно является вашим критерием «как можно лучше», например, хотите ли вы|y[n]z[n]|быть как можно меньше для всехnили|y[n]z[n]|2быть как можно меньше для всехn.
Дилип Сарват

@ Фонон, да, это должно быть БИХ первого порядка. Критерий прост, результат должен быть как можно ближе к среднему значению последних k входов в систему, где n [ k , inf ] . Я был бы рад увидеть результат в обоих случаях. Хотя я предполагаю, что аналитическое решение жизнеспособно только для | y [ n ] - z [ n ] | 2 . y[n]kn[k,inf]|y[n]z[n]|2
Рой

Ответы:


10

There is no analytic solution for α being a scalar (I think). Here is a script that gives you α for a given K. If you need it online you can build a LUT. The script finds the solution that minimizes

0πdw|H1(jw)H2(jw)|2

where H1 is the FIR frequency response and H2 is the IIR frequency response.

You did not specify any range for K. But I just want to make it clear that the following system is equivalent to your mean filter and has the same computational complexity and your first order IIR!

H(z)=1K1zK1z1

function a = find_a(K)

w = 0.0001:0.001:pi;
as = [-1:0.001:-0.001  0.001:0.001:1];

E = zeros(size(as));
for idx=1:length(as)
    fJ = J(w,as(idx),K);
    E(idx) = sum(fJ);
end

[Emin, indx] = min(E)
a = as(indx)

function f = J(w,a,K)
    num = 2*(2-a)*(1-cos(w*K)) + 2*(cos(w*(K-1)) - cos(w)) - 2*(1-a)*(cos(w)-cos(w*(K+1)));
    den = (2-a)^2 + 1 + (1-a)^2 + 2*(1-a)*cos(2*w) - 2*(2-a)^2*cos(w);
    f = -(a/K)*num./den;
    f = f+(1/K^2)*(1-cos(w*K))./(1-cos(w))+a^2./(1+(1-a)^2-2*(1-a)*cos(w));
end

end

@Drazick It is relative straight forward. The two expressions for the IIR and FIR is plugged into the integral. The key to finding the alternative expression for the FIR filter is to recognize the geometric progression/series. You find all the details here: en.wikipedia.org/wiki/Geometric_progression#Geometric_series. In the script, the J function calculates the expression under the integral sign.
niaren

@niaren I know this is an old post so if you can remember: how is your function 'f' derived? I've coded a similar thing but using the complex transfer functions for FIR (H1) and IIR (H2) and then doing sum(abs(H1 - H2)**2). I've compared this with your sum(fj), but get different resulting outputs. Thought I would ask before ploughing through the math.
Dom

@Dom That IS a long time ago and I really can't remember. I guess I just went through the process of working out [H1(jω)H2(jω)][H1(jω)H2(jω)]. I can't remember how I verified the expression. I don't mind going through the math again...
niaren

@niaren Hi, I tried to derive your expression but got stuck when adding up the complex fractions. I made a mistake in my code...your function seems to give results that are proportional to sum(abs(H1 - H2)**2), indicating it is correct.
Dom

16

There's a nice discussion of this problem in Embedded Signal Processing with the Micro Signal Architecture, roughly between pages 63 and 69. On page 63, it includes a derivation of the exact recursive moving average filter (which niaren gave in his answer),

H(z)=1N1zN1z1.

For convenience with respect to the following discussion, it corresponds to the following difference equation:

yn=yn1+1N(xnxnN).

The approximation which puts the filter into the form you specified requires assuming that xnNyn1, because (and I quote from pg. 68) "yn1 is the average of xn samples". That approximation allows us to simplify the preceding difference equation as follows:

yn=yn1+1N(xnyn1)yn=yn11Nyn1+1Nxnyn=(11N)yn1+1Nxn.

Setting α=1N, we arrive at your original form, yn=αxn+(1α)yn1, which shows that the coefficient you want (with respect to this approximation) is exactly 1N (where N is the number of samples).

Is this approximation the "best" in some respect? It's certainly elegant. Here's how the magnitude response compares [at 44.1kHz] for N = 3, and as N increases to 10 (approximation in blue):

N=3 N=[3,10]


As Peter's answer suggests, approximating an FIR filter with a recursive filter can be problematic under a least squares norm. An extensive discussion of how to solve this problem in general can be found in JOS's thesis, Techniques for Digital Filter Design and System Identification with Application to the Violin. He advocates the use of the Hankel Norm, but in cases where the phase response doesn't matter, he also covers Kopec's Method, which might work well in this case (and uses an L2 norm). A broad overview of the techniques in the thesis can be found here. They may yield other interesting approximations.


This is an "Elegant" way to say something about the memory of the first order IIR Filter. Its memory is equivalent to 1α. I'll look into the other methods you mentioned. Thanks.
Royi

Could you explain intuitively why under the LS norm (L2) there's no solution?
Royi

I'm not sure if there is a LS solution in this case or not yet, I just know that it tends have issues with convergence for the general "IIR-based FIR approximation" problem. I'll update w/ more info when I get a chance.
datageist

Well, If the cost function Peter suggested (The first one) is right there is a solution. At least according to my calculations.
Royi

Great. I'm curious to see how the "heuristic" 1/N approach compares to something more canonical.
datageist

16

OK, let's try to derive the best:

y[n]=αx[n]+(1α)y[n1]=αx[n]+(1α)αx[n1]+(1α)2y[n2]=αx[n]+(1α)αx[n1]+(1α)2αx[n2]+(1α)3y[n3]
so that the coefficient of x[nm] is α(1α)m.

The best mean-square approximation will minimize:

J(α)=m=0k1(α(1α)m1k)2+m=kα2(1α)2m=m=0k1(α2(1α)2m2kα(1α)m+1k2)+α2(1α)2km=0(1α)2m=α21(1α)2k1(1α)2+2αk1(1α)k1(1α)+α2(1α)2k1(1α)2+1k=α21(1α)2+2k(1(1α)k)+1k=α22αα2+2k(1(1α)k)+1k=α2α+2k(1(1α)k)+1k
because the FIR coefficients are zero for m>k1.

Next step is to take derivatives and equate to zero.


Looking at a plot of the derived J for K=1000 and α from 0 to 1, it looks like the problem (as I've set it up) is ill-posed, because the best answer is α=0.

enter image description here


I think there's a mistake here. The way it should be according to my calculations is:

J(α)=m=0k1(α(1α)m1k)2+m=kα2(1α)2m=m=0k1(α2(1α)2m2kα(1α)m+1k2)+α2(1α)2km=0(1α)2m=α21(1α)2k1(1α)22αk1(1α)k1(1α)+1k+α2(1α)2k1(1α)2

Simplifying it according to Mathematica yields:

J(α)=α2α+2(1α)k1k

Using the following code on MATLAB yields something equivalent though different:

syms a k;

expr1 = (a ^ 2) * ((1 - ((1 - a) ^ (2 * k))) / (1 - ((1 - a) ^ 2)));
expr2 = ((2 * a) / k) * ((1 - ((1 - a) ^ (k))) / (1 - (1 - a)));
expr3 = (1 / k);
expr4 = ((a ^ 2) * ((1 - a) ^ (2 * k))) / (1 - ((1 - a) ^ (2)));

simpExpr = simplify(expr1 - expr2 + expr3 + expr4);

J(α)=2α2k2(1α)k+1k

Anyhow, those functions do have minimum.


So let's assume that we really only care about the approximation over the support (length) of the FIR filter. In that case, the optimization problem is just:

J2(α)=m=0k1(α(1α)m1k)2

Plotting J2(α) for various values of K versus α results in the date in the plots and table below.

For K = 8. αmin = 0.1533333
For K = 16. αmin = 0.08
For K = 24. αmin = 0.0533333
For K = 32. αmin = 0.04
For K = 40. αmin = 0.0333333
For K = 48. αmin = 0.0266667
For K = 56. αmin = 0.0233333
For K = 64. αmin = 0.02
For K = 72. αmin = 0.0166667

enter image description here

The red dashed lines are 1/K and the green lines are αmin, the value of α that minimizes J2(α) (chosen from alpha=[0:.01:1]/3;).


1
Was just going to post the exact same thing = )
Phonon

@Phonon: Feel free to continue! I've marked it as community wiki for that purpose.
Peter K.

The derivative w.r.t α is a series with an infinite number of terms (i.e. not a polynomial) that you have to set equal to 0 and then solve for α, and so some care (or possibly approximation) is going to be necessary.
Dilip Sarwate

Can someone please check and/or correct my working? :-)
Peter K.

@DilipSarwate, What would be the best approximation? Thanks.
Royi


3

I stumbled upon this old question and I would like to share my solution. As mentioned in other answers, there is no analytical solution, but the function to be minimized behaves nicely and the optimal value of α can be found easily with a few Newton iterations. There is also a formula to check the optimality of the result.

The impulse response of the length N FIR moving average filter is given by

(1)hFIR[n]=1N(u[n]u[nN])

where u[n] is the unit step function. The first order IIR filter

(2)y[n]=αx[n]+(1α)y[n1]

has the impulse response

(3)hIIR[n]=α(1α)nu[n]

The goal is now to minimize the squared error

(4)ϵ=n=0(hFIR[n]hIIR[n])2

Using (1) and (3), the error can be written as

ϵ(α)=n=0N1(α(1α)n1N)2+n=Nα2(1α)2n=α2n=0(1α)2n2αNn=0N1(1α)n+n=0N11N2=α21(1α)22αN1(1α)N1(1α)+1N(5)=α2α2N(1(1α)N)+1N,0<α<2

This expression is very similar to the one given in this answer, but it's not identical. The restriction on α in (5) makes sure that the infinite sum converges, and it is identical to the stability condition for the IIR filter given by (2).

Setting the derivative of (5) to zero results in

(6)(1α)N1(2α)2=1

Note that the optimal α must be in the interval (0,1] because larger values of α result in an alternating impulse response (3), which cannot approximate the constant impulse repsonse of the FIR moving average filter.

Taking the square root of (6) and introducing β=1α, we obtain

(7)β(N+1)/2+β(N1)/21=0

This equation cannot be solved analytically for β, but it can be solved for N:

(8)N=2log(1+β)log(β),β0

Equation (8) can be used to double-check a numerical solution of (7); it must return the specified value of N.

Equation (7) can be solved with a few lines of (Matlab/Octave) code:

N = 50;     % desired filter length of FIR moving average filter

if ( N == 1 )    % no iteration for trivial case
    b = 0;
else
    % Newton iteration
    b = 1;       % starting value
    Nit = 7;
    n = (N+1)/2;
    for k = 1:Nit,
        f = b^n + b^(n-1) -1;
        fp = n*b^(n-1) + (n-1)*b^(n-2);
        b = b - f/fp;
    end

    % check result
    N0 = -2*log(1+b)/log(b) + 1     % must equal N
end

a = 1 - b;

Below is a table with the optimal values of α for a range of filter lengths N:

   N     alpha

   1   1.0000e+00
   2   5.3443e-01
   3   3.8197e-01
   4   2.9839e-01
   5   2.4512e-01
   6   2.0809e-01
   7   1.8083e-01
   8   1.5990e-01
   9   1.4333e-01
  10   1.2987e-01
  20   6.7023e-02
  30   4.5175e-02
  40   3.4071e-02
  50   2.7349e-02
  60   2.2842e-02
  70   1.9611e-02
  80   1.7180e-02
  90   1.5286e-02
 100   1.3768e-02
 200   6.9076e-03 
 300   4.6103e-03
 400   3.4597e-03
 500   2.7688e-03
 600   2.3078e-03
 700   1.9785e-03
 800   1.7314e-03
 900   1.5391e-03
1000   1.3853e-03
Используя наш сайт, вы подтверждаете, что прочитали и поняли нашу Политику в отношении файлов cookie и Политику конфиденциальности.
Licensed under cc by-sa 3.0 with attribution required.