From 1dc49e588dfe0d2d8e8f3df29ab4c0ebb26150b8 Mon Sep 17 00:00:00 2001 From: Eric Denovellis Date: Tue, 5 Jun 2018 16:13:27 -0400 Subject: [PATCH] Speed up hilbert transform by zero padding the fft/ifft --- ripple_detection/core.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ripple_detection/core.py b/ripple_detection/core.py index 6d69826..61484e3 100644 --- a/ripple_detection/core.py +++ b/ripple_detection/core.py @@ -5,6 +5,7 @@ import numpy as np import pandas as pd +from scipy.fftpack import next_fast_len from scipy.io import loadmat from scipy.ndimage.filters import gaussian_filter1d from scipy.signal import filtfilt, hilbert, remez @@ -207,7 +208,10 @@ def _extend_segment(segments_to_extend, containing_segments): def get_envelope(data, axis=0): '''Extracts the instantaneous amplitude (envelope) of an analytic signal using the Hilbert transform''' - return np.abs(hilbert(data, axis=axis)) + n_samples = data.shape[axis] + instantaneous_amplitude = np.abs( + hilbert(data, N=next_fast_len(n_samples), axis=axis)) + return np.take(instantaneous_amplitude, np.arange(n_samples), axis=axis) def gaussian_smooth(data, sigma, sampling_frequency, axis=0, truncate=8):