Seeing how I was able to find no solution to this issue, I started to look at other possible ways of handling the sensors.
I found out a much better way of handling the sensor data, that eliminated not only the azimuth swaying, but also the need to have a filter to smooth the sensor values:
Instead of using Sensor.TYPE_ACCELEROMETER
and Sensor.TYPE_MAGNETIC_FIELD
values to calculate the rotation matrix, I used the Sensor.TYPE_ROTATION_VECTOR
sensor. Then, got the rotation matrix from this, and as done earlier, got the orientation array from it.
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
switch (sensorEvent.sensor.getType()) {
case Sensor.TYPE_ROTATION_VECTOR:
rotationMatrix=new float[16];
SensorManager.getRotationMatrixFromVector(rotationMatrix, sensorEvent.values);
}
SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X,
SensorManager.AXIS_Z, outRotationMatrix); // Remap coordinate System to compensate for the landscape position of device
SensorManager.getOrientation(outRotationMatrix, orientationValues);
currentOrientation = (float) (Math.toDegrees(orientationValues[0]) + this.getDeclination()); //Azimuth; (Degrees);
eyeLevelInclination = (float) Math.toDegrees(orientationValues[1]); //Pitch; (Degrees); down is 90 , up is -90.
deviceOrientation = (float) Math.toDegrees(orientationValues[2]); // Roll;
sendSensorBroadcast(); // Let other classes know of update to sensor data.
As you can see in my question above, I was using a low pass filter to smooth the azimuth and pitch values, but this wasn't needed anymore, as the values were pretty smooth without any filtering. Plus, the azimuth swaying is almost gone now. I say almost because you can maybe still see a bit of it happening, but its barely noticeable, maybe about half a degree.
I was stuck with this problem for quite some time, hopefully this will help someone else too.