Mercurial > hg > auditok
changeset 256:6dcfe7bf1c94
Handle special cases in signal conversion and energy calculation
author | Amine Sehili <amine.sehili@gmail.com> |
---|---|
date | Wed, 04 Sep 2019 21:48:13 +0100 |
parents | 0a5f9566b069 |
children | b3095d23d0c3 |
files | auditok/signal.py auditok/signal_numpy.py |
diffstat | 2 files changed, 7 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/auditok/signal.py Mon Sep 02 22:17:29 2019 +0100 +++ b/auditok/signal.py Wed Sep 04 21:48:13 2019 +0100 @@ -20,7 +20,8 @@ array(fmt, all_channels[ch::channels]) for ch in range(channels) ] avg_arr = array( - fmt, (sum(samples) // channels for samples in zip(*mono_channels)) + fmt, + (round(sum(samples) / channels) for samples in zip(*mono_channels)), ) return avg_arr @@ -34,7 +35,7 @@ def calculate_energy_single_channel(x): - energy = max(sum(i ** 2 for i in x) / len(x), 1e-20) + energy = max(sum(i ** 2 for i in x) / len(x), _EPSILON) return 10 * math.log10(energy)
--- a/auditok/signal_numpy.py Mon Sep 02 22:17:29 2019 +0100 +++ b/auditok/signal_numpy.py Wed Sep 04 21:48:13 2019 +0100 @@ -15,7 +15,7 @@ def average_channels(data, fmt, channels): array = np.frombuffer(data, dtype=fmt).astype(np.float64) - return array.reshape(-1, channels).mean(axis=1) + return array.reshape(-1, channels).mean(axis=1).round() def separate_channels(data, fmt, channels): @@ -24,9 +24,11 @@ def calculate_energy_single_channel(x): - return 10 * np.log10(np.dot(x, x).clip(min=_EPSILON) / x.size) + x = np.asarray(x) + return 10 * np.log10((np.dot(x, x) / x.size).clip(min=_EPSILON)) def calculate_energy_multichannel(x, aggregation_fn=np.max): + x = np.asarray(x) energy = 10 * np.log10((x * x).mean(axis=1).clip(min=_EPSILON)) return aggregation_fn(energy)