Предположим, что первый БИХ фильтр первого порядка:
Как я могу выбрать параметр IIR как можно лучше приближает FIR, который является средним арифметическим последних выборок:
Где , что означает, что вход для IIR может быть длиннее, чем и все же я хотел бы получить наилучшее приближение среднего значения последних входов.
Я знаю, что БИХ имеет бесконечную импульсную характеристику, поэтому я ищу лучшее приближение. Я был бы рад за аналитическое решение, будь то для функции стоимости или .
Как можно решить эту проблему оптимизации, учитывая только 1-го порядка БИХ.
Спасибо.
Ответы:
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
whereH1 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!
источник
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),
For convenience with respect to the following discussion, it corresponds to the following difference equation:
The approximation which puts the filter into the form you specified requires assuming thatxn−N≈yn−1 , because (and I quote from pg. 68) "yn−1 is the average of xn samples". That approximation allows us to simplify the preceding difference equation as follows:
Settingα=1N , we arrive at your original form, yn=αxn+(1−α)yn−1 , 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):
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 anL2 norm). A broad overview of the techniques in the thesis can be found here. They may yield other interesting approximations.
источник
OK, let's try to derive the best:
The best mean-square approximation will minimize:
Next step is to take derivatives and equate to zero.
Looking at a plot of the derivedJ 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 .
I think there's a mistake here. The way it should be according to my calculations is:
Simplifying it according to Mathematica yields:
Using the following code on MATLAB yields something equivalent though different:
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:
PlottingJ2(α) for various values of K versus α results in the date in the plots and table below.
The red dashed lines are1/K and the green lines are αmin , the value of α that minimizes J2(α) (chosen from alpha=[0:.01:1]/3; ).
источник
Based in experimental tests with
k
in range (2 to 100) the best fit (sum squared error) gives a relation ofalfa = 1/k^0.865
beingk
number of samples for MovAvg filterисточник
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 lengthN FIR moving average filter is given by
whereu[n] is the unit step function. The first order IIR filter
has the impulse response
The goal is now to minimize the squared error
Using(1) and (3) , the error can be written as
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
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
This equation cannot be solved analytically forβ , but it can be solved for N :
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:
Below is a table with the optimal values ofα for a range of filter lengths N :
источник