code: private float getQuaternionW(float x, float y, float z) {
          return (float) Math.cos(Math.asin(Math.sqrt(x * x + y * y + z * z)));
      }
docs: Finds the missing value. Seems to lose a degree of freedom, but it
  doesn't. That degree of freedom is already lost by the sensor.