# HG changeset patch # User Amine Sehili # Date 1567630093 -3600 # Node ID 6dcfe7bf1c942edaa5316f66c2ce74d83ff17e4a # Parent 0a5f9566b069e40892a41895c5182dfdc2ed8407 Handle special cases in signal conversion and energy calculation diff -r 0a5f9566b069 -r 6dcfe7bf1c94 auditok/signal.py --- 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) diff -r 0a5f9566b069 -r 6dcfe7bf1c94 auditok/signal_numpy.py --- 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)