# [time-nuts] PLL Math Question

Chris Albertson albertson.chris at gmail.com
Thu Mar 13 01:06:07 EDT 2014

```On Wed, Mar 12, 2014 at 9:13 PM, Daniel Mendes <dmendesf at gmail.com> wrote:
> This is a FIR x IIR question...
>
> moving average = FIR filter with all N coeficients equalling 1/N
> exponential average = using a simple rule to make an IIR filter

Isn't his "moving average" just a convolution of the data with a box car
function?  That treats the last N samples equally and is likely not
optimal.   I think why he wants is a low pass filter.  This method is like
the hockey player who skates to where to puck was about 5 seconds ago.  It
is not the best way to play the game.  He will in fact NEVER get to the
puck if the puck is moving he is domed to chase it forever..   Same here
you will never get there.

But if you have a long time constant on the control loop you have in effect
the kind of "averaging" you want, one that tosses out erratic noisy data.
A PID controller uses only three memory locations and is likely the best
solution.

We have to define "best".  I'd define it as "the error integrated over time
is minimum".  I think PiD gets you that and it is also easy to program and
uses very little memory.  Just three values (1) the error, (2) the total of
all errors you've seen (in a perfect world this is zero because the
positive and negative errors cancel) and (3) the rate of change in the
error (is it getting bigger of smaller and how quickly?)  Multiply each of
those numbers by a constant and that is the correction to the output value.
It's maybe 6 or 10 lines of C code.   The "magic" is finding the right
values for the constants.