From f9132cf4ecf8ca6fe24b83a2121acda269b08a51 Mon Sep 17 00:00:00 2001 From: marksdestiny Date: Sun, 11 Jun 2023 17:02:50 +0200 Subject: [PATCH] Improve integrate_bands performance Reduce the memory consumption and improve the performance when using the integrate_bands function on large signals. --- acoustics/signal.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/acoustics/signal.py b/acoustics/signal.py index 4481ae6..1ba363a 100644 --- a/acoustics/signal.py +++ b/acoustics/signal.py @@ -711,30 +711,34 @@ def density_spectrum(x, fs, N=None): return np.fft.fftshift(f), np.fft.fftshift(fr) -def integrate_bands(data, a, b): +def integrate_bands(data, dataf, bandf): """ Reduce frequency resolution of power spectrum. Merges frequency bands by integration. :param data: Vector with narrowband powers. - :param a: Instance of :class:`Frequencies`. - :param b: Instance of :class:`Frequencies`. + :param dataf: Instance of :class:`Frequencies`. + :param bandf: Instance of :class:`Frequencies`. .. note:: Needs rewriting so that the summation goes over axis=1. """ try: - if b.fraction % a.fraction: + if bandf.fraction % dataf.fraction: raise NotImplementedError("Non-integer ratio of fractional-octaves are not supported.") except AttributeError: pass - lower, _ = np.meshgrid(b.lower, a.center) - upper, _ = np.meshgrid(b.upper, a.center) - _, center = np.meshgrid(b.center, a.center) - - return ((lower < center) * (center <= upper) * data[..., None]).sum(axis=-2) + dataCount = len(data) + bandCount = len(bandf.center) + bandPower = np.zeros(bandCount) + for bandIndex in range(0, bandCount - 1): + upper = np.repeat(bandf.upper[bandIndex], dataCount) + lower = np.repeat(bandf.lower[bandIndex], dataCount) + bandPower[bandIndex] = ((lower < dataf.center) * (dataf.center <= upper) * data).sum() + return bandPower + def bandpass_frequencies(x, fs, frequencies, order=8, purge=False, zero_phase=False): """"Apply bandpass filters for frequencies