zeechimp commited on
Commit
97277f9
·
verified ·
1 Parent(s): 769c9b8

Create preprocessing.py

Browse files
Files changed (1) hide show
  1. preprocessing.py +35 -0
preprocessing.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from scipy.signal import butter, sosfiltfilt
3
+
4
+
5
+ def bandpass_filter(
6
+ voltage: np.ndarray, sample_rate: int, low_hz: float, high_hz: float
7
+ ) -> np.ndarray:
8
+ """
9
+ Zero-phase Butterworth bandpass along the sample axis.
10
+ voltage: (n_channels, n_samples)
11
+ """
12
+ sos = butter(
13
+ 4,
14
+ [low_hz / (sample_rate / 2), high_hz / (sample_rate / 2)],
15
+ btype="band",
16
+ output="sos",
17
+ )
18
+ return sosfiltfilt(sos, voltage, axis=1).astype(np.float32)
19
+
20
+
21
+ def extract_snippets(
22
+ spike_band: np.ndarray, positions: np.ndarray, length: int
23
+ ) -> np.ndarray:
24
+ """
25
+ Extract fixed-length snippets centred on the given positions.
26
+ Pads with zeros at the edges.
27
+ Returns: (n_spikes, n_channels, length)
28
+ """
29
+ half = length // 2
30
+ n_channels, n_samples = spike_band.shape
31
+ padded = np.pad(spike_band, ((0, 0), (half, half)), mode="constant")
32
+ out = np.empty((len(positions), n_channels, length), dtype=np.float32)
33
+ for i, p in enumerate(positions):
34
+ out[i] = padded[:, p : p + length]
35
+ return out