I am using a "Microsoft Kinect for Windows" to track the $x$, $y$, and $z$ positions of my Shoulder, Elbow, Wrist, and Hand. Each position is normalised to my shoulder so that my shoulder is always at $[1,1,1]$, and other points are relative.
The output of this program is a $3$ by $4$ matrix, in the form:
$$ \begin{bmatrix} Sx & Ex & Wx & Hx \\ Sy & Ey & Wy & Hy \\ Sz & Ez & Wz & Hz \\ \end{bmatrix} $$
From this I want to calculate the abduction and flexion angle between the shoulder and elbow.The abduction and flexion motions are shown below. How would I do this?

The co-ordinate system:

My Attempt So Far
To calculate the abduction angle, I used the dot product formula referenced against a unit vector pointing down, defined as zero degrees:
$$ \theta=\cos^{-1}\frac{ \begin{bmatrix} 1\\ 0 \end{bmatrix}\cdot \begin{bmatrix} Ez\\ Ey \end{bmatrix} } { |\begin{bmatrix} 1\\ 0 \end{bmatrix}| |\begin{bmatrix} Ez\\ Ey \end{bmatrix}| } $$
But this gives a value of $30$ degrees when my arm is straight down (it should be zero), and it increases to $45$ degrees when my arm is straight up (it should be 180).
What equation should I actually be using?
If the coordinate of the shoulder joint is normalized to $(1,1,1)$, then the calculation of the angle of the shoulder-to-elbow rotation must be done relative to this point--that is to say, you must translate all the coordinates so that the shoulder joint is at the origin.
If the diagram with the person has the axes in correct orientation, then "down" would be the vector $$-\hat j = (0,-1,0)$$ and the rotation would be computed by the dot product $$(-\hat j) \cdot (\vec E - \vec S) = (0,-1,0) \cdot (E_x - 1, E_y - 1, E_z - 1)$$ with the angle given by $$\theta = \cos^{-1} \frac{1 - E_y}{\sqrt{(E_x - 1)^2 + (E_y - 1)^2 + (E_z - 1)^2}}.$$ This gives the rotation angle as measured in the plane of motion. To find the the component angles which are the projection of the above angle in the $xy$- and $yz$-planes, you would eliminate in the denominator the $z$- and $x$-coordinate contributions, respectively.