AudiooPy 0.5

https://sourceforge.net/projects/audioopy/

Module audioopy

Class BaseVolume

Description

A base class to estimate the rms - root-mean-square.

It provides a framework for estimating and analyzing the volume (rms) of a signal over a specified window length. It includes methods to calculate various statistical measures such as mean, median, variance, standard deviation, and z-scores of the volume values.

The main functionalities of the BaseVolume class include:

  • Estimating the volume (RMS) of a signal.
  • Calculating statistical measures such as mean, median, variance, standard deviation, and z-scores.
  • Providing access to individual volume values and the entire list of volumes.
Example
 >>> bv = BaseVolume(win_len=0.02)
 >>> bv._volumes = [0.1, 0.2, 0.3, 0.4, 0.5]
 >>> print(bv.mean())
 > 0.3
 >>> print(bv.max())
 > 0.5
 >>> print(bv.zscores())
 > [-1.2649110640673518, -0.6324555320336759, 0.0, 0.6324555320336759, 1.2649110640673518]

Constructor

Create a BaseVolume instance.

Parameters
  • win_len: (float) Size of the window (in seconds)
Raises
  • ValueError: if win_len is not a float
View Source
def __init__(self, win_len: float=0.01):
    """Create a BaseVolume instance.

    :param win_len: (float) Size of the window (in seconds)
    :raises: ValueError: if win_len is not a float

    """
    self._volumes = list()
    self._rms = 0.0
    self._winlen = 0.01
    self.set_winlen(win_len)

Public functions

get_winlen

Return the windows length used to estimate the volume values.

Returns
  • (float) Duration in seconds.
View Source
def get_winlen(self):
    """Return the windows length used to estimate the volume values.

        :return: (float) Duration in seconds.

        """
    return self._winlen

set_winlen

Fix the windows length used to estimate the volume values.

Parameters
  • win_len: (float) Size of the window (in seconds)
Returns
  • (float) Assigned value for the window length.
View Source
def set_winlen(self, win_len: float):
    """Fix the windows length used to estimate the volume values.

        :param win_len: (float) Size of the window (in seconds)
        :return: (float) Assigned value for the window length.

        """
    win_len = float(win_len)
    if 0.002 <= win_len <= 0.2:
        self._winlen = win_len
    return self._winlen

volume

Return the global volume value (rms).

Returns
  • (float)
View Source
def volume(self):
    """Return the global volume value (rms).

        :return: (float)

        """
    return self._rms

volume_at

Return the value of the volume at a given index.

Returns
  • (float)
Raises
  • IndexError: if index is out of range
Parameters
  • index
View Source
def volume_at(self, index: int):
    """Return the value of the volume at a given index.

        :return: (float)
        :raise: IndexError: if index is out of range

        """
    return self._volumes[index]

volumes

Return the list of volume values (rms).

Returns
  • (list of float)
View Source
def volumes(self):
    """Return the list of volume values (rms).

        :return: (list of float)

        """
    return self._volumes

len

Return the number of RMS values that were estimated.

Returns
  • (int)
View Source
def len(self):
    """Return the number of RMS values that were estimated.

        :return: (int)

        """
    return len(self._volumes)

min

Return the minimum of RMS values.

Returns
  • (float) Return the min rms or 0. if no values.
View Source
def min(self):
    """Return the minimum of RMS values.

        :return: (float) Return the min rms or 0. if no values.

        """
    if len(self._volumes) == 0:
        return 0.0
    return min(self._volumes)

max

Return the maximum of RMS values.

Returns
  • (float) Return the max rms or 0. if no values.
View Source
def max(self):
    """Return the maximum of RMS values.

        :return: (float) Return the max rms or 0. if no values.

        """
    if len(self._volumes) == 0:
        return 0.0
    return max(self._volumes)

mean

Return the mean of RMS values.

Returns
  • (float) Return the mean rms or 0. if no values.
View Source
def mean(self):
    """Return the mean of RMS values.

        :return: (float) Return the mean rms or 0. if no values.

        """
    if len(self._volumes) == 0:
        return 0.0
    return math.fsum(self._volumes) / len(self._volumes)

median

Return the median of RMS values.

Returns
  • (float) Return the median rms or 0. if no values.
View Source
def median(self):
    """Return the median of RMS values.

        :return: (float) Return the median rms or 0. if no values.

        """
    if len(self._volumes) == 0:
        return 0.0
    middle = len(self._volumes) // 2
    if len(self._volumes) % 2:
        return self._volumes[middle]
    newlist = sorted(self._volumes)
    return float(newlist[middle] + newlist[middle - 1]) / 2.0

variance

Return the real variance of RMS values.

variance is 1/N * sum[(i-rms_mean)^2]

Returns
  • (float) Return the variance or 0. if no values.
View Source
def variance(self):
    """Return the real variance of RMS values.

        variance is 1/N * sum[(i-rms_mean)^2]

        :return: (float) Return the variance or 0. if no values.

        """
    if len(self._volumes) == 0:
        return 0.0
    rms_mean = self.mean()
    return math.fsum((pow(i - rms_mean, 2) for i in self._volumes)) / float(len(self._volumes))

stdev

Return the standard deviation of RMS values.

stdev is sqrt(variance)

Returns
  • (float) Return the stdev or 0. if no values.
View Source
def stdev(self):
    """Return the standard deviation of RMS values.

        stdev is sqrt(variance)

        :return: (float) Return the stdev or 0. if no values.

        """
    if len(self._volumes) == 0:
        return 0.0
    return math.sqrt(self.variance())

coefvariation

Return the coefficient of variation of RMS values.

Returns
  • (float) coefficient of variation given as a percentage.
View Source
def coefvariation(self):
    """Return the coefficient of variation of RMS values.

        :return: (float) coefficient of variation given as a percentage.

        """
    m = self.mean()
    if m < 0.001:
        return 0.0
    percent_mean = m * 100.0
    return self.stdev() / percent_mean

zscores

Return the z-scores of RMS values.

The z-score determines the relative location of a data value.

Returns
  • (list of float)
View Source
def zscores(self):
    """Return the z-scores of RMS values.

        The z-score determines the relative location of a data value.

        :return: (list of float)

        """
    if len(self._volumes) == 0:
        return []
    all_scores = [0.0] * len(self._volumes)
    mean = self.mean()
    stdev = self.stdev()
    for i, rms_value in enumerate(self._volumes):
        all_scores[i] = (rms_value - mean) / stdev
    return all_scores

stderr

Calculate the standard error of the RMS values.

Returns
  • (float)
View Source
def stderr(self):
    """Calculate the standard error of the RMS values.

        :return: (float)

        """
    if len(self._volumes) == 0:
        return 0.0
    return self.stdev() / float(math.sqrt(len(self._volumes)))

Overloads

__len__

View Source
def __len__(self):
    return len(self._volumes)

__iter__

View Source
def __iter__(self):
    for x in self._volumes:
        yield x

__getitem__

View Source
def __getitem__(self, i):
    return self._volumes[i]