# High-order EMA works great - but why?

Started by March 24, 2019
```I have a classic digital PLL with multiplier-based phase detector,
intended to lock at 50Hz. The sampling frequency Fs is 25kHz, the loop
filter bandwidth varies between Fc=10Hz to 40Hz, as I am experimenting
with different filter structures. The PLL itself works like
a charm and my reference filter is a 500th order FIR: fir1(500,Fc/(Fs/2)).

To a reasonable degree the loop converges to a 55Hz input signal after
4..5 periods; after about 12 periods the phase error is essentialy zero:

https://i.postimg.cc/MpmPz0QK/convergence-sine-fir.png
https://i.postimg.cc/hGGrgknp/convergence-sine-fir-magnified.png

(green is the input, blue is the VCO signal, red is the error)

Interestingly, in the case of a heavily distorted signal the convergence
is still great:

input=0.5*(1.5*sin(2*pi*f0+(80.0/180.0)*pi)+0.5*sin(3*2*pi*f0+(60.0/180.0)*pi)
+ 0.2*sin(5*2*pi*f0+(45.0/180.0)*pi) +
0.15*sin(7*2*pi*f0+(36.0/180.0)*pi) + 0.1*sin(11*2*pi*f0+(30.0/180.0)*pi))

https://i.postimg.cc/t48TCPQw/convergence-sine-fir-distorted.png

But to te point: as the overhead of the FIR is bit too high, my next
attempt was to use 2 sections of the same biquad

fs=25e3;
fc=35;
Wc=fc/fs;

[B,A] = cheby1(2,1,Wc);

The convergence rate is worse, but still acceptable. I am concerned
with the huge relative magnitude of the B and A coefficients, as the
target implementation is going to be fixed-point. But as a double-based
proof of concept it is fine.

My third attempt was to use an exponential moving averager and
experiment with the alpha. It turns out that 8 EMA sections (I'm not
sure the term "order" is still correct here) in series, all with
alpha=1/70 work comparably well to the IIR. The number of
multiplications is a bit higher than in the case of a biquad (16
instead of 10), but the structure seems to be unconditionally
stable and easy to implement in fixed-point. It even seems to
require less scaling fun than a good implementation of the FIR.

https://i.postimg.cc/TPPZSN4X/convergence-ema-sine.png
https://i.postimg.cc/MTVtryqS/convergence-ema-distorted.png
https://i.postimg.cc/5yf57TsB/convergence-ema-distorted-magnified.png

(in the last case the VCO tracks the f0 frequency component exactly,
there is no phase error, contrary to the first impression from the plot).

I think I am onto something, but most likely not the first one.
Could you please explain to me why the multi-stage EMA works so
well or suggest a good reading on the theoretical properties of
this kind of structures? I haven't seen the performance of EMAs
appreciated in the entry-level DSP books.

Best regards, Piotr

```
```Piotr Wyderski  <peter.pan@neverland.mil> wrote:

>I have a classic digital PLL with multiplier-based phase detector,
>intended to lock at 50Hz. The sampling frequency Fs is 25kHz, the loop
>filter bandwidth varies between Fc=10Hz to 40Hz, as I am experimenting
>with different filter structures. The PLL itself works like
>a charm and my reference filter is a 500th order FIR: fir1(500,Fc/(Fs/2)).
>
>To a reasonable degree the loop converges to a 55Hz input signal after
>4..5 periods; after about 12 periods the phase error is essentialy zero:
>
>https://i.postimg.cc/MpmPz0QK/convergence-sine-fir.png
>https://i.postimg.cc/hGGrgknp/convergence-sine-fir-magnified.png
>
>(green is the input, blue is the VCO signal, red is the error)
>
>Interestingly, in the case of a heavily distorted signal the convergence
>is still great:
>
>input=0.5*(1.5*sin(2*pi*f0+(80.0/180.0)*pi)+0.5*sin(3*2*pi*f0+(60.0/180.0)*pi)
>+ 0.2*sin(5*2*pi*f0+(45.0/180.0)*pi) +
>0.15*sin(7*2*pi*f0+(36.0/180.0)*pi) + 0.1*sin(11*2*pi*f0+(30.0/180.0)*pi))
>
>https://i.postimg.cc/t48TCPQw/convergence-sine-fir-distorted.png
>
>But to te point: as the overhead of the FIR is bit too high, my next
>attempt was to use 2 sections of the same biquad
>
>	fs=25e3;
>	fc=35;
>	Wc=fc/fs;
>
>	[B,A] = cheby1(2,1,Wc);
>
>The convergence rate is worse, but still acceptable. I am concerned
>with the huge relative magnitude of the B and A coefficients, as the
>target implementation is going to be fixed-point. But as a double-based
>proof of concept it is fine.

You can use a lattice topology to reduce the coefficient sensitivity and
ensure stability when implementing this transfer function.  (If you're
using Matlab with the filter design thingie, this is called an
"ARMA" filter.)

>My third attempt was to use an exponential moving averager and
>experiment with the alpha.  It turns out that 8 EMA sections (I'm not
>sure the term "order" is still correct here) in series, all with
>alpha=1/70 work comparably well to the IIR.

Is this another way of saying you're using a cascade of single-pole
IIR filters?  Which is to say, you're using a Bessel filter?

Steve
```
```On Monday, March 25, 2019 at 8:52:55 PM UTC-4, Steve Pope wrote:
> Piotr Wyderski  <peter.pan@neverland.mil> wrote:
>
> >I have a classic digital PLL with multiplier-based phase detector,
> >intended to lock at 50Hz. The sampling frequency Fs is 25kHz, the loop
> >filter bandwidth varies between Fc=10Hz to 40Hz, as I am experimenting
> >with different filter structures. The PLL itself works like
> >a charm and my reference filter is a 500th order FIR: fir1(500,Fc/(Fs/2)).
>

why does it work so well?

because with a passband requirement of circa 50 Hz and a stopband requirement circa 25 kHz, the loop filter is not particularly challenging.  Why use a 500 order filter?

fundamentally the filtering is easy because your Fs is much higher compared to the desired loop BW.  Works the same way in analog implementations.

m

```
```Le dimanche 24 mars 2019 04:19:09 UTC-4, Piotr Wyderski a &eacute;crit&nbsp;:
> I have a classic digital PLL with multiplier-based phase detector,
> intended to lock at 50Hz. The sampling frequency Fs is 25kHz, the loop
> filter bandwidth varies between Fc=10Hz to 40Hz, as I am experimenting
> with different filter structures. The PLL itself works like
> a charm and my reference filter is a 500th order FIR: fir1(500,Fc/(Fs/2)).
>
> To a reasonable degree the loop converges to a 55Hz input signal after
> 4..5 periods; after about 12 periods the phase error is essentialy zero:
>
> https://i.postimg.cc/MpmPz0QK/convergence-sine-fir.png
> https://i.postimg.cc/hGGrgknp/convergence-sine-fir-magnified.png
>
> (green is the input, blue is the VCO signal, red is the error)
>
> Interestingly, in the case of a heavily distorted signal the convergence
> is still great:
>
> input=0.5*(1.5*sin(2*pi*f0+(80.0/180.0)*pi)+0.5*sin(3*2*pi*f0+(60.0/180.0)*pi)
> + 0.2*sin(5*2*pi*f0+(45.0/180.0)*pi) +
> 0.15*sin(7*2*pi*f0+(36.0/180.0)*pi) + 0.1*sin(11*2*pi*f0+(30.0/180.0)*pi))
>
> https://i.postimg.cc/t48TCPQw/convergence-sine-fir-distorted.png
>
> But to te point: as the overhead of the FIR is bit too high, my next
> attempt was to use 2 sections of the same biquad
>
> 	fs=25e3;
> 	fc=35;
> 	Wc=fc/fs;
>
> 	[B,A] = cheby1(2,1,Wc);
>
> The convergence rate is worse, but still acceptable. I am concerned
> with the huge relative magnitude of the B and A coefficients, as the
> target implementation is going to be fixed-point. But as a double-based
> proof of concept it is fine.
>
> My third attempt was to use an exponential moving averager and
> experiment with the alpha. It turns out that 8 EMA sections (I'm not
> sure the term "order" is still correct here) in series, all with
> alpha=1/70 work comparably well to the IIR. The number of
> multiplications is a bit higher than in the case of a biquad (16
> instead of 10), but the structure seems to be unconditionally
> stable and easy to implement in fixed-point. It even seems to
> require less scaling fun than a good implementation of the FIR.
>
> https://i.postimg.cc/TPPZSN4X/convergence-ema-sine.png
> https://i.postimg.cc/MTVtryqS/convergence-ema-distorted.png
> https://i.postimg.cc/5yf57TsB/convergence-ema-distorted-magnified.png
>
> (in the last case the VCO tracks the f0 frequency component exactly,
> there is no phase error, contrary to the first impression from the plot).
>
> I think I am onto something, but most likely not the first one.
> Could you please explain to me why the multi-stage EMA works so
> well or suggest a good reading on the theoretical properties of
> this kind of structures? I haven't seen the performance of EMAs
> appreciated in the entry-level DSP books.
>
> 	Best regards, Piotr

Quick question : Since your frequency is 50 Hz, why not use a SOGI-PLL? Is there a reason to try another filter structure?
```