-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnoise_reduction-BW-MA-paramtest.py
194 lines (163 loc) · 6.62 KB
/
noise_reduction-BW-MA-paramtest.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 11 16:52:43 2017
@author: Andy
"""
## -*- coding: utf-8 -*-
import glob
import matplotlib.pyplot as plt
import cPickle as pkl
import numpy as np
from scipy import signal
import csv
import numpy as np
from scipy.signal import butter, lfilter, freqz
import matplotlib.pyplot as plt
import copy
import Functions as Fun
import SignalProcessingFunctions as SPF
# User parameters
folder = 'Code from John 10-14\\'
# saveFolder = '..\\..\\..\\Data\\t_aMax_aMin2_aMean_EP\\'
name = 'all_data.pkl'
QList = np.array([500]) #np.array([500,1000,1500,2000,2500,3000,3250])
RPMList = np.array([120]) #np.array([0,30,120,250,500,750,1000])
conditionList = ['Dry'] #['Dry','Water', '3mM SDS', '6.5mM SDS', '10mM SDS', 'Water with 10mM SDS']
diskFrac = 0.9
viewTimeSignal = False
# Derived Quantities and Constants
diskRad = 15 # cm
wetTarget = diskFrac * diskRad
# Cleanup existing windows
plt.close('all')
# Get list of data files to process
fileList = glob.glob(folder+name)
N1 = len(fileList)
for j in range(N1):
# Load data and get list of fields
if 'allData' not in globals():
with open(fileList[j],'rb') as f:
allData = pkl.load(f)
print fileList[j]
keyList = allData.keys()
N = len(keyList)
# Initialize data storage arrays
plotData = {}
for condition in conditionList:
plotData[condition] = {}
plotData[condition]['ePMax'] = np.zeros((len(QList),len(RPMList)))
plotData[condition]['wetTime'] = np.zeros((len(QList),len(RPMList)))
plotData[condition]['wetVolume'] = np.zeros((len(QList),len(RPMList)))
for i in range(N):
# Parse the experiment data
data = allData[keyList[i]]
RPM = data['RPM']
QTarget = data['flowRate']
condition = data['condition']
if condition not in conditionList:
continue
if (QTarget not in QList) or (RPM not in RPMList):
continue
# Set the indicies of the array for assignment of plotting data
ind0 = np.argmax(QList==QTarget)
ind1 = np.argmax(RPMList==RPM)
# Parse remaining experiment data
fps = data['fps']
t0 = data['t0']
time = data['time']
aMax = data['aMax']
aMin2 = data['aMin2']
aMean = data['aMean']
eP = data['excessPerimeter']
if len(time) < 1:
continue
# Adjust data fields
timeRinsing = time[time > t0] - t0
aMax = aMax[time > t0]
aMin2 = aMin2[time > t0]
aMean = aMean[time > t0]
eP = eP[time > t0]
Q = Fun.convert_flowrate(QTarget)
#######################################################################
### REDUCE NOISE ###
#######################################################################
################### BUTTERWORTH FILTER ################################
print 'Butterworth Filter'
for cutoff in [10,20,30]:
# Filter requirements.
order = 6
fs = fps # sample rate, Hz
# cutoff = 20 # desired cutoff frequency of the filter, Hz
# Get the filter coefficients so we can check its frequency response.
b, a = SPF.butter_lowpass(cutoff, fs, order)
# Plot the frequency response.
w, h = freqz(b, a, worN=8000)
plt.subplot(2, 1, 1)
plt.plot(0.5*fs*w/np.pi, np.abs(h), 'b')
plt.plot(cutoff, 0.5*np.sqrt(2), 'ko')
plt.axvline(cutoff, color='k')
plt.xlim(0, 0.5*fs)
plt.title("Lowpass Filter Frequency Response")
plt.xlabel('Frequency [Hz]')
plt.grid()
# Filter the data, and plot both the original and filtered signals.
ePFiltered = SPF.butter_lowpass_filter(eP, cutoff, fs, order)
plt.subplot(2, 1, 2)
plt.plot(time, eP, 'b-', label='data')
plt.plot(time, ePFiltered, 'g-', linewidth=2, label='filtered data')
plt.xlabel('Time [sec]')
plt.grid()
plt.legend()
plt.subplots_adjust(hspace=0.35)
plt.show()
#######################################################################
###################### LOCAL AVERAGE #################################
print 'Local Average Filter'
# Filter
for n in range(0,101,20):
eP_LA = SPF.local_average_filter(eP, n)
# Plot
# plt.plot(time, eP, 'b-', label='data')
plt.plot(time, eP_LA, 'g-', linewidth=2, label='filtered data')
plt.xlabel('Time [sec]')
plt.grid()
plt.legend()
pltTitle = 'Local Average Filtering with n = ' + str(n)
plt.title(pltTitle)
plt.show()
#######################################################################
######################### MEDIAN FILTER ###############################
print 'Median Filter'
# Filter
for n in range (1, 156, 20):
eP_MF = signal.medfilt(eP, n) # note: n must be odd
# Plot
# plt.plot(time, eP, 'b-', label='data')
plt.plot(time, eP_MF, 'g-', linewidth=2, label='filtered data')
plt.xlabel('Time [sec]')
plt.grid()
plt.legend()
pltTitle = 'Median Filtering with n = ' + str(n)
plt.title(pltTitle)
plt.show()
#######################################################################
######################## KILL SPIKES (cf. Tom O'Haver, UMD, 2016) #####
### !!! TODO seems to do nothing...
print 'Kill Spikes'
# Parameters
thresh = 0.2
width = 150
# Filter
eP_KS = SPF.kill_spikes_filter(time, eP, width, thresh)
# Plot
# plt.plot(time, eP, 'b-', label='data')
plt.plot(time, eP_KS, 'g-', linewidth=2, label='filtered data')
plt.xlabel('Time [sec]')
plt.grid()
plt.legend()
pltTitle = 'Kill-spikes filtering with width = ' + str(width) + ' and thresh = ' + str(thresh)
plt.title(pltTitle)
plt.show()
#######################################################################
##################
#