periodicity coefficient
I suggest using Spectral Flatness, aka Wiener Entropy. It is defined as a ratio of geometric and arithmetic mean of the magnitude spectra $X(k)$:
$$\Xi=\dfrac{\sqrt[K]{\prod_{k=0}^{K} X(k)}}{\frac{1}{K}\sum_{k=0}^{K}X(k)} $$
For signals which have flat spectra, its value tends towards $1$, whereas for tonal signals it is close to $0$. In your particular application, you might want to consider $1-\Xi$ as a measure of &quot;tonalness&quot; in your signal.

Equivalently, in order to avoid underflows, one can replace the geometric mean implementation with $$GM=\exp\left( \frac{1}{K} \sum_{k=0}^{K}\ln X(k) \right) $$