Skip to content

Algorithms | latest | band-energy-ratio

Band energy ratio

The band energy ratio is defined as the ratio of the band energy in an arbitrary frequency band [fl,fu) and the total energy. This feature is also known as partial power. fl and fu are the lower and upper frequency bounds of the band.

BandEnergyRatio=m=mlmu1Xp[m]m=0M1Xp[m]Xp=|X|2ml=Mfl2fsmu=Mfu2fs

Where m[0,M) is the bin index of the spectrum X and the power spectrum Xp.

References

Code

INFO

The following snippet is written in a generic and unoptimized manner. The code aims to be comprehensible to programmers familiar with various programming languages and may not represent the most efficient or idiomatic Python practices. Please refer to implementations for optimized implementations in different programming languages.

py
import numpy as np


def band_energy_ratio(
    spectrum: np.ndarray, samplerate: float, f_lower: float, f_upper: float
) -> float:
    powerspectrum = np.abs(spectrum) ** 2
    n = len(powerspectrum)
    n_lower = int(2 * n * f_lower / samplerate)
    n_upper = int(2 * n * f_upper / samplerate)
    return np.sum(powerspectrum[n_lower:n_upper]) / np.sum(powerspectrum)
Run in playground