-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_filter.py
111 lines (90 loc) · 2.31 KB
/
test_filter.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
# fft_test.py
# simple test on the use of the fft
def bandpass_resp(fmin,fmax,dt):
"""
Create a bandpass filter and plot the response
Uses scipy, numpy
No data input necesary
Assumes data is a numpy array, column vector
"""
import math
import scipy.signal as signal
import numpy as np
import matplotlib.pyplot as plt
# Define nyquist and scale filter
fs = 1/dt
fnyq = 0.5*fs
f0 = fmin/fnyq
f1 = fmax/fnyq
wn = [f0,f1]
b, a = signal.butter(4, wn,'bandpass')
w, h = signal.freqz(b, a)
freq = fnyq*w/np.pi
plt.plot(freq, (abs(h)))
plt.plot(fmin, 0.5*np.sqrt(2), 'ko')
plt.plot(fmax, 0.5*np.sqrt(2), 'ko')
plt.axvline(fmin, color='k')
plt.axvline(fmax, color='k')
plt.title('Butterworth filter frequency response')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Amplitude ')
plt.grid(which='both', axis='both')
plt.show()
def bandpass(data,fmin,fmax,dt):
"""
Filter the signal in data using a Butterworth bandpass
filter, and a two pass filter. Similar to Matlab's approach
Uses scipy, numpy
Assumes data is a numpy array, column vector
"""
import scipy.signal as signal
import numpy as np
ndim = data.ndim
if ndim==1 :
x = data
x = x[:,np.newaxis]
# Define nyquist and scale filter
fnyq = 0.5/dt
f0 = fmin/fnyq
f1 = fmax/fnyq
print(fnyq,fmin,fmax,f0,f1)
wn = [f0,f1]
b, a = signal.butter(4, wn,'bandpass')
y = signal.filtfilt(b, a, x[:,0])
y = y[:,np.newaxis]
return y
import math
import numpy as np
import matplotlib.pyplot as plt
import random
import math
import numpy as np
import matplotlib.pyplot as plt
import random
npts = 50
f1 = 0.2
f2 = 0.1
f3 = 0.3
dt = 1.0
t = np.arange(npts)
xnoise = np.random.normal(0.0,0.1,npts)
xsin1 = 0.5*np.sin(2*math.pi*f2*t)
xsin2 = 0.5*np.sin(2*math.pi*f3*t)
data2 = xsin1 + xsin2 + 2.*xnoise
data3 = xsin1 + xsin2
data = np.sin(2*math.pi*f1*t)
bandpass_resp(f2,f3,1.0)
bandpass_resp(0.05,0.25,1.0)
bandpass_resp(0.25,0.40,1.0)
fdata = bandpass(data,f2,f3,1)
fdata2 = bandpass(data2,0.05,0.25,1)
fdata2b = bandpass(data2,0.25,0.4,1)
# Plot the data
plt.subplot(211)
plt.plot(t,data)
plt.plot(t,data2)
plt.subplot(212)
plt.plot(t, fdata,'r')
plt.plot(t, fdata2,'m')
plt.plot(t, fdata2b,'k')
plt.show()