Let
- $H$ be a $\mathbb R$-Hilbert space
- $A\in\mathfrak L(H)$ be compact and self-adjoint
- $J:=\mathbb N\cap[0,|\sigma(A)\setminus\{0\}|]$ and $(\mu_j)_{j\in J}$ be an enumeration of $\sigma(A)\setminus\{0\}$
- $\pi_j$ denote the orthogonal projection of $H$ onto $$E_j:=\mathcal N(\mu_j-A),$$ $d_j:=\dim E_j$ and $\left(e^{(j)}_1,\ldots,e^{(j)}_{d_j}\right)$ be an orthonormal basis of $E_j$ for $j\in J$
- $I:=\mathbb N\cap[0,\operatorname{rank}A]$ and \begin{align}(\lambda_i)_{i\in I}&:=(\underbrace{\mu_1,\ldots,\mu_1}_{=:\:d_1\text{ times}},\underbrace{\mu_2,\ldots,\mu_2}_{=:\:d_2\text{ times}},\ldots),\\(e_i)_{i\in I}&:=\left(e^{(1)}_1,\ldots,e^{(1)}_{d_1},e^{(2)}_1,\ldots,e^{(2)}_{d_2},\ldots\right)\end{align}
By definition, $$\pi_j=\sum_{i=1}^{d_j}e^{(j)}_i\otimes e^{(j)}_i\;\;\;\text{for all }j\in J\tag1$$ and $$A=\sum_{j\in J}\mu_j\pi_j=\sum_{i\in I}\lambda_ie_i\otimes e_i\tag2.$$
As indicated in equation $(2)$ of this answer, we may write $$A=U^\ast DU\tag3$$ for some orthogonal $U\in\mathfrak L(H)$ and a "diagonal" $D\in\mathfrak L(H)$. But how are $U$ and $D$ constructed?
In light of $(2)$, the right-hand side of $(2)$ should be the "diagonal" operator $D$ (and I guess this notion has to be understood with respect to a reference orthonormal system (or basis?)) and I suppose that $U$ is then some kind of basis transformation.