-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsquiggle_processing_utils.py
More file actions
39 lines (32 loc) · 1.33 KB
/
squiggle_processing_utils.py
File metadata and controls
39 lines (32 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import scipy.signal as scisignal
import numpy as np
from dtw import dtw
from dtw import warp as dtw_warp
def de_noise_signal(signal):
# TODO track down this justification
# Bessel filter order and Wn chosen based on how good some chart looked to me at some point
fs = 3012 # Sampling frequency in Hz
cutoff_freq = 100 # Cutoff frequencies in Hz
nyquist = 0.5 * fs
Wn = cutoff_freq / nyquist
order = 8
b, a = scisignal.bessel(order, Wn, 'low')
return scisignal.filtfilt(b, a, signal)
def compute_dtw_distance(signal1, signal2):
alignment = dtw(signal1, signal2, step_pattern="symmetric1", distance_only=True)
return alignment.distance
def compute_dtw_warp(signal1, signal2):
alignment = dtw(signal1, signal2, step_pattern="symmetric1", keep_internals=True)
return dtw_warp(alignment, index_reference=False)
def scale_and_shift(signal, scale, shift):
return signal * scale + shift
def rescale_to_range(signal, new_min, new_max):
scale, shift = get_rescale_factors(signal, new_min, new_max)
return scale_and_shift(signal, scale, shift)
def get_rescale_factors(signal, new_min, new_max):
signal = np.array(signal)
orig_min = np.min(signal)
orig_max = np.max(signal)
scale = (new_max - new_min) / (orig_max - orig_min)
shift = new_min - orig_min * scale
return scale, shift