I would like to estimate the "harmonicity" (harmonics-to-noise ratio ?) of an audio signal from its spectrum. What kind of algorithm can I use?

  • 1
    $\begingroup$ What's the mathematical formula that you use to define harmonicity ? It must have an explicit mathematical definition, otherwise you can neither estimate nor compute it. Please add that formula to the question. $\endgroup$
    – Fat32
    Jul 10 '17 at 11:41
  • $\begingroup$ perhaps to add to Fat's question: what does it mean to have a totally harmonic signal? or a totally inharmonic signal? if "harmonic" means periodic and "inharmonic" means lacking any component of periodicity (like white noise), then i would measure hamonicity to be the maximum value of the autocorrelation (other than the peak at a lag of 0) normalized by the maximum value that is at a lag of 0. $\endgroup$ Jul 10 '17 at 20:24

Please take a look at this previous answer. Using the semantics and mathematical definitions from that answer...

Let $x[n]$ be the input signal from which you want to measure harmonicity or periodicity. Assume $x[n]$ has no DC component (like it is the output of a DC blocking filter). And $n_0$ is the sample index of the neighborhood of signal around where you want the harmonicity to be measured. $N$ is a large integer defining how wide your rectangular window is.

Average Squared Difference Function, ASDF:

$$ Q_x[k, n_0] \triangleq \frac{1}{N} \sum\limits_{n=0}^{N-1} \left(x[n+n_0-\left\lfloor \tfrac{N+k}{2}\right\rfloor] \ - \ x[n+n_0-\left\lfloor \tfrac{N+k}{2}\right\rfloor + k] \right)^2 $$

$\left\lfloor \cdot \right\rfloor$ is the floor() function and, if $k$ is even then $ \left\lfloor \frac{k}{2}\right\rfloor = \left\lfloor \frac{k+1}{2}\right\rfloor = \frac{k}{2} $.

From that define an alternative form of Autocorrelation:

$$ R_x[k,n_0] = R_x[0,n_0] - \frac12 Q_x[k, n_0] $$


$$ R_x[0, n_0] \triangleq \frac{1}{N} \sum\limits_{n=0}^{N-1} (x[n+n_0-\left\lfloor \tfrac{N}{2}\right\rfloor])^2 $$

Since $Q_x[0, n_0] = 0$ and $Q_x[k, n_0] \ge 0$ for all lags $k$, that means that $ R_x[k, n_0] \le R_x[0, n_0] $ for all lags $k$.

You will find a period $P$ such that $P > 0$ and $R_x[P, n_0] > R_x[k, n_0]$ for all lags $k>k_0$, other than the highest peak at $k=0$.

$k_0$ is the smallest value of $k>0$ such that $ R_x[k, n_0]<0 $.

Once that peak location $P$ is found, the harmonicity is

$$ r_x(n_0) \triangleq \frac{R_x[P, n_0]}{R_x[0, n_0]} $$

If $r_x(n_0) = 1$, then $x[n]$ is perfectly periodic (or "harmonic") in the vicinity of sample index $n_0$. If $r_x(n_0) = 0$, it's noise, no periodicity to be found.

  • $\begingroup$ Thanks for your answer. However, as I asked, I would like to compute the harmonicity of the spectrum and not of the signal. $\endgroup$
    – sandoval31
    Oct 19 '17 at 14:14
  • $\begingroup$ Then inverse Fourier transform the spectrum into a signal. Or consider what implication these operations in the time domain have in the frequency domain. This will be about comb filtering. $\endgroup$ Jun 13 '21 at 23:32

To answer the broad question "what algorithms you could use": There are different ways of computing an harmonics-to-noise ratio, in the time or in frequency, from basic interval cutting in the frequency domain to more involved cepstral/liftering techniques with local peak detection, using a limited number of partials, etc. Advantages and drawbacks of different methods are described for instance in Temporal and spectral estimations of harmonics-to-noise ratio in human voice signals, Qi and Hillman, 1997

Other potential sources :

From the sources above, you have possibilities to make your questions more precise, and help contributors to provide more accurate contributions.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.