audmath¶

db¶

audmath.db(x, *, bottom=-120)[source]

Convert value to decibels.

The decibel of a value $$x \in \R$$ is given by

$\text{db}(x) = \begin{cases} 20 \log_{10} x, & \text{if } x > 10^\frac{\text{bottom}}{20} \\ \text{bottom}, & \text{else} \end{cases}$

where $$\text{bottom}$$ is provided by the argument of same name.

Parameters
Return type
Returns

input value(s) in dB

Example

>>> db(1)
0.0
>>> db(0)
-120.0
>>> db(2)
6.020599913279624
>>> db([0, 1])
array([-120.,    0.])


inverse_db¶

audmath.inverse_db(y, *, bottom=-120)[source]

Convert decibels to amplitude value.

The inverse of a value $$y \in \R$$ provided in decibel is given by

$\text{inverse\_db}(y) = \begin{cases} 10^\frac{y}{20}, & \text{if } y > \text{bottom} \\ 0, & \text{else} \end{cases}$

where $$\text{bottom}$$ is provided by the argument of same name.

Parameters
Return type
Returns

input signal

Example

>>> inverse_db(0)
1.0
>>> inverse_db(-120)
0.0
>>> inverse_db(-3)
0.7079457843841379
>>> inverse_db([-120, 0])
array([0., 1.])


inverse_normal_distribution¶

audmath.inverse_normal_distribution(y)[source]

Inverse normal distribution.

Returns the argument $$x$$ for which the area under the Gaussian probability density function is equal to $$y$$. It returns $$\text{nan}$$ if $$y \notin [0, 1]$$.

The area under the Gaussian probability density function is given by:

$\frac{1}{\sqrt{2\pi}} \int_{-\infty}^x \exp(-t^2 / 2)\,\text{d}t$

This function is a numpy port of the Cephes C code. Douglas Thor implemented it in pure Python under GPL-3.

The output is identical to the implementation provided by scipy.special.ndtri(), and scipy.stats.norm.ppf(), and allows you to avoid installing and importing scipy. audmath.inverse_normal_distribution() is slower for large arrays as the following comparison of execution times on a standard PC show.

Samples

scipy

audmath

10.000

0.00s

0.01s

100.000

0.00s

0.09s

1.000.000

0.02s

0.88s

10.000.000

0.25s

9.30s

Parameters

y (Union[int, float, Sequence, ndarray]) – input value

Return type
Returns

inverted input

Example

>>> inverse_normal_distribution([0.05, 0.4, 0.6, 0.95])
array([-1.64485363, -0.2533471 , 0.2533471 , 1.64485363])


rms¶

audmath.rms(x, *, axis=None, keepdims=False)[source]

Root mean square.

The root mean square for a signal of length $$N$$ is given by

$\sqrt{\frac{1}{N} \sum_{n=1}^N x_n^2}$

where $$x_n$$ is the value of a single sample of the signal.

For an empty signal 0 is returned.

Parameters
Return type
Returns

root mean square of input signal

Example

>>> rms([])
0.0
>>> rms([0, 1])
0.7071067811865476
>>> rms([[0, 1], [0, 1]])
0.7071067811865476
>>> rms([[0, 1], [0, 1]], keepdims=True)
array([[0.70710678]])
>>> rms([[0, 1], [0, 1]], axis=1)
array([0.70710678, 0.70710678])


window¶

audmath.window(samples, shape='tukey', half=None)[source]

Return a window.

The window will start from and/or end at 0. If at least 3 samples are requested and the number of samples is odd, the windows maximum value will always be 1.

The shape of the window is selected via shape The following figure shows all available shapes. For the Kaiser window we use $$\beta = 14$$ and set its first sample to 0.

Parameters
• samples (int) – length of window

• shape (str) – shape of window

• half (Optional[str]) – if None return whole window, if 'left' or 'right' return left or right half-window. Other than the whole window the half-windows will always contain 1 as maximum value as long as samples > 1

Return type

ndarray

Returns

window

Raises
• ValueError – if shape is not supported

• ValueError – if half is not supported

Example

>>> window(7)
array([0.  , 0.25, 0.75, 1.  , 0.75, 0.25, 0.  ])
>>> window(6)
array([0.  , 0.25, 0.75, 0.75, 0.25, 0.  ])
>>> window(5, shape='linear', half='left')
array([0.  , 0.25, 0.5 , 0.75, 1.  ])