forked from abactor/Ballagumi-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_sync_V2.py
320 lines (230 loc) · 9.77 KB
/
parse_sync_V2.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import sys, os
import string
import time
import struct
from struct import *
import datetime
from datetime import datetime
import scipy
from scipy import signal as sc
from scipy.io.wavfile import read, write
import numpy as np
from multiprocessing import Process
import PySide
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import *
from PySide.QtUiTools import *
import pylab
import math
import csv
import matplotlib
matplotlib.rcParams['backend.qt4']='PySide'
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
################################################****************************###########################################
class mapperData():
def __init__(self):
self.signals = {} # A dict keeping count of the signal values for each signal
self.times = {} # A dict keeping count of the timestamps for each signal
self.smoothsignals = {}
self.signal_names = []
self.signal_values = []
self.timestamps_seconds = []
self.duration_seconds = []
self.filename = ""
self.audio_filename = ""
def changeToDuration(self):
for t in self.timestamps_seconds:
delta = int(t) - int(self.timestamps_seconds[0])
self.duration_seconds.append(delta)
def changeNTPToUTC(self,times):
import datetime
values_utc = []
times_utc = {}
t = datetime.datetime(1900, 1, 1)
for key, value in self.times.iteritems():
if key not in times_utc:
times_utc[key]=[]
for i in range(len(value)):
delta = datetime.timedelta(seconds=value[i])
t_utc = t + delta
times_utc[key].append(t_utc)
print "TIME VALUE Of and Example Signal In UTC", times_utc['/Fungible1.1/1/LeftBase/A-B/COMP'][0]
return times_utc
def getTwosComp(self,val, bits):
#"""compute the 2's compliment of int value val"""
if( (int(val) & (1<<(bits-1))) != 0 ):
val = int(val) - (1<<bits)
return val
def parseAudioData(self):
self.sampling_rate, self.audioInput = read(self.audio_filename)
print 'Sampling Rate', self.sampling_rate, " "#, self.audioInput
def plotAudioData(self,subplot):
#self.audioInput_ds = scipy.signal.decimate(self.audioInput,10,n=8,ftype='fir',axis=-1)
#obtain x-axis
p2_x = np.arange(0,len(self.audioInput))
# decimate signal manually for plotting
p2_audioInput_ds = []
p2_x_ds = []
for i in range(0, len(self.audioInput), 20):
p2_audioInput_ds.append(self.audioInput[i])
p2_x_ds.append(p2_x[i]/self.sampling_rate)
#print "decimating audio input",
#print "audio input length", len(self.audioInput), "decimated input length", len(p2_audioInput_ds), " ", len(p2_x_ds)
p2 = subplot.plot(p2_x_ds,p2_audioInput_ds)
subplot.set_xlabel('Time (Seconds)')
subplot.set_ylabel('Audio Data from Synthesizer')
subplot.grid(b=None,which='major')
title("Sensor vs. Audio Signal - Ballagumi")
def parseData(self):
if len(self.filename) >= 2:
datafile = open(self.filename,'U')
data_line = csv.reader(datafile, dialect='excel')
for line in data_line:
line = str(line)
line = line.replace("']","")
line = line.replace("['","")
line = line.strip()
#Get Values from Line
line_sections = line.split(" ")
self.signal_names.append(line_sections[2])
self.signal_values.append(float(line_sections[5]))
self.timestamps_seconds.append(int(line_sections[0]))
self.changeToDuration()
for i, sig in enumerate(self.signal_names):
current_name = self.signal_names[i]
current_val = self.signal_values[i]
current_time = self.duration_seconds[i]
# Modify the sensor data before feeding them to the dictionary
current_val = self.getTwosComp(current_val,8)
if current_name not in self.signals:
self.signals[current_name] = []
self.signals[current_name].append(current_val)
self.times[current_name] = []
self.times[current_name].append(current_time)
else:
self.signals[current_name].append(current_val)
self.times[current_name].append(current_time)
else:
print "Exception, Filename Not Available"
# Plot all the sensor signals on one plot together
def plot_allsensorsignals(self,subplot):
plot_array=[]
time_array=[]
legend_array=[]
for key in sorted(self.signals.iterkeys()):
plot_array.append(self.signals[key])
for key in sorted(self.times.iterkeys()):
time_array.append(self.times[key])
legend_array.append(self.signals.keys())
for i in range(len(plot_array)):
p1 = subplot.plot(time_array[i],plot_array[i])
subplot.set_xlabel('Time (Seconds)')
subplot.set_ylabel('Sensor Data from Ballagumi')
subplot.grid(b=None,which='major')
for label in subplot.xaxis.get_ticklabels():
label.set_rotation(55)
label.set_fontsize(10)
fontP = FontProperties()
fontP.set_size('small')
box = subplot.get_position()
subplot.set_position([box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9]) # Shink current axis's height by 10% on the bottom
#l1 = subplot.legend(legend_array, prop=fontP, ncol = 3, loc='upper center', bbox_to_anchor=(0.5, -0.1))
def plot_sensorsignal(self,subplot,signalname):
plot_array=[]
time_array=[]
legend_array=[]
for key, value in self.signals.iteritems():
plot_array.append(self.signals[key])
if key == signalname:
print 'Found Key', key, 'Matching Signal Name', signalname
p1 = subplot.plot(self.times[key],self.signals[key])
plot_name = "Plot for Signal: " + str(signalname)
subplot.set_xlabel('Time (Seconds)')
subplot.set_ylabel('Audio Data from Synth')
subplot.grid(b=None,which='major')
for label in subplot.xaxis.get_ticklabels():
label.set_rotation(55)
label.set_fontsize(10)
legend_array.append(key)
box = subplot.get_position()
subplot.set_position([box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9]) # Shink current axis's height by 10% on the bottom
subplot.legend(legend_array, "upper right")
def smooth_data(self):
for key,value in self.signals.iteritems():
if key not in self.smoothsignals:
self.smoothsignals[key]=[]
for index, val in enumerate(value):
if (index < len(value)-1):
if (abs(value[index] - value[index-1])>=10 and abs(value[index+1] - value[index])>=10):
value[index] = value [index-1]
self.smoothsignals[key] = value
class SensorView(QWidget):
def __init__(self):
super(SensorView, self).__init__()
self.currentData = mapperData()
self.initUI()
def initUI(self):
self.button = QPushButton("Display Plots")
self.button.setGeometry(10,50,400,30)
self.button.setParent(self)
self.button.clicked.connect(self.plotQSignal)
self.signalComboBox = QComboBox()
self.signalComboBox.setGeometry(10,10,400,30)
self.signalComboBox.setParent(self)
self.signalComboBox.activated.connect(self.getSignalName)
# self.setLayout(hbox)
self.setGeometry(500,300,450,350)
self.setWindowTitle('Ballagumi Data Sync')
self.show()
def fillQBox(self):
# Add all the signal names from the device to the combobox
if dict(self.currentData.signals):
for name in self.currentData.signals.iterkeys():
#print name
self.signalComboBox.addItem(name)
else:
print "Empty Signals Dictionary"
def getSignalName(self):
print 'Selected Sensor Signal', self.signalComboBox.currentText()
self.current_signal = self.signalComboBox.currentText()
def plotQSignal(self):
self.plotWindow = QMainWindow()
self.plotWindow.resize(600,600)
# generate the plot
fig = Figure(figsize=(500,400), dpi=72, facecolor=(1,1,1), edgecolor=(0,0,0))
# subplot 1 - Audio Data Stream
ax1 = fig.add_subplot(211)
self.currentData.plot_sensorsignal(ax1,self.current_signal)
# subplot 2 - Sensor Data Stream
ax2 = fig.add_subplot(212)
self.currentData.plotAudioData(ax2)
#self.currentData.plot_allsensorsignals(ax2)
self.canvas = FigureCanvas(fig)
self.plotWindow.setCentralWidget(self.canvas)
self.plotWindow.show()
######### END OF QWIDGET CLASS - ON TO MAIN ##########
def main():
app = QApplication(sys.argv)
ex = SensorView()
if len(sys.argv) == 3:
ex.currentData.filename = sys.argv[1]
ex.currentData.audio_filename = sys.argv[2]
ex.currentData.parseData()
ex.currentData.parseAudioData()
elif len(sys.argv) == 2:
ex.currentData.filename = sys.argv[1]
ex.currentData.parseData()
ex.currentData.audioInput = [0,1]
#ex.currentData.parseAudioData()
else:
print "Error, mapperRec File Not Included"
sys.exit()
ex.fillQBox()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
# (times, signals_sm) = smooth_data(times,signals)