0
$\begingroup$

Quite often in C code you see something along the lines of:

$$ y = (1-a) y_{prev} + input * a $$

..where $a$ is some small number. This equation then runs in discrete intervals at the loop rate.

Let's assume $a = 0.1$.

If I convert this difference equation into z domain I arrive at the following:

$$ Y(z) = 0.1 / (1 - 0.9 * z^{-1}) = 0.1z/(z - 0.9)$$

This function has a horrific phase response: phase response

If I instead start with a continuous time low pass filter and convert it to difference equation I get a slightly different filter.

$$ H(s) = 1 / (s + 1)$$

I convert it into z domain using bi-linear transform:

$$ H(z) = (0.04762 + 0.04762 z^{-1})/(1 - 0.9048z^{-1}) $$

I then convert this into difference equation:

$$ y_n = 0.04762x_n + 0.04762x_{n-1} + 0.9048 * y_{n-1} $$

The result compared to the "simplistic" filter:

new filter

The result of the "proper" C implementation looks much more like a low pass filter than the "simplistic" implementation. The simplistic implementation is more like a lag regulator (I realized this while writing this post so it's almost that I have answered my own question). While the "proper" lpf is more like an analog LPF. Obviously this can negatively impact the performance of the system if the different behaviors are not taken into account. Are there situations when either one is better suited than the other?

$\endgroup$
1
$\begingroup$

In practice, there isn't a huge difference. The differences are mostly at higher frequencies which most applications will consider the "stop band" or "not the area of interest". The main benefit of a "proper" low pass filter is that you have a zero at Nyquist and you get much more attenuation very close to Nyquist.

If you care, the C code is almost identical and easy enough to write. Using your notation it looks like

$$y[n]= (1-a) \cdot y[n-1] + a \cdot (x[n]+x[n-1])/2 $$

The only difference is that instead of adding just the current input sample, you add the average of the last two inputs.

$\endgroup$
1
$\begingroup$

The "simplistic" implementation that you referenced above is often called a leaky integrator, a special case of a first-order IIR lowpass filter. They are discussed in more detail in several previous questions:

To answer your question specifically, yes, it is a lowpass filter. You can certainly construct more complicated filters with a better lowpass frequency response. The main advantages of the leaky integrator construct are its very simple implementation, and the fact that you can adjust its cutoff frequency by simply changing the value of $a$ (as $a \to 0$, the cutoff of the filter decreases).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.