-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
50 lines (35 loc) · 1.25 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Very simple module to calculate Root-Mean-Square values.
"""
import numpy as np
def combine(x, n=2, period=1, mean=True, noise=False, axis=0):
""" Combine several values, using a power of 'n' """
if not hasattr(x, '__iter__'): # Single value sent to function
return x
x = np.asarray(x) # convert to numpy array for easier handling
if noise:
x = 10 ** (x / 20) # convert from dB to SPL
x = x ** n # SQUARE
period = np.asarray(period)
if 1 < period.size < x.size:
raise ValueError(
'If more than one period is entered, lengths must match.'
)
x = x * period # Account for time. Used when sample intervals differ.
nans = np.isnan(x)
if nans.all():
return np.nan
x = np.nansum(x, axis=axis) # SUM
if mean: # MEAN
if period.size == 1:
divider = np.nansum(~nans, axis=axis) * period
else:
divider = np.nansum(period, axis=axis)
x /= divider
x = x ** (1 / n) # ROOT
if noise:
return 20 * np.log10(x) # convert back to dB
return x
def rms(x, period=1, noise=False, axis=0):
""" Calculate the Root-Mean-Square of 'x' """
return combine(x, n=2, period=period, noise=noise, axis=axis)