This code calculates the estimated direction of the gravity vector, based on the current value of the quaternion.
// Estimated direction of gravity in the body frame (factor of two divided out)
vx = q[1] * q[3] - q[0] * q[2];
vy = q[0] * q[1] + q[2] * q[3];
vz = q[0] * q[0] - 0.5f + q[3] * q[3];
To obtain the so-called "linear acceleration" you subtract this from the measured acceleration (eg. ax, ay, az) after scaling the two vectors appropriately.
After they are both scaled in terms of m/s^2 ( 1 g = 9.8 m/s^2) then you can subtract the individual terms directly, e.g. the x acceleration in the body frame is (ax-vx) m/s^2.