# How to apply a Butterworth filter to data of varying sample rate?

I am trying to apply a Butterworth bandpass filter to accelerometer data of my smartphone. However, the accelerometer samples I receive do not come at regular intervals. Sample frequency varies between 1 Hz and 128 Hz in as little as a second.

I read the source code of a few Java implementations of Butterworth, but all of them appeared to assume a constant sample rate. Is there a way to implement Butterworth that allows for a variable sample rate? Or should I interpolate the accelerometer data to obtain a constant sample rate?

If you answer with math, please include a layman's explanation. I'm a bit weak on the theoretical side of signal processing.

• Most signal processing theory assumes uniformly-sampled data at a prescribed rate. As you suspected, one approach could be to use interpolation (e.g. polynomial interpolation) to resample your signal to a uniform grid, then apply whatever filters you like. – Jason R Apr 28 '15 at 12:30
• @JasonR Thanks, I'll look into polynomial interpolation! – WabbitSeason Apr 29 '15 at 7:58
• I'd suggest fitting a spline, and evaluating it on a uniform grid if that's what you want. Piecewise cubics are pretty smooth (on each piece), so no need to low-pass after that. Spline package in Java ? no idea, try questions/tagged/java+spline – denis Oct 21 '15 at 16:39

I can figure out two options:

• Interpolate data
• Remember that filters are dynamical systems and solve the corresponding ordinary differential equation (ODE)

In the second method, you basically use the retrieved time step as the time step in the ODE solver. Assuming you have a second order system:

$$\ddot{y}+\frac{\omega_0}{Q}\dot{y}+y\omega_0^2=x\omega_0^2$$

For butterworth, $Q=\frac{1}{\sqrt{2}}$ Introduce two variables $u=y$ and $v=\dot{u}$, and you get two coupled first order ODE:

\left\{\begin{aligned} \dot{u}&=v\\ \dot{v}&=x\omega_0^2-u\omega_0^2-\frac{\omega_0}{Q}v \end{aligned}\right.

Assuming you do not want to have a too high cutoff frequency

\left\{\begin{aligned} u&\leftarrow u+v\Delta t\\ v&\leftarrow v+ \left(x\omega_0^2-u\omega_0^2-\frac{\omega_0}{Q}v\right)\Delta t\end{aligned}\right.

Where $\Delta t$ is the time difference between two steps. If you want $\omega_0 > \frac{0.25\text{ Hz}}{2\pi}$, you need to consider the Euler backward method, or if you need more accuracy, the trapezoid method.

You may not want the overshoots and undershoots of the Butterworth filter. Then, set $Q$ a bit lower. Probably, you want a critically damped system, which do not have any such effects. In that case, set $Q=\frac{1}{2}$. An interesting side-effect of choosing the ODE approach is that you get a filtered derivative for free (it is the $v$ component), if you were to trig on changes in acceleration.