# periodicity coefficient

I wonder if an efficient method exist to compute how much a signal is periodic, it should be ~1.0 when the signal is totally periodic (like a sinusoïdal signal) and ~0.0 when totally random, like a white noise.

Edit : This question came with a big misunderstanding, it has been modified to fit the answer.

• Can you define what you mean by "stationariness"? Stationary has a mathematical meaning for random signals. White Gaussian noise is stationary. So it's not clear what you mean that the "stationary coefficent" should be 0 for white noise. Aug 7, 2017 at 14:44
• erf ! I realize I was totally wrong with my understanding of what's a "stationary" signal. I will edit my question... I d'like to measure how much a signal is periodic. e.g. it should be ~1.0 for a pure sinusoïd at 50hz and ~0.0 for a totally random signal. what term should I use ? periodicness ? Aug 7, 2017 at 16:27
• @snoobdogg How about spectral flatness, AKA Wiener Entropy? Keep in mind that it is flatness, not "peakiness", so it will be 1 for noise and 0 for sinusoids.
– jojek
Aug 7, 2017 at 16:58
• @snoobdogg I think you mean periodicity in time, or, "peakiness" of the Fourier spectrum is what you are after. If there are strong peaks in the spectrum then the signal is not totally random. Whereas for white Gaussian noise, there are no peaks as all the energy is equally distributed over all frequency bands. I would go with jojek's idea of using spectral flatness as a metric. Aug 7, 2017 at 18:33
• Ok thank you, it's clearly what's I'm looking for : ) Aug 7, 2017 at 19:31

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 "tonalness" 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)$$

• – jojek
Jan 9, 2021 at 12:23

The unbiased, normalized autocorrelation function (ACF) $r_{xx}[\tau]$ directly computes the desired "periodicity coefficient". It is defined as

$$r_{xx}[\tau] = \frac{N}{\left\|x\right\|_2^2\cdot(N-\tau)}\sum_{i=0}^{N-\tau-1}x[i]x[i+\tau]\quad\text{ for }0\leq \tau \leq (N-1),$$

with $\left\|x\right\|_2^2$ the squared (Frobenius) norm (e.g., the energy) and $N$ the length of $x$.

You just have to determine the height of the first maximum with a time lag $\tau$ different from zero, $\tau_\text{max} = \text{arg max}\left\{r_{xx}[\tau]\right\}\text{ for }\tau \neq 0$.

• If $r_{xx}[\tau_\text{max}]=1$ the signal is periodic with period $\tau_\text{max}$,
• If $r_{xx}[\tau_\text{max}]=-1$ the signal is periodic with period $\tau_\text{max}$ with the phase-inverted version of itself,
• If $r_{xx}[\tau_\text{max}]=0$ the signal is not periodic.

Values $-1 \leq r_{xx}[\tau_\text{max}] \leq 1$ are a measure for to which degree the signal is periodic.

The periodicity measure you asked for, with a range of $0\ldots 1$, can, e.g., be implemented by discarding all values $r_{xx}[\tau_\text{max}] < 0$, resulting in:

$$p = \text{max}\left\{0, r_{xx}[\tau_\text{max}]\right\}.$$

• there is a real problem with that method/definition for when $\tau \approx N$ and the window gets smaller and smaller. This answer and this answer describe a way to adapt the Average Squared Difference Function to an autocorrelation-like function that compares well to $r_{xx}(\tau)$ for small $\tau$. Aug 13, 2017 at 14:36