# Is the typical implementation of low pass filter in C code actually not a typical low pass filter?

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:

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:

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?

$$y[n]= (1-a) \cdot y[n-1] + a \cdot (x[n]+x[n-1])/2$$
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).