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
hFIR[n]=1N(u[n]−u[n−N])(1)
where u[n] is the unit step function. The first order IIR filter
y[n]=αx[n]+(1−α)y[n−1](2)
has the impulse response
hIIR[n]=α(1−α)nu[n](3)
The goal is now to minimize the squared error
ϵ=∑n=0∞(hFIR[n]−hIIR[n])2(4)
Using (1) and (3), the error can be written as
ϵ(α)=∑n=0N−1(α(1−α)n−1N)2+∑n=N∞α2(1−α)2n=α2∑n=0∞(1−α)2n−2αN∑n=0N−1(1−α)n+∑n=0N−11N2=α21−(1−α)2−2αN1−(1−α)N1−(1−α)+1N=α2−α−2N(1−(1−α)N)+1N,0<α<2(5)
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
(1−α)N−1(2−α)2=1(6)
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
β(N+1)/2+β(N−1)/2−1=0(7)
This equation cannot be solved analytically for β, but it can be solved for N:
N=−2log(1+β)log(β),β≠0(8)
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