-
Notifications
You must be signed in to change notification settings - Fork 6
/
sensor_GUI.py
826 lines (711 loc) · 31.7 KB
/
sensor_GUI.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
"""
Name: ECG v3 python code for wireless transmission and ADC conversion
Purpose: Plots the voltages communicated through UART from ez430-RF2500
and filters the high frequency interference
Author: Ben Peng, University of Rochester, NY.
Updated: Feb 8 2012
"""
#import warnings
#warning.filterwarnings("ignore", category=SyntaxWarning)
import os
import pprint
import random
import sys
import wx
import serial
import scipy
import math
import csv
# The recommended way to use wx with mpl is with the WXAgg
# backend.
#
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import \
FigureCanvasWxAgg as FigCanvas, \
NavigationToolbar2WxAgg as NavigationToolbar
import numpy as np
import pylab
buffersize = 4600 # data to be shown in screen
# 4600: 20 sec
buffer_for_DSP = 230 # must less than buffer size 1397 is the total sample one ADC can be stored in flash mem for Fs=230Hz
# 230: 1 sec
# 460: 2 sec
cutoff_pt = 40 # cutoff point 25 is arount 20 Hz
x_length_factor = 10 # enter times of buffer_for_DSP
PROC = 0
# ==================================== define DataRead =========================================
class DataRead(object): # DataRead has been defined as class
"""
This class reads data from the MSP430 serial port
"""
#Flag to tell if new data has been read
drawIt = 0
counter = 0
readingData = 0
#Variable arrays to keep track of data; [] is list which is albe to changed
A0 = []
A1 = []
A2 = []
A3 = []
A4 = []
A12 = []
A13 = []
A14 = []
xValues = []
#initialize arrays to have [buffersize] elements, will keep track of
#the last [buffersize] data points
for i in range(buffersize):
A0.append(0)
A1.append(0)
A2.append(0)
A3.append(0)
A4.append(0)
A12.append(0)
A13.append(0)
A14.append(0)
xValues.append(i)
#Global variable to keep track of serial port configuration
ser = serial.Serial()
ser.baudrate = 9600
ser.timeout = .1
port = 0
def __init__(self): # Everytime for DataRead must go through this path. this is for the definition of CMD welcome msg
print "\n------------------------------------------------------------------"
print "MSP430 ADC value viewer\nPress Ctrl+C to exit at any time"
print "------------------------------------------------------------------"
print "Available serial ports:"
for n,s in self.scan():
print "(%d) %s" % (n,s)
#Ask user which serial port to open
while self.port == 0:
self.port = raw_input("\nWhich serial port is connected to the MSP430?\n\
(Enter the number in parentheses or 'skip' for no connection)")
if self.port == "skip":
break
try:
self.port = int(self.port)
except ValueError:
self.port = 0
print "error: port selection must be an integer"
#Got the serial port information, use it
if self.port != "skip":
self.ser.port = self.port
try:
self.ser.open()
except:
print "Could not connect to serial port %d, exiting" %self.port
exit()
if self.ser.isOpen() == True:
print "Serial port " + self.ser.portstr + " successfully opened, listening for data"
print "Press Ctrl+C to exit or select File->Exit from the menu bar\n"
else:
print "Skipping serial connection, no serial port will be opened"
def startADC(self): # define what to do for startADC.
#send the ADC start signal through the serial port
if self.ser.isOpen() == True:
#send the letter s
print "Sending ADC start signal through serial port"
self.ser.write("s")
else:
print "Could not send ADC start signal, serial port is not open"
def read(self):
#read data from the serial port
if self.ser.isOpen() == True:
#read a serial port until endline ("\n")
serialmsg = self.ser.readline()
#if we read something, parse and convert ADC values to volts
if len(serialmsg) > 0:
if len(serialmsg)> 4 and serialmsg[4] == ',':
#parse message data
serialmsg = serialmsg.strip()
ADCvals = serialmsg.split(', ')
#Check for complete data set notification
if ADCvals[0] == '0682' \
and ADCvals[1] == '0682' \
and ADCvals[2] == '0682' \
and ADCvals[3] == '0682' \
and ADCvals[4] == '0682' \
and ADCvals[5] == '0682' \
and ADCvals[6] == '0682' \
and ADCvals[7] == '0682':
self.drawIt = 1
self.readingData = 0
#----- want to run ADC automatically -------
self.startADC()
#-------------------------------------------
else:
try:
for i in range (len(ADCvals)):
ADCvals[i] = float(ADCvals[i])*3.3/1023
print ["%0.3f" %i for i in ADCvals]
self.update_data_arrays(ADCvals)
except ValueError:
pass
else:
print serialmsg,
else:
if self.port != "skip":
print "serial port not open"
#Scan function from the pySerial example code
def scan(self):
"""scan for available ports. return a list of tuples (num, name)"""
available = []
for i in range(256):
try:
s = serial.Serial(i)
available.append( (i, s.portstr))
s.close() # explicit close 'cause of delayed GC in java
except serial.SerialException:
pass
return available
def closeSerial(self):
"""Closes serial port for clean exit"""
print "Closing serial port"
try:
self.ser.close();
except:
pass
#This function updates the data arrays when given the latest data
# @staticmethod
# def update_plot_lock():
# global PROC
# PROC = 1
# print "Udated completed: PROC:", PROC
def update_data_arrays(self, lastReading):
if len(lastReading) >= 8:
self.A0.append(lastReading[0])
self.A1.append(lastReading[1])
self.A2.append(lastReading[2])
self.A3.append(lastReading[3])
self.A4.append(lastReading[4])
self.A12.append(lastReading[5])
self.A13.append(lastReading[6])
self.A14.append(lastReading[7])
self.A0.pop(0)
self.A1.pop(0)
self.A2.pop(0)
self.A3.pop(0)
self.A4.pop(0)
self.A12.pop(0)
self.A13.pop(0)
self.A14.pop(0)
self.counter += 1
if self.counter%8 == 0: # this one control the figure flash rate! 50 is original
self.drawIt = 1
if self.counter%(buffer_for_DSP) == 0: # want to save data length for DSP
#----- want to save data automatically
path_default = os.getcwd()+'/sample.csv'
self.save_data_arrays_auto(path_default)
# self.PROC = 1 # hope to enable plot the new data
global PROC
PROC = 1
print "plot new processed data PROC:", PROC
# app_rev = wx.PySimpleApp()
# app_rev.frame_rev = GraphFrame_rev()
# app_rev.frame_rev.draw_plot_rev(self.PROC)
# #app_rev.frame_rev.Show()
# app_rev.MainLoop()
else:
print "Could not append corrupted data"
# self.startADC()
def save_data_arrays(self, path):
#write the data to the file
datastring = ""
thefile = open(path, 'w')
#Write data Labels to file
datastring = "A0, A1, A2, A3, A4, A12, A13, A14\n"
thefile.write(datastring)
#Now write the data
for i in range(buffersize):
datastring = "%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f\n" %(self.A0[i],
self.A1[i], self.A2[i], self.A3[i], self.A4[i], self.A12[i], self.A13[i], self.A14[i])
# datastring = "%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f\n" %(self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i],
# self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i], self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i], self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i], self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i], self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i], self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i], self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i])
thefile.write(datastring)
thefile.close()
#print "save successful"
#-------------------save sliced data for DSP -----------------------------
def save_data_arrays_auto(self, path):
#write the data to the file
datastring = ""
thefile = open(path, 'w')
thefile_rev = open(os.getcwd()+'/dif.csv', 'w')
#Write data Labels to file
datastring = "A0, A1, A2, A3, A4, A12, A13, A14\n"
thefile.write(datastring)
#Now write the data
#for i in range(buffer_for_DSP): # can change for later data operation
for i in range(buffersize-buffer_for_DSP, buffersize): # can change for later data operation
datastring = "%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f\n" %(self.A0[i],
self.A1[i], self.A2[i], self.A3[i], self.A4[i], self.A12[i], self.A13[i], self.A14[i])
# datastring = "%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f\n" %(self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i],
# self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i], self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i], self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i], self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i], self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i], self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i], self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i])
thefile.write(datastring)
#----- want to get the differential signal from Electrode 2 and Electrode 3 for period depends on buffer_for_DSP
#dif_signal = self.A1[i] - self.A12[i] #self.A3[i] - self.A12[i]
dif_signal = self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i]
#dif_signal = self.A14[i] - self.A12[i] - self.A1[i] + self.A3[i] #self.A3[i] - self.A12[i]
dif_string = "%.3f\n" %(dif_signal)
thefile_rev.write(dif_string)
#transpose_dif = transpose(dif_signal)
#print dif_signal
thefile.close()
thefile_rev.close()
print "save diff successfully"
#----- want to open dif signal and do ifft --------------------------------------
dif_sig = csv.reader(open("dif.csv", "rU"),delimiter='\n')
dif_sig_list = []
dif_sig_list.extend(dif_sig)
signal = []
for data in dif_sig_list:
signal.append(data[0])
#print signal
def convertStr(s):
try:
ret = int(s)
except ValueError:
ret = float(s)
return ret
t = range(len(signal))
for i in range(len(signal)):
t[i] = convertStr(signal[i])
raw_fft = scipy.fft(t) # (G) and (H)
bp = raw_fft.copy()
# bp *= real(fft_raw.dot(raw_fft))/real(bp.dot(bp))
for i in range(len(bp)): # (H-red)
if i>=cutoff_pt:
bp[i]=0
reconstruct = abs(scipy.ifft(bp))
np.savetxt('reconstruct.txt', reconstruct)
print "processed data is ready to use"
#self.PROC = 1
#-----------------------------------------------------------------------
#--------------------------------------------------
## definition of 2nd class, the first one is ReadData
class GraphFrame(wx.Frame):
""" The main frame of the application
"""
title = 'MSP430 ADC Value Plotter'
refreshCounter = 0
# PROC_cue = 0
def __init__(self):
wx.Frame.__init__(self, None, -1, self.title)
#Instantiate DataRead class to talk to serial port
self.dataread = DataRead()
#Create wx window
self.create_menu()
self.create_status_bar()
self.create_main_panel()
#Start recurring timer for updating plot
self.redraw_timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_redraw_timer, self.redraw_timer)
self.redraw_timer.Start(3)
def create_menu(self):
#Configure window menu bar
self.menubar = wx.MenuBar()
menu_file = wx.Menu()
m_expt = menu_file.Append(-1, "&Save plot image\tCtrl-S", "Save plot image")
self.Bind(wx.EVT_MENU, self.on_save_plot, m_expt)
m_startadc = menu_file.Append(-1, "&Start ADC BEN\tCtrl-G",\
"Trigger ADC to start taking data")
self.Bind(wx.EVT_MENU, self.on_startADC, m_startadc)
menu_file.AppendSeparator()
m_exit = menu_file.Append(-1, "&Exit\tCtrl-X", "Exit")
self.Bind(wx.EVT_MENU, self.on_exit, m_exit) #exit event handler
self.Bind(wx.EVT_CLOSE, self.on_close) #window close event handler
self.menubar.Append(menu_file, "&File")
self.SetMenuBar(self.menubar)
def create_main_panel(self):
#Configure main window panel THIS realtime signal frame
self.panel = wx.Panel(self)
self.init_plot()
self.canvas = FigCanvas(self.panel, -1, self.fig)
self.save_button = wx.Button(self.panel, -1, "Save plot data")
self.Bind(wx.EVT_BUTTON, self.on_save_button, self.save_button)
self.startADC_button= wx.Button(self.panel, -1, " Initialize ADC ")
self.Bind(wx.EVT_BUTTON, self.on_startADC, self.startADC_button)
self.hbox1 = wx.BoxSizer(wx.HORIZONTAL)
self.hbox1.Add(self.save_button, border=5, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL )
self.hbox1.Add(self.startADC_button, border=5, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL)
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.vbox.Add(self.canvas, 1, flag=wx.LEFT | wx.TOP | wx.GROW)
self.vbox.Add(self.hbox1, 0, flag=wx.ALIGN_RIGHT)
self.panel.SetSizer(self.vbox)
self.vbox.Fit(self)
def create_status_bar(self):
self.statusbar = self.CreateStatusBar()
def init_plot(self):
self.dpi = 100
self.fig = Figure((5.0, 4.0), dpi=self.dpi)
self.axes = self.fig.add_subplot(111)
self.axes.set_axis_bgcolor('black')
self.axes.set_title('MSP430 ADC Values', size=12)
self.axes.grid(True, color='gray')
self.axes.axis([0,buffersize,-1,3.6]) #Axes limits:([xmin,xmax,ymin,ymax])
pylab.setp(self.axes.get_xticklabels(), fontsize=8)
pylab.setp(self.axes.get_yticklabels(), fontsize=8)
# plot the data as a line series, and save the reference
# to the plotted line series
self.line0, = self.axes.plot(DataRead.xValues, DataRead.A0, label="PX_OUT1")
self.line1, = self.axes.plot(DataRead.xValues, DataRead.A1, label="ECG_OUT1")
self.line2, = self.axes.plot(DataRead.xValues, DataRead.A2, label="PX_OUT2")
self.line3, = self.axes.plot(DataRead.xValues, DataRead.A3, label="ECG_OUT2")
self.line4, = self.axes.plot(DataRead.xValues, DataRead.A4, label="PX_OUT3")
self.line12, = self.axes.plot(DataRead.xValues, DataRead.A12, label="ECG_OUT3")
self.line13, = self.axes.plot(DataRead.xValues, DataRead.A13, label="PX_OUT4")
self.line14, = self.axes.plot(DataRead.xValues, DataRead.A14, label="ECG_OUT4")
self.axes.legend(loc='upper center',
prop=matplotlib.font_manager.FontProperties(size='xx-small'),
ncol=8)
#
def draw_plot(self):
""" Redraws the plot
"""
#Read serial data and update the plot
self.dataread.read()
# self.PROC_cue = self.dataread.PROC
# print "PROC_CUE = ",self.PROC_cue
self.line0.set_ydata(DataRead.A0)
self.line1.set_ydata(DataRead.A1)
self.line2.set_ydata(DataRead.A2)
self.line3.set_ydata(DataRead.A3)
self.line4.set_ydata(DataRead.A4)
self.line12.set_ydata(DataRead.A12)
self.line13.set_ydata(DataRead.A13)
self.line14.set_ydata(DataRead.A14)
#Only readraw canvas only if the data set is complete
#Do this to minimize the effect of suspected memory leak in canvas.draw()
if self.dataread.drawIt == 1:
self.canvas.draw()
self.dataread.drawIt = 0
#if end of data set, re-enable "start ADC" button
if self.dataread.readingData == 0:
self.startADC_button.SetLabel("Start ADC")
self.startADC_button.Enable()
def on_startADC(self, event):
if self.dataread.readingData == 0:
print "start ADC was triggered"
self.dataread.startADC()
print "trigger was sent, waiting for data"
#Disable the button and menu item until data acquisition is finished
self.startADC_button.Disable()
self.startADC_button.SetLabel("Reading data...")
self.dataread.readingData = 1
else:
print "ADC operation already in progress"
def on_save_button(self, event):
print "save button was pressed"
#Write the data to a file, launch a file dialog box
file_choices = "CSV (*.csv)|*.csv"
dlg = wx.FileDialog(
self,
message="Save plot data as...",
defaultDir=os.getcwd(),
defaultFile="MSP430data.csv",
wildcard=file_choices,
style=wx.SAVE | wx.OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK: # it is passed requisit requirement to save the following format for col and rows.
path = dlg.GetPath()
self.dataread.save_data_arrays(path)
def on_save_plot(self, event):
file_choices = "PNG (*.png)|*.png"
dlg = wx.FileDialog(
self,
message="Save plot as...",
defaultDir=os.getcwd(),
defaultFile="plot.png",
wildcard=file_choices,
style=wx.SAVE | wx.OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
self.canvas.print_figure(path, dpi=self.dpi)
self.flash_status_message("Saved to %s" % path)
def on_redraw_timer(self, event):
self.draw_plot()
self.refreshCounter += 1
def on_exit(self, event):
self.redraw_timer.Stop()
self.dataread.closeSerial()
self.Destroy()
def on_close(self, event):
self.redraw_timer.Stop()
self.dataread.closeSerial()
self.Destroy()
def flash_status_message(self, msg, flash_len_ms=1500):
self.statusbar.SetStatusText(msg)
self.timeroff = wx.Timer(self)
self.Bind(
wx.EVT_TIMER,
self.on_flash_status_off,
self.timeroff)
self.timeroff.Start(flash_len_ms, oneShot=True)
def on_flash_status_off(self, event):
self.statusbar.SetStatusText('')
####---------- processed windows--------------------------------------------------
class GraphFrame_rev(wx.Frame):
""" The main frame of the RECONSTRUCTED signal
"""
title = 'Processed ECG Value Plotter'
refreshCounter = 0
strob = 0
def __init__(self):
wx.Frame.__init__(self, None, -1, self.title)
#Instantiate DataRead class to talk to serial port
#self.dataread = GraphFrame.save_data_arrays_auto.reconstruct
# self.READ = DataRead.read
# self.control = GraphFrame.PROC_cue
#Create wx window
self.create_sub_panel()
self.create_status_bar()
#Start recurring timer for updating plot
self.redraw_timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_redraw_timer, self.redraw_timer)
#if DataRead.PROC == 1:
self.redraw_timer.Start(1) # 1000 millisec for restarting the timer
# self.redraw_timer.Start((1.000/230)*buffer_for_DSP*20000) # 1000 millisec for restarting the timer
#DataRead.PROC = 0
# self.draw_plot_rev
def on_redraw_timer(self,event ):
# print "DataRead.PROC:", PROC
if PROC == 1:
# print "DataRead.PROC:", PROC
self.draw_plot_rev()
self.refreshCounter += 1
#print "counter now is :", self.refreshCounter
def create_status_bar(self):
self.statusbar = self.CreateStatusBar()
def draw_plot_rev(self):
# print "Current PROC is: " ,DataRead.PROC
#self.dataread.read()
#Update the plot
# import saved processed data MAY changed strob
# want to plot them
self.pros_sig_new = csv.reader(open("reconstruct.txt", "rU"),delimiter='\n')
self.pros_sig_list_new = []
self.pros_sig_list_new.extend(self.pros_sig_new)
self.signal_rev_new = []
for data in self.pros_sig_list_new:
self.signal_rev_new.append(data[0])
#print signal_rev_new
def convertStr(s):
try:
ret = int(s)
except ValueError:
ret = float(s)
return ret
self.processed_sig_new = range(len(self.signal_rev_new))
for i in range(len(self.signal_rev_new)):
self.processed_sig_new[i] = 1.0*(convertStr(self.signal_rev_new[i]))
#self.line.set_ydata(processed_sig_new)
#print "read plot_new successfully"
if self.strob == 9:# & control == 1:
#self.line.set_ydata(processed_sig_new)
self.fig_rev.clf()
self.axes = self.fig_rev.add_subplot(111)
self.axes.set_axis_bgcolor('black')
self.axes.set_title('Processed ADC Values', size=12)
self.axes.grid(True, color='gray')
# self.axes.axis([0,buffer_for_DSP*x_length_factor,-1,3.6]) #Axes limits:([xmin,xmax,ymin,ymax])
self.axes.axis([0, 1.0/230*buffer_for_DSP*x_length_factor,-0.5,1.2]) #Axes limits:([xmin,xmax,ymin,ymax])
pylab.setp(self.axes.get_xticklabels(), fontsize=8)
pylab.setp(self.axes.get_yticklabels(), fontsize=8)
# self.axes.plot(range(buffer_for_DSP), self.processed_sig_new, label="PROCESSED SIG")
x_unit = np.linspace(0,1.0/230*buffer_for_DSP,buffer_for_DSP)
lineADD = self.axes.plot(x_unit, self.processed_sig_new, label="PROCESSED SIG")
pylab.setp(lineADD, 'color', 'b', 'linewidth', 2.0)
self.fig_rev.canvas.draw()
global PROC
PROC = 0
self.strob = 0
print "plot_new0 successfully"
elif self.strob == 0:# & control == 1:
#self.fig_rev.clf()
# self.axes.plot(range(buffer_for_DSP,buffer_for_DSP*(x_length_factor-1)), self.processed_sig_new, label="PROCESSED SIG")
x_unit_2 = np.linspace(1.0/230*buffer_for_DSP,1.0/230*buffer_for_DSP*2,buffer_for_DSP)
lineADD = self.axes.plot(x_unit_2, self.processed_sig_new, label="PROCESSED SIG")
pylab.setp(lineADD, 'color', 'b', 'linewidth', 2.0)
self.fig_rev.canvas.draw()
global PROC
PROC = 0
self.strob = 1
print "plot_new1 successfully"
elif self.strob == 1:# & control == 1:
#self.fig_rev.clf()
# self.axes.plot(range(buffer_for_DSP*(x_length_factor-1),buffer_for_DSP*x_length_factor), self.processed_sig_new, label="PROCESSED SIG")
x_unit_3 = np.linspace(1.0/230*buffer_for_DSP*2,1.0/230*buffer_for_DSP*3,buffer_for_DSP)
lineADD = self.axes.plot(x_unit_3, self.processed_sig_new, label="PROCESSED SIG")
pylab.setp(lineADD, 'color', 'b', 'linewidth', 2.0)
self.fig_rev.canvas.draw()
global PROC
PROC = 0
self.strob = 2
print "plot_new3 successfully"
elif self.strob == 2:# & control == 1:
#self.fig_rev.clf()
x_unit_4 = np.linspace(1.0/230*buffer_for_DSP*3,1.0/230*buffer_for_DSP*4,buffer_for_DSP)
lineADD = self.axes.plot(x_unit_4, self.processed_sig_new, label="PROCESSED SIG")
pylab.setp(lineADD, 'color', 'b', 'linewidth', 2.0)
self.fig_rev.canvas.draw()
global PROC
PROC = 0
self.strob = 3
print "plot_new4 successfully"
elif self.strob == 3:# & control == 1:
#self.fig_rev.clf()
x_unit_5 = np.linspace(1.0/230*buffer_for_DSP*4,1.0/230*buffer_for_DSP*5,buffer_for_DSP)
lineADD = self.axes.plot(x_unit_5, self.processed_sig_new, label="PROCESSED SIG")
pylab.setp(lineADD, 'color', 'b', 'linewidth', 2.0)
self.fig_rev.canvas.draw()
global PROC
PROC = 0
self.strob = 4
print "plot_new5 successfully"
elif self.strob == 4:# & control == 1:
#self.fig_rev.clf()
x_unit_6 = np.linspace(1.0/230*buffer_for_DSP*5,1.0/230*buffer_for_DSP*6,buffer_for_DSP)
lineADD = self.axes.plot(x_unit_6, self.processed_sig_new, label="PROCESSED SIG")
pylab.setp(lineADD, 'color', 'b', 'linewidth', 2.0)
self.fig_rev.canvas.draw()
global PROC
PROC = 0
self.strob = 5
print "plot_new6 successfully"
elif self.strob == 5:# & control == 1:
#self.fig_rev.clf()
x_unit_7 = np.linspace(1.0/230*buffer_for_DSP*6,1.0/230*buffer_for_DSP*7,buffer_for_DSP)
lineADD = self.axes.plot(x_unit_7, self.processed_sig_new, label="PROCESSED SIG")
pylab.setp(lineADD, 'color', 'b', 'linewidth', 2.0)
self.fig_rev.canvas.draw()
global PROC
PROC = 0
self.strob = 6
print "plot_new7 successfully"
elif self.strob == 6:# & control == 1:
#self.fig_rev.clf()
x_unit_8 = np.linspace(1.0/230*buffer_for_DSP*7,1.0/230*buffer_for_DSP*8,buffer_for_DSP)
lineADD = self.axes.plot(x_unit_8, self.processed_sig_new, label="PROCESSED SIG")
pylab.setp(lineADD, 'color', 'b', 'linewidth', 2.0)
self.fig_rev.canvas.draw()
global PROC
PROC = 0
self.strob = 7
print "plot_new8 successfully"
elif self.strob == 7:# & control == 1:
#self.fig_rev.clf()
x_unit_9 = np.linspace(1.0/230*buffer_for_DSP*8,1.0/230*buffer_for_DSP*9,buffer_for_DSP)
lineADD = self.axes.plot(x_unit_9, self.processed_sig_new, label="PROCESSED SIG")
pylab.setp(lineADD, 'color', 'b', 'linewidth', 2.0)
self.fig_rev.canvas.draw()
global PROC
PROC = 0
self.strob = 8
print "plot_new9 successfully"
elif self.strob == 8:# & control == 1:
#self.fig_rev.clf()
x_unit_10 = np.linspace(1.0/230*buffer_for_DSP*9,1.0/230*buffer_for_DSP*10,buffer_for_DSP)
lineADD = self.axes.plot(x_unit_10, self.processed_sig_new, label="PROCESSED SIG")
pylab.setp(lineADD, 'color', 'b', 'linewidth', 2.0)
self.fig_rev.canvas.draw()
global PROC
PROC = 0
self.strob = 9
print "plot_new10 successfully"
else:
#print "not this way!!!!!!"
pass
##### need to use the completed data stream to enable refleshing data
#self.line
# if self.strob == 1:
# self.canvas.draw()
# self.strob = 0
def create_sub_panel(self):
#Configure main window panel RECONSTRUCTED
self.panel = wx.Panel(self)
self.init_plot_rev()
self.canvas = FigCanvas(self.panel, -1, self.fig_rev)
self.save_button = wx.Button(self.panel, -1, "Save plot data")
self.Bind(wx.EVT_BUTTON, self.on_save_button, self.save_button)
#self.startADC_button= wx.Button(self.panel, -1, " Initialize ADC ")
#self.Bind(wx.EVT_BUTTON, self.on_startADC, self.startADC_button)
self.hbox1 = wx.BoxSizer(wx.HORIZONTAL)
self.hbox1.Add(self.save_button, border=5, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL )
#self.hbox1.Add(self.startADC_button, border=5, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL)
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.vbox.Add(self.canvas, 1, flag=wx.LEFT | wx.TOP | wx.GROW)
self.vbox.Add(self.hbox1, 0, flag=wx.ALIGN_RIGHT)
self.panel.SetSizer(self.vbox)
self.vbox.Fit(self)
def on_save_button(self, event):
print "save button was pressed"
#Write the data to a file, launch a file dialog box
file_choices = "CSV (*.csv)|*.csv"
dlg = wx.FileDialog(
self,
message="Save plot data as...",
defaultDir=os.getcwd(),
defaultFile="Processed_ECG_data.csv",
wildcard=file_choices,
style=wx.SAVE | wx.OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK: # it is passed requisit requirement to save the following format for col and rows.
path = dlg.GetPath()
self.p_data_array(path)
def p_data_array(self, path):
#----- want to get current plotted signal
parsed_signal = self.processed_sig_new
np.savetxt(path, parsed_signal)
def init_plot_rev(self):
self.dpi = 100
self.fig_rev = Figure((5.0, 4.0), dpi=self.dpi)
self.axes = self.fig_rev.add_subplot(111)
self.axes.set_axis_bgcolor('black')
self.axes.set_title('Processed ADC Values', size=12)
self.axes.grid(True, color='gray')
# self.axes.axis([0,buffer_for_DSP*x_length_factor,-1,3.6]) #Axes limits:([xmin,xmax,ymin,ymax])
self.axes.axis([0, 1.0/230*buffer_for_DSP*x_length_factor,-0.5,1.2]) #Axes limits:([xmin,xmax,ymin,ymax])
pylab.setp(self.axes.get_xticklabels(), fontsize=8)
pylab.setp(self.axes.get_yticklabels(), fontsize=8)
# plot the data as a line series, and save the reference
# to the plotted line series
# import saved processed data
# want to plot them
pros_sig = csv.reader(open("reconstruct.txt", "rU"),delimiter='\n')
pros_sig_list = []
pros_sig_list.extend(pros_sig)
signal_rev = []
for data in pros_sig_list:
signal_rev.append(data[0])
#print signal_rev
def convertStr(s):
try:
ret = int(s)
except ValueError:
ret = float(s)
return ret
processed_sig = range(len(signal_rev))
for i in range(len(signal_rev)):
processed_sig[i] = convertStr(signal_rev[i])
if (len(processed_sig) < buffer_for_DSP) == True:
for j in range(0,buffer_for_DSP-len(processed_sig)):
processed_sig.append(0)
pro_mod = processed_sig
else:
pro_mod = processed_sig[0:buffer_for_DSP] # this is for the first time plot when buffer_for_DSP is changed
# pylab.ion()
# end loading the processed sig
x_unit = np.linspace(0,1.0/230*buffer_for_DSP,buffer_for_DSP)
self.line, = self.axes.plot(x_unit, pro_mod, label="PROCESSED SIG")
# for i in range(1,buffer_for_DSP):
# self.line.set_ydata(processed_sig[i])
# pylab.draw() # redraw the canvas
self.axes.legend(loc='upper center',
prop=matplotlib.font_manager.FontProperties(size='xx-small'),
ncol=1)
if __name__ == '__main__':
app = wx.PySimpleApp()
app.frame = GraphFrame()
app.frame.Show()
app.frame_rev = GraphFrame_rev()
app.frame_rev.Show()
app.MainLoop()