/ concept-collection / benchcompress
Sign in
concept-collection / benchcompress
benchcompress / helpers.py
44 lines · 1.4 KBBlameHistoryRaw
1import numpy as np
3def bandpass_filter(array, *, sampling_frequency, lowcut, highcut) -> np.ndarray:
4 from scipy.signal import butter, lfilter
6 nyquist = 0.5 * sampling_frequency
7 low = lowcut / nyquist
8 high = highcut / nyquist
9 b, a = butter(5, [low, high], btype="band")
10 return lfilter(b, a, array, axis=0) # type: ignore
12def lowpass_filter(array, *, sampling_frequency, highcut) -> np.ndarray:
13 from scipy.signal import butter, lfilter
15 nyquist = 0.5 * sampling_frequency
16 high = highcut / nyquist
17 b, a = butter(5, high, btype="low")
18 return lfilter(b, a, array, axis=0) # type: ignore
21def highpass_filter(array, *, sampling_frequency, lowcut) -> np.ndarray:
22 from scipy.signal import butter, lfilter
24 nyquist = 0.5 * sampling_frequency
25 low = lowcut / nyquist
26 b, a = butter(5, low, btype="high")
27 return lfilter(b, a, array, axis=0) # type: ignore
30def estimate_noise_level(array: np.ndarray, *, sampling_frequency: float) -> float:
31 array_filtered = highpass_filter(
32 array, sampling_frequency=sampling_frequency, lowcut=300
33 )
34 MAD = float(
35 np.median(np.abs(array_filtered.ravel() - np.median(array_filtered.ravel())))
36 / 0.6745
37 )
38 return MAD
41def compute_entropy_per_sample(a):
42 _, counts = np.unique(a, return_counts=True)
43 p = counts / len(a)
44 return -np.sum(p * np.log2(p))
moveopenescclose