spafe.features.spfeats#

  • Description : Spectral and frequency stats and features extraction algorithms implementation.

  • Copyright (c) 2019-2024 Ayoub Malek. This source code is licensed under the terms of the BSD 3-Clause License. For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.

class spafe.features.spfeats.SpectralFeats[source]#

Bases: TypedDict

spectral_skewness: float#
spectral_kurtosis: float#
spectral_entropy: float#
spectral_spread: float#
spectral_flatness: float#
spectral_rolloff: float#
spectral_flux: float#
spectral_mean: float#
spectral_rms: float#
spectral_std: float#
spectral_variance: float#
spafe.features.spfeats.spectral_centroid(fs: int, spectrum: ndarray, i: int = 1) float[source]#

Compute the spectral centroid (which is the barycenter of the spectrum) as described in [Peeters].

Parameters
  • fs (int) – input signal sampling rate.

  • spectrum (numpy.ndarray) – signal spectrum.

  • i (int) – centroid order. (Default is 1).

Returns

spectral centroid.

Return type

(float)

Note

\[\begin{split}\mu_{i} &= \frac{\sum_{n=0}^{N}f_{k}^{i}*a_{k}}{\sum_{n=0}^{N}a_{k}}\\ S_{centroid} &= \mu_{i}\\\end{split}\]
spafe.features.spfeats.spectral_skewness(sig: ndarray, fs: int, spectrum: ndarray)[source]#

Compute the spectral skewness (which is a measure of the asymmetry of a distribution around its mean) as described in [Peeters].

Parameters
Returns

spectral skewness.

Return type

(float)

Note

\[\begin{split}S_{skewness} = \mu_{3} &= \frac{\sum_{n=0}^{N}(f_{k} - \mu_{1})^{3} . a_k}{\mu_{2}^{3} . \sum_{n=0}^{N}a_{k}}\\\end{split}\]
spafe.features.spfeats.spectral_kurtosis(sig: ndarray, fs: int, spectrum: ndarray) float[source]#

Compute the spectral kurtosis (which is a measure of the flatness of a distribution around its mean) as described in [Peeters].

Parameters
Returns

spectral kurtosis.

Return type

(float)

Note

\[\begin{split}S_{kurtosis} = \mu_{4} &= \frac{\sum_{n=0}^{N}(f_{k} - \mu_{1})^{4} . a_k}{\mu_{2}^{4} . \sum_{n=0}^{N}a_{k}}\\\end{split}\]
spafe.features.spfeats.spectral_entropy(spectrum: ndarray) float[source]#

Compute the spectral entropy as described in [Misra].

Parameters

spectrum (numpy.ndarray) – signal spectrum.

Returns

spectral entropy.

Return type

(float)

Note

\[\begin{split}S_{entropy} &= \frac{\sum_{n=0}^{N}(f_{k} - \mu_{1})^{3} . a_k}{\mu_{2}^{3} . \sum_{n=0}^{N}a_{k}}\\\end{split}\]

References

Misra

: Misra, H., S. Ikbal, H. Bourlard, and H. Hermansky. β€œSpectral Entropy Based Feature for Robust ASR.” 2004 IEEE International Conference on Acoustics, Speech, and Signal Processing.

spafe.features.spfeats.spectral_spread(sig: ndarray, fs: int, spectrum: ndarray) float[source]#

Compute the spectral spread (basically a variance of the spectrum around the spectral centroid) as described in [Peeters].

Parameters
Returns

spectral spread.

Return type

(float)

Note

\[S_{spread} = \sqrt{\sum{0}{N}\frac{(f_k - \mu_{1}) . s_k}{\sum{0}{N}a_k}}\]
spafe.features.spfeats.spectral_flatness(spectrum: ndarray) float[source]#

Compute spectral flatness.

Args:

# spectrum (numpy.ndarray) : signal spectrum.

Returns:

(float) : spectral flatness value.

Note:
\[S_{flatness} = \frac{exp(\frac{1}{N}\sum_{k}log(a_{k}))}{\frac{1}{N}\sum_{k}a_{k}}\]
spafe.features.spfeats.spectral_rolloff(spectrum: ndarray, k: float = 0.85) float[source]#

Compute the spectral roll-off point which measures the bandwidth of the audio signal by determining the frequency bin under which a specified k percentage of the total spectral energy is contained below. see [Scheirer].

Parameters
  • spectrum (numpy.ndarray) – signal spectrum.

  • k (float) – constant. (Default is 0.85).

Returns

spectral rolloff point.

Return type

(float)

spafe.features.spfeats.spectral_flux(spectrum: ndarray, p: int = 2) float[source]#

Compute the spectral flux, which measures how quickly the power spectrum of a signal is changing. This implementation computes the spectral flux using the L2-norm per default i.e. the square root of the sum of absolute differences squared [Scheirer].

Parameters
  • spectrum (numpy.ndarray) – signal spectrum.

  • p (int) – norm type. (Default is 2).

Returns

spectral flux.

Return type

(float)

Note

\[S_{flux} = (\sum_{k}(|a_{k}(t) - a_{k}(t-1))^2}{\sqrt{\sum_{k}a_{k}(t-1)|^p)^{\frac{1}{p}}\]

References

Scheirer(1,2)

: Scheirer, E., and M. Slaney. β€œConstruction and Evaluation of a Robust Multifeature Speech/Music Discriminator.” 1997 IEEE International Conference on Acoustics, Speech, and Signal Processing, 1997.

spafe.features.spfeats.extract_feats(sig: ndarray, fs: int, nfft: int = 512) SpectralFeats[source]#

Compute various spectral features [Peeters].

Parameters
  • sig (numpy.ndarray) – input mono signal.

  • fs (int) – input signal sampling rate.

  • nfft (int) – number of FFT points. (Default is 512).

Returns

a dictionary including various frequency and spectral features (see Notes).

Return type

(dict)

Note

The resulting dictionary includes the following elements:

  • spectral features
    • spectral_centroid : spectral centroid.

    • spectral_skewness : frequencies skewness.

    • spectral_kurtosis : frequencies kurtosis.

    • spectral_entropy : spectral entropy.

    • spectral_spread : spectral spread.

    • spectral_flatness : spectral flatness.

    • spectral_rolloff : spectral rolloff.

    • spectral_flux : spectral flux.

    • spectral_mean : spectral mean.

    • spectral_rms : spectral root mean square.

    • spectral_std : spectral standard deviation.

    • spectral_variance : spectral variance.

Examples

from spafe.features.spfeats import extract_feats
from scipy.io.wavfile import read
from pprint import pprint

# read audio
fpath = "../../../tests/data/test.wav"
fs, sig = read(fpath)

# compute erb spectrogram
spectral_feats = extract_feats(sig, fs)
pprint(spectral_feats)

References

Peeters(1,2,3,4,5)

: Peeters, G. β€œA Large Set of Audio Features for Sound Description (Similarity and Classification) in the CUIDADO Project.” Technical Report; IRCAM: Paris, France, 2004.