-
Notifications
You must be signed in to change notification settings - Fork 0
/
downsampled.py
79 lines (65 loc) · 2.6 KB
/
downsampled.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon May 9 16:32:55 2022
@author: genzel
"""
import os, sys, json
import numpy as np
import pandas as pd
import mdaio
from scipy import signal
from scipy.signal import butter, lfilter
from timeit import default_timer as timer
from scipy.signal import butter, sosfilt, sosfreqz
def butter_bandpass(lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
sos = butter(order, [low, high], analog=False, btype='band', output='sos')
return sos
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
sos = butter_bandpass(lowcut, highcut, fs, order=order)
y = sosfilt(sos, data)
return y
directory = input("Enter the directory: ")
print(directory)
#directory = '/media/genzel/data/spikesorting/Rat/rat2/Rat_Hm_Ephys_Rat2_389237_20200915_postsleep.mountainsort/'
import pathlib
def main():
for filename in os.listdir(directory):
f = os.path.join(directory, filename)
# checking if it is a file
extension = f[-4:]
check = f[-14:]
if os.path.isfile(f):
print(f)
start = timer()
if extension == '.mda' and check != 'timestamps.mda':
studyday = filename[25:33]
postsleep = mdaio.readmda(f)
postsleep = postsleep[:,:108000000*4]
postsleep = np.transpose(postsleep).astype(int)
recording = pd.DataFrame()
for j in range(4):
fe = 30000
f_nyq = fe/2
fc1 = 1
fc2 = 300
recording1 = butter_bandpass_filter(postsleep[:, j], fc1, fc2, fe, order=6)
q=10
down1 = signal.decimate(recording1,q)
q=5
down1 = signal.decimate(down1,q)
ch1 = pd.DataFrame(down1)
recording = pd.concat([recording,ch1], axis=1)
recording.columns = ['wavech1','wavech2','wavech3','wavech4']
recording = recording.to_numpy()
recording = recording.transpose()
if not os.path.exists(directory+'/StudyDay'+str(studyday)+'/'):
os.makedirs(directory+'/StudyDay'+str(studyday)+'/')
mdaio.writemda16i(recording,directory+'/StudyDay'+str(studyday)+'/'+filename);
end = timer()
print(f'elapsed time: {end - start}')
if __name__ == '__main__':
main()