Android Accelerometer and Low-pass Filter

521 words | 2017-8-6

A sensors in our mobile phones have, among other things, an imperfect output signal quality. Well, it’s difficult to do something with the hardware, but one can play with software digital signal filters.

A bit of theory

Consider the simplest low-pass filter: Schematic diagram of RC filter

The voltage drop across the resistor and the current through it are connected:

\[V_{in}(t) - V_{out}(t)=Ri(t)\]

On the other hand, the definition of capacity implies:

\[Q_c(t)=V_{out}(t)\] \[i(t)=\frac{dQ(t)}{dt}\]

where \(Q_c(t)\) - charge of the capacitor at time \(t\). Then \(i(t)=C\frac{V_{out}(t)}{dt}\) and finally the filter is described by the equation:

\[V_{in}(t) - V_{out}(t)=RC\frac{V_{out}}{dt}\]

One can go to a discrete form. Assume that samples of the input and output signals are taken at regular intervals \( \Delta T\), sequence \(\lbrace x_1, x_2, x_3, \ldots x_n\rbrace\) - input signal \(V_{in}\), \(\lbrace y_1, y_2, y_3, \ldots y_n\rbrace\) - output signal \(V_{out}\), then:

\[x_i - y_i=RC\frac{y_i - y_{i-1}}{\Delta T}\]


\[y_i=y_{i-1}\left(\frac{RC}{RC+\Delta T}\right)+x_i\left(\frac{\Delta T}{RC + \Delta T}\right)\] \[y_i=\alpha y_{i-1} + (1 - \alpha)x_i\]

\(\alpha=\frac{RC}{RC+\Delta T}\) - coefficient of smoothing. From \(RC\), the filter smoothing frequency \(f_c = \frac{1}{2\pi RC}\) is determined, that is, the frequency above which the signal begins to smooth out.

Filter in Maxima

Suppose we have at the input a useful signal with a frequency of 2 kHz and noise with a frequency of 4 kHz:

Input signal and noise

Naturally, the filter input receives a total signal:

Total input signal

Signal and noise are given by functions:


In Maxima we can describe the input data with two lists of values, tl - time counts of 100 intervals of 0.00005 seconds, kH2lkH4l - a list of input values \(x_i\):

tl:makelist(i / (100 * 500), i, 0, 100)$
kH2lkH4l:makelist(kH4(i / (100 * 500)) + kH2(i / (100 * 500)), i, 0, 100)$

The filter function itself consists of a recursive part that literally repeats \(y_i=\alpha y_{i-1} + (1 - \alpha)x_i\):

lowpass_rec(x, alpha):= 
  if length(x) = 1 then
    [(1 - alpha) * first(x)]
  else block([y],
    y: lowpass_rec(rest(x), alpha),
    return(cons(alpha * first(y) + (1 - alpha) * first(x), y)))$

and the entry point:

lowpass(x, alpha):=reverse(lowpass_rec(reverse(x), alpha))$

\(\Delta T=0.00005\) is fixed and is defined by frequency of data receipt from the sensor. The smoothing frequency is 3kHz: \(f_c=\frac{1}{2\pi RC}=3000\)Hz.

\[RC= \frac{1}{2\pi f_c}=5.305164769729845E-5\] \[\alpha=\frac{RC}{RC+\Delta T}=0.7262210965743948\]

Let’s see how the filter works with this value of \(\alpha\) together with the others.

plot2d([[discrete, tl, kH2lkH4l], [discrete, tl, lowpass(kH2lkH4l, 0.5)],
        [discrete, tl, lowpass(kH2lkH4l, 0.7262210965743948)],
        [discrete, tl, lowpass(kH2lkH4l, 0.9)]],
        [legend, "V_in", "a=0.5", "a=0.726", "a=0.9"])$

Low Pass Filter Operation

For the other values of \(\alpha \), the boundary frequencies of this filter are equal, respectively:

\(\alpha=0.5, f_c=7957.747154\) Hz - all passes through the filter practically unchanged.

\(\alpha=0.9, f_c=884.194128\) Hz - the filter actively smoothes both our signal in 2kHz and noise in 4kHz.


It seems that one can already insert this filter as there is somewhere like:

    public void onSensorChanged(SensorEvent event)

\(\ldots\) and be disappointed. The reason is in \(\Delta T \) or in the sampling period, which varies from model to model, and during the program operation can vary quite significantly. And for a fixed \(\alpha \) this will change the operating frequency of the filter. So \(\alpha \) must be dynamic, we’ll discuss it next time. :smile: