-
Notifications
You must be signed in to change notification settings - Fork 21
/
filters.py
137 lines (93 loc) · 3.84 KB
/
filters.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
''' Functions used for bandpass filtering and freuquency band generation'''
import numpy as np
from scipy import signal
from scipy.signal import butter, sosfilt, sosfreqz
__author__ = "Michael Hersche and Tino Rellstab"
__email__ = "herschmi@ethz.ch,tinor@ethz.ch"
# def bandpass_filter(signal_in,f_band_nom):
# ''' Filter N channels with fir filter of order 101
# Keyword arguments:
# signal_in -- numpy array of size [NO_channels, NO_samples]
# f_band_nom -- normalized frequency band [freq_start, freq_end]
# Return: filtered signal
# '''
# numtabs = 101
# h = signal.firwin(numtabs,f_band_nom,pass_zero=False)
# NO_channels ,NO_samples = signal_in.shape
# sig_filt = np.zeros((NO_channels ,NO_samples))
# for channel in range(0,NO_channels):
# sig_filt[channel] = signal.convolve(signal_in[channel,:],h,mode='same') # signal has same size as signal_in (centered)
# return sig_filt
def bandpass_filter(signal_in,f_band_nom):
''' Filter N channels with fir filter of order 101
Keyword arguments:
signal_in -- numpy array of size [NO_channels, NO_samples]
f_band_nom -- normalized frequency band [freq_start, freq_end]
Return: filtered signal
'''
order = 4
sos = butter(order, f_band_nom, analog=False, btype='band', output='sos')
sig_filt = sosfilt(sos, signal_in)
return sig_filt
def load_bands(bandwidth,f_s,max_freq = 40):
''' Filter N channels with fir filter of order 101
Keyword arguments:
bandwith -- numpy array containing bandwiths ex. [2,4,8,16,32]
f_s -- sampling frequency
Return: numpy array of normalized frequency bands
'''
f_bands = np.zeros((99,2)).astype(float)
band_counter = 0
for bw in bandwidth:
startfreq = 4
while (startfreq + bw <= max_freq):
f_bands[band_counter] = [startfreq, startfreq + bw]
if bw ==1: # do 1Hz steps
startfreq = startfreq +1
elif bw == 2: # do 2Hz steps
startfreq = startfreq +2
else : # do 4 Hz steps if Bandwidths >= 4Hz
startfreq = startfreq +4
band_counter += 1
# convert array to normalized frequency
f_bands_nom = 2*f_bands[:band_counter]/f_s
return f_bands_nom
def load_filterbank(bandwidth,fs, order = 4, max_freq = 40,ftype = 'butter'):
''' Calculate Filters bank with Butterworth filter
Keyword arguments:
bandwith -- numpy array containing bandwiths ex. [2,4,8,16,32]
f_s -- sampling frequency
Return: numpy array containing filters coefficients dimesnions 'butter': [N_bands,order,6] 'fir': [N_bands,order]
'''
f_band_nom = load_bands(bandwidth,fs,max_freq) # get normalized bands
n_bands = f_band_nom.shape[0]
if ftype == 'butter':
filter_bank = np.zeros((n_bands,order,6))
elif ftype == 'fir':
filter_bank = np.zeros((n_bands,order))
for band_idx in range(n_bands):
if ftype == 'butter':
filter_bank[band_idx] = butter(order, f_band_nom[band_idx], analog=False, btype='band', output='sos')
elif ftype == 'fir':
filter_bank[band_idx] = signal.firwin(order,f_band_nom[band_idx],pass_zero=False)
return filter_bank
def butter_fir_filter(signal_in,filter_coeff):
if filter_coeff.ndim == 2: # butter worth
return sosfilt(filter_coeff, signal_in)
elif filter_coeff.ndim ==1: # fir filter
NO_channels ,NO_samples = signal_in.shape
sig_filt = np.zeros((NO_channels ,NO_samples))
for channel in range(0,NO_channels):
sig_filt[channel] = signal.convolve(signal_in[channel,:],filter_coeff,mode='same') # signal has same size as signal_in (centered)
return sig_filt
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