×
Namespaces

Variants
Actions

Reducing accelerometer sensor noise with Qt

From Nokia Developer Wiki
Jump to: navigation, search

This code snippet shows how to reduce accelerometer noise using Qt.

Contents

Introduction

In the CS000915 - Reducing accelerometer sensor noise code snippet, it was shown how to reduce accelerometer noise in Symbian/C++. This code snippet will show how this same noise filtering can be achieved using Qt. You can refer the code example Qt Bubble Level v1.3 also where sensor noise reduction is shown.

Summary

Qt provides a possibility to add custom filters for QSensor objects. These filters can be used for decreasing the sensors notifying frequency. For the accelerometer a filter can be added by implementing the QAccelerometerFilter interface and adding it for the QAccelerometer object via the addFilter method. In this code snippet sixteen sensor readings are collected to a buffer and the average of these readings is set as the notified reading.

Preconditions

  • Qt 4.7 or higher is installed.

Sources

accelerometerfilter.h

#include <QAccelerometerFilter> 
#include <QList>
 
QTM_USE_NAMESPACE
 
#define BUFFER_SIZE 16
 
class AccelerometerFilter : public QObject, public QAccelerometerFilter
{
Q_OBJECT
 
public:
explicit AccelerometerFilter(QObject *parent = 0);
private:
QList<QAccelerometerReading*> *readingBuffer;
int bufferPos;
public slots:
bool filter(QAccelerometerReading *reading);

accelerometerfilter.cpp

#include "accelerometerfilter.h"  
 
AccelerometerFilter::AccelerometerFilter(QObject *parent) :
QAccelerometerFilter(), readingBuffer(new QList<QAccelerometerReading*>())
{
}
 
bool AccelerometerFilter::filter(QAccelerometerReading *reading)
{
if (bufferPos < BUFFER_SIZE) {
readingBuffer->append(reading);
bufferPos++;
return false; // Don't send a notification yet
}
else {
bufferPos = 0;
qreal xSum = 0, ySum = 0, zSum = 0;
for (int i = 0; i < readingBuffer->count(); i++) {
xSum += readingBuffer->at(i)->x();
ySum += readingBuffer->at(i)->y();
zSum += readingBuffer->at(i)->z();
}
readingBuffer->clear();
reading->setX(xSum / BUFFER_SIZE);
reading->setY(ySum / BUFFER_SIZE);
reading->setZ(zSum / BUFFER_SIZE);
return true; // Send a notification that the reading has changed
}
}

myaccelerometer.h

#include <QAccelerometer> 
#include <QVariant>
 
QTM_USE_NAMESPACE
 
class MyAccelerometer : public QObject {
Q_OBJECT
public:
explicit MyAccelerometer(QObject *parent = 0);
private:
QAccelerometer *accelerometer;
signals:
void accelerationChanged(qreal, qreal, qreal);
public slots:
void updateAcceleration();

myaccelerometer.cpp

#include <QAccelerometerReading> 
#include "myaccelerometer.h"
#include "accelerometerfilter.h"
 
MyAccelerometer::MyAccelerometer(QObject *parent) :
QObject(parent), accelerometer(new QAccelerometer(this)) {
accelerometer->addFilter(new AccelerometerFilter(this)); // Adding the filter
connect(accelerometer, SIGNAL(readingChanged()), this, SLOT(updateAcceleration()));
accelerometer->start();
}
 
void MyAccelerometer::updateAcceleration() {
QAccelerometerReading *reading = accelerometer->reading();
emit accelerationChanged(reading->x(), reading->y(), reading->z());
}

Postconditions

This code snippet demonstrated how to reduce accelerometer noise using Qt.

Article Metadata
Tested with
Devices(s): Nokia N8-00, Nokia E7-00
Compatibility
Platform(s): Symbian, Maemo, Harmattan, Qt 4.7
Symbian
Article
Keywords: sensors, accelerometer, filter, noise
Created: makivioj (04 Jul 2011)
Last edited: hamishwillee (11 Oct 2012)
This page was last modified on 11 October 2012, at 01:23.
90 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×