# Smoothing Accelerometer/Gyroscope Displacement Tracking for Mouse Motion

I'm working on a project to combine a linear acceleration reading from an accelerometer and gyroscope, adjusted for gravity, into displacement/position on an x-y plane, specifically for turning the motion of the sensor into mouse motion. I have a basic implementation working, but the movement feels very noisy and unnatural.

Essentially I'm following the "double integration" method. I've done some research and am familiar with some of the limits to overall accuracy of this approach, but its been my impression that people are talking about this in the context of dead-reckoning for navigation, so I was hoping we could get a "good enough" solution for mouse movement feels relatively natural. Especially since at some level if its being used to register mouse movement there'll be a feedback loop from the user controlling the mouse.

One of the biggest problems is that I haven't found a satisfactory way to remove noise that doesn't degrade the overall feel of the motion too much. I've tried Kalman filters, but have only gotten it working via external libraries such as Kalman.js given the complexity of the math behind it. So it could've partly been down to not having the right parameters for the filter. However I've also read that a Kalman filter may be a bad choice for smoothing mouse movements since its hard to model the underlying system of a person moving a mouse around to interact with a computer.

Is there a different filtering technique we could apply to smooth the movement readings out? Or is producing a reasonable short lived estimate of displacement, even for this application of producing mouse movements, ultimately too difficult with just an inexpensive accelerometer/gyro?

• Probably dsp.stackexchange.com could be a better place to ask this question. Mar 28, 2017 at 16:30
• If you have a noise problem , can this be defined in amplitude and spectrum relative to full scale normal use. Is it due to random , vibration or sampling noise? Can you apply nonlinear gain variables for acceleration to velocity ? Is your sampling rate high enough? Mar 28, 2017 at 16:38
• Kalman filters are common for nav control on flight parameters for a,v heading with prediction corrections and noise rejection but ultimately some other feedback is needed for position error correction or drift compensation or auto-rezero accel on idle. Mar 28, 2017 at 16:48
• Thanks @TonyStewart.EEsince'75 , thats a good clarification regarding the use of Kalman filters. I'll have to think a bit about how the noise is best defined, but I think the noise is a mix of mostly random noise, and quirks introduced by applying some more heuristic techniques for handling noise such as having a "friction" for the computed velocity.
– wik
Mar 28, 2017 at 19:13

Since we don't get many details of your design (hardware, software, etc), I can only offer a generic answer.

First step is to look at your sensor output. Can you get a telemetry channel out of your system that you can plot and visualize? From this you'll be able to determine your static noise versus the meaningful signal that you are trying to detect and respond to. And when accelerometers are involved, noise will abound!

Once you have this data, you'll be well equipped to design a filter to suit your needs. Especially with IMUs, it doesn't help to just shotgun it with a filter and call it good. You need to read your signal in real time and design a filter to fit the need. If you find that you need Kalman, then fine, but maybe you don't need it. Or maybe you want it but can't until you've modeled your noise.

Different filters have different pros and cons, so you have to determine what filtering you can apply and what it will cost you (lag time is usually involved). With IMUs, don't be surprised if you have to layer some filters as well, and perhaps filter->process->filter->process for a couple iterations until you can clearly detect your meaningful data.

• Thanks, I hadn't really considered layered filters much but the more I read about this the more it seems that a higher order approach like that will be necessary. I have some telemetry hooked up now to plot the readings, any pointers on how to go about interpreting the plots? I can definitely see some portions that look like they represent certain kinds of noise, but I'm not sure what the next step in interpreting/removing them is.
– wik
Apr 3, 2017 at 15:32
• A plus about getting sensor telemetry out of your system is that you can put it into another program (usually Excel) and design different filters and get the result right away. It will help your development cycle to be able to work out this stuff on your computer using the data you pulled out. When you are able to tame the signal to your liking, you can code it into the target and see how it goes. Repeat until you like what you see. Apr 5, 2017 at 13:29

In short you have the kalman filter and particle filter (better) to create estimates of the states. Particle filters do better 'honing' in on the state, and are similar to Kalman filters. It is difficult to get these working because of the complexity you have to dot all your I's and cross all of your T's but it is worth it because they will estimate the position because they track the mean and variance of the states.

With a Kalman or particle filter, you only need to worry about the physical system you want to model and the accuracy of the sensor (not the input, in your case the person is part of the input). In this case the accelerometer, which actually happens to be a double integrator system. You integrate the acceleration to get velocity and the velocity to get position. The math works out to be the same if your using a double integrator vs a Kalman filter. You could use the same set of state equations for both. The difference is the Kalman filter also tracks the mean and variance.

The first thing you do for Kalman filters or to model any physical system is come up with a model, most people choose state space as a representation of the equations because its a matrix and easy to manipulate. If you want to implement this in code you also have to consider that your system is being sampled, so you have to convert the system of equations to discrete time. Then its plug and chug into the Kalman filter equations.

Then you have smoothing filters like low pass filters, median filters and Savitzky Golay filters to name a few if you just want to filter the output of whatever state estimate your using.

Filters can only do so much, if you can't solve the problem with smoothing then you probably need to look at changing your system and getting a sensor with less noise and more resolution. Another option is using two accelerometers and feeding the states from both into the kalman filter, which from what I understand is what some 3d position sensors do, but you would also have to solve the state equation for two sensors and probably factor in the distance between them.