Calculate angle from accelerometer on a moving axis For the purpose of this question, let's say that I have an accelerometer that is on a foot of a human. ( red square on by the heel in the below photo )

I'd calculate the angle the following way, as the leg goes from step 1 to step 2.
$\theta = tan^{-1}(\frac{x}{y})$
At step 1, the angle would be $0$, as $y = -1$ and $x = 0$. As the leg moves towards step 2, it would come closer and closer to $90^{\circ}$. If the leg could be raised enough to be parallel to the waist, it would be a full $90^{\circ}$, given the assumption that if $y == 0$, the result equates to $90^{\circ}$.
Now that I have illustrated what I am doing, I am curious to monitor what happens on the z axis. The base assumption thus far has been that the person will go from Step 1, to Step 2, and not swing their leg back and forth. So what I want to do is, as the person is moving their leg, I want to measure the angular change on the Z axis. i.e Is the person moving their leg back and forth while going from Step 1 to Step 2.
This has me a bit stumped at the moment, because I do not know which of the other two axis I should use to determine the change in angle on the Z axis. I'd expect to see the following:
As the person moves the leg back over time, but continues from step 1 to step 2, that the angle goes from $0^{\circ}$ to $-90^{\circ}$. As the person moves the leg forwards, but continues from step 1 to step 2, that the angle on the z axis goes from $0^{\circ}$ to $90^{\circ}$.

In the plane $y-z$ the rotation would be
$$\alpha = \tan^{-1}\left(\frac{z}{y}\right)$$
with positive values for $\alpha$ when moving back.
A general expression for the absolute angle between the leg and y axis is
$$\phi = \tan^{-1}\left(\frac{\sqrt{x^2+z^2}}{|y|}\right)$$
For a general description you could use spherical coordinates system using the standard axis system

$$\begin{align} r&=\sqrt{x^2 + y^2 + z^2} \\ \theta &= \arccos\frac{z}{\sqrt{x^2 + y^2 + z^2}} = \arccos\frac{z}{r} \\ \varphi &= \arctan \frac{y}{x} \end{align}$$
Note that the inverse tangent must be suitably defined, taking into account the correct quadrant.
Setting, with your reference system


*

*$z=-Y$

*$y= Z$

*$x=-X$

