-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyFilters.py
64 lines (49 loc) · 1.82 KB
/
MyFilters.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
# Keep this here because making a filter is never as simple as I would expect
from scipy.signal import freqz, lfilter, butter
from scipy.signal import filtfilt as ff
import matplotlib.pyplot as plt
import numpy as np
# http://stackoverflow.com/questions/12093594/
# how-to-implement-band-pass-butterworth-filter-with-scipy-signal-butter
def butter_bandpass(lowcut, highcut, Fs, order=5):
nyq = 0.5 * Fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
return b, a
def butter_bandpass_filter(
data, lowcut, highcut, Fs, order=5, plot_filter=False, filtfilt=True,
axis=-1):
b, a = butter_bandpass(lowcut, highcut, Fs, order=order)
y = ff(b, a, data, axis) if filtfilt else lfilter(b, a, data, axis)
if plot_filter:
filter_plot(b, a, Fs)
return y
def butter_highpass(highcut, Fs, order=5):
nyq = 0.5 * Fs
high = highcut / nyq
b, a = butter(order, high, btype='highpass')
return b, a
def butter_highpass_filter(
data, highcut, Fs, order=5, plot_filter=False, filtfilt=True, axis=-1):
b, a = butter_highpass(highcut, Fs, order=order)
y = ff(b, a, data, axis) if filtfilt else lfilter(b, a, data, axis)
if plot_filter:
filter_plot(b, a, Fs)
return y
def butter_lowpass(lowcut, Fs, order=5):
nyq = 0.5 * Fs
low = lowcut / nyq
b, a = butter(order, low, btype='lowpass')
return b, a
def butter_lowpass_filter(
data, lowcut, Fs, order=5, plot_filter=False, filtfilt=True, axis=-1):
b, a = butter_lowpass(lowcut, Fs, order=order)
y = ff(b, a, data, axis) if filtfilt else lfilter(b, a, data, axis)
if plot_filter:
filter_plot(b, a, Fs)
return y
def filter_plot(b, a, Fs):
w, h = freqz(b, a)
fig, ax = plt.subplots()
ax.plot((Fs*0.5/np.pi)*w, abs(h))