getHNR: Get HNR

View source: R/spectralDescr.R

getHNRR Documentation

Get HNR

Description

Calculates the harmonics-to-noise ratio (HNR), that is, the ratio of the intensity of the harmonic component to the intensity of the noise component, following Boersma (1993). Normally called internally by analyze, but can also be called directly on a time series.

Usage

getHNR(
  x = NULL,
  samplingRate = NA,
  acf_x = NULL,
  lag_min = 2,
  lag_max = NULL,
  interpol = c("sinc", "spline", "parab", "none"),
  wn = "hanning",
  idx_max = NULL,
  win_sinc = NULL
)

Arguments

x

a numeric vector (time series). Provide either x or acf_x, not both.

samplingRate

sampling rate, Hz

acf_x

pre-computed normalized autocorrelation of x (e.g. from acf_fft), if already available. If supplied, x and wn are ignored for ACF computation.

lag_min, lag_max

minimum and maximum lag (in samples) to search for the ACF peak. Defaults: lag_min = 2, lag_max = length(x) / 2 (or length(acf_x) if acf_x is given).

interpol

method of refining the peak location: 'sinc' = windowed sinc interpolation with Brent's search (most accurate); 'spline' = cubic spline upsampling; 'parab' = parabolic interpolation on three points; 'none' = no interpolation.

wn

window function applied to x before ACF computation (ignored when acf_x is supplied). Also used for the sinc interpolation kernel.

idx_max

(internal) the lag of the ACF peak, if already known. Skips the peak search.

win_sinc

(internal) a pre-computed window of length 2 * min(250, floor(length(acf_x) / 2)) for the sinc interpolation, to avoid rebuilding it on every call. If NULL (default), the window is computed internally.

Value

A list:

f0

frequency (Hz) corresponding to the ACF peak

max_acf

height of the ACF peak, 0 to 1

HNR

harmonics-to-noise ratio in dB: 10 * log10(max_acf / (1 - max_acf))

References

Boersma, P. (1993). Accurate short-term analysis of the fundamental frequency and the harmonics-to-noise ratio of a sampled sound. In Proceedings of the Institute of Phonetic Sciences (Vol. 17, No. 1193, pp. 97–110).

Examples

signal = sin(2 * pi * 150 * (1:16000) / 16000)
signal = signal / sqrt(mean(signal ^ 2))
noise = rnorm(16000)
noise = noise / sqrt(mean(noise ^ 2))
SNR = 40  # ground truth
s = signal + noise * 10 ^ (-SNR / 20)
soundgen:::getHNR(s, 16000, lag_min = 16000 / 1000,
  lag_max = 16000 / 75, interpol = 'none')
soundgen:::getHNR(s, 16000, lag_min = 16000 / 1000,
  lag_max = 16000 / 75, interpol = 'sinc')

soundgen documentation built on Sept. 20, 2026, 5:07 p.m.