-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer.py
633 lines (520 loc) · 19.3 KB
/
analyzer.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
import pygame, sys
import getopt
from pygame.locals import *
import serial
import numpy as np
from scipy.signal import argrelextrema
import time
import socket
import subprocess
#keymap
#http://www.pygame.org/docs/ref/key.html
ser = serial.Serial(
port='/dev/ttyACM0',
baudrate=38400,#9600,
parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.SEVENBITS
)
MODE_FFT = 0
MODE_SWEEP = 2
MODE_THD = 4
MODE_HARMONICS = 5
MODE_RMS = 3
class Gnuplotter(object):
script = """set logscale x 10
set logscale y 10
set grid mxtics mytics xtics ytics lt 1 lc rgb 'gray70', lt 1 lc rgb 'gray90'
set mxtics 10
set mytics 5
set boxwidth 8
set title "{title}"
set xlabel "freq[Hz]"
set ylabel "amp[rel.]"
set xrange [10:50000]
set yrange [0.001: 1]
plot {plot}
set size 1.0, 1.0
#set terminal postscript portrait enhanced mono dashed lw 1 "Helvetica" 14
#set terminal pngcairo transparent enhanced font "arial,10" fontscale 1.0 size 500,500; set zeroaxis;
#set terminal pngcairo enhanced font "arial,20" fontscale 1.0 size 1500,1500;
#set output "my-plot.png"
#set terminal pdf
#set output "my-plot.pdf"
set term svg enhanced font "arial,20" mouse size 1500,1200
set output "./tmp/my-plot.svg"
replot
set terminal x11
set size 1,1
#pause -1
"""
def __init__(self, plotfiles):
myfile = open("./tmp/gnuplot.scr","w")
string = ''
for name in plotfiles:
string += '"{name}" using 1:2 smooth csplines title "{title}" with lines,'.format(name=name, title=name)
script = self.script.format(
title = 'frequency sweep',
plot = string
)
myfile.write(script)
myfile.close()
def plot(self):
subprocess.Popen("gnuplot ./tmp/gnuplot.scr", shell=True, stdout=subprocess.PIPE).stdout.read()
class Const(object):
#W = 1280#640
#H = 800#480
#W = 1366
#H = 768
W = 640
H = 480
FULLSCREEN = True
margin = 10
rW = W-margin
rH = H-margin
r2W = rW-margin
r2H = rH-margin
halfW = W/2
halfH = H/2
#ip = socket.gethostbyname(socket.gethostname())
#scaling
ip = "NIY"
xs = r2W/512.0
linewidth = 2
@classmethod
def rescale(cls):
pass
@classmethod
def raspi(cls):
cls.W = 1280
cls.H = 800
@classmethod
def desktop(cls):
cls.W = 1280
cls.H = 1024
@classmethod
def laptop(cls):
cls.W = 1366
cls.H = 768
@classmethod
def window(cls):
cls.W = 640
cls.H = 480
cls.FULLSCREEN = False
class Colors(object):
red = pygame.Color(255, 0, 0)
darkred = pygame.Color(150,0,0)
darkerred = pygame.Color(80,0,0)
blue = pygame.Color(0, 0, 255)
green = pygame.Color(0, 255, 0)
white = pygame.Color(255, 255, 255)
black = pygame.Color(0,0,0)
darkgrey = pygame.Color(100,100,100)
background = black
class Handleargs(object):
#http://www.tutorialspoint.com/python/python_command_line_arguments.htm
def __init__(self, argv):
self.argv = argv
self.device = ""
def process(self):
try:
opts, args = getopt.getopt(self.argv,"hd:",["ifile=","ofile="])
except getopt.GetoptError:
print 'analyzer.py -d <device> '
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
f=open('help.txt', 'r')
for line in f.readlines():
print line,
f.close()
sys.exit()
if opt == '-d':
self.device = arg
if arg == 'raspi':
Const.raspi()
elif arg == 'desktop':
Const.desktop()
elif arg == 'laptop':
Const.laptop()
elif arg == 'window':
Const.window()
else:
print 'unknown option: "{0}"'.format(arg)
sys.exit(1)
class Axes(object):
def __init__(self, surface):
self.surface = surface
def settext(self, text, x, y, size,center):
fontinstance = pygame.font.Font('freesansbold.ttf', size)
renderinstance = fontinstance.render(text, False, Colors.blue)
rectinstance = renderinstance.get_rect()
if (center):
rectinstance.centerx = x
rectinstance.centery = y
else:
rectinstance.topleft=(x, y)
self.surface.blit(renderinstance, rectinstance)
def clear(self):
self.surface.fill(Colors.background)
def frame(self):
pygame.draw.line(self.surface, Colors.darkred, (Const.margin,Const.margin), (Const.rW,Const.margin), 2)
pygame.draw.line(self.surface, Colors.darkred, (Const.rW,Const.margin), (Const.rW, Const.rH), 2)
pygame.draw.line(self.surface, Colors.darkred, (Const.rW, Const.rH), (Const.margin, Const.rH), 2)
pygame.draw.line(self.surface, Colors.darkred, (Const.margin,Const.rH), (Const.margin, Const.margin), 2)
def draw(self):
self.frame()
for i in range(10):
pygame.draw.line(self.surface, Colors.darkred, (i*Const.W/10, Const.margin),(i*Const.W/10,Const.rH), 1)
def drawlog(self):
self.frame()
self.settext('ip {0}'.format(Const.ip),1100,10,18, False)
logrange = [1, 10.0,100.0,1000.0,10000.0]
for i in [0,1,2,3]:
currX = int(np.log10(logrange[i])*Const.r2W/4.0)+Const.margin
self.settext(" {0}Hz ".format(logrange[i+1]), currX, Const.rH, 10, False)
for j in np.arange(logrange[i], logrange[i+1], logrange[i], dtype=np.float):
pygame.draw.line( self.surface,
Colors.darkred,
(int(1.0*np.log10(j)*Const.r2W/4.0)+Const.margin, Const.margin),
(int(1.0*np.log10(j)*Const.r2W/4.0)+Const.margin, Const.rH),
1)
pygame.draw.line( self.surface,
Colors.darkred,
(currX, Const.margin),
(currX,Const.rH),
1)
class Stats(object):
def __init__(self, wav):
self.wav = wav
self.max = None
self.min = None
self.rms = None
self.std = None
self.dostats()
def dostats(self):
self.max = np.nanmax(self.wav)
self.min = np.nanmin(self.wav)
self.std = np.nanstd(self.wav)
@classmethod
def bookkeepproduct(self, vector1, vector2):
n1 = len(vector1)
n2 = len(vector2)
if n1==n2:
return np.multiply(vector1, vector2)
else:
return -1
class Plots(object):
def __init__(self):
pass
class Main(object):
def __init__(self):
pygame.init()
self.fps = pygame.time.Clock()
if (Const.FULLSCREEN):
self.Surface = pygame.display.set_mode((Const.W, Const.H), pygame.FULLSCREEN)
else:
self.Surface = pygame.display.set_mode((Const.W, Const.H))
pygame.display.set_caption("Teensy Analyser")
self.red = pygame.Color(255, 0, 0)
self.blue = pygame.Color(0, 0, 255)
self.green = pygame.Color(0, 255, 0)
self.axes = Axes(self.Surface)
self.fourier = np.zeros(512, dtype=np.float)
self.waveformx = np.zeros(1024, dtype=np.int)
self.waveformy = np.zeros(1024, dtype=np.float)
self.calibrate = np.ones(1024, dtype=np.float)
self.modeofoperation = MODE_FFT
self.stats = None
self.loadcalibration()
def loadcalibration(self):
file = open("./cal.dat", 'r')
lines = file.readlines()
for i, line in enumerate(lines):
l=line.split(' ')
self.calibrate[i] = float(l[1])
def scaleme(self):
yscale = 0.8 * Const.r2H/np.max(self.fourier)
return yscale
def plotsweep(self, points, scale):
#points should be less than 2000
yscale= Const.r2H/1.0;
xscale= Const.r2W/4;
xoffset=xscale
self.axes.clear()
self.axes.drawlog()
self.axes.settext("Sweep mode", 10, 10, 32, False)
if scale:
fullscale = 1.0/(self.stats.max-self.stats.min)
scaleoffset = -self.stats.min
min = self.stats.min
offset = 0.0
else:
fullscale = 0.1
min = 0.0
scaleoffset = 0.0
offset = -Const.halfH
for i in range(points):
if i>2:
if np.log10(self.waveformx[i])>0:
x1 = np.log10(self.waveformx[i])
else:
x1 = 0
if np.log10(self.waveformx[i+1])>0:
x2 = np.log10(self.waveformx[i+1])
else:
x2 = 0
pygame.draw.line( self.Surface,
Colors.red,
( int(10+xscale*x1)-xoffset,
int(1.0*Const.rH-(yscale*fullscale*(self.calibrate[i]*self.waveformy[i]-scaleoffset))+offset)),
( int(10+xscale*x2)-xoffset,
int(1.0*Const.rH-(yscale*fullscale*(self.calibrate[i+1]*self.waveformy[i+1]-scaleoffset))+offset)),
Const.linewidth)
pygame.display.update()
def freqbin(self, mybin):
return 43*mybin
def plotharmonics(self):
#http://stackoverflow.com/questions/4624970/finding-local-maxima-minima-with-numpy-in-a-1d-numpy-array
extrema = argrelextrema(self.fourier, np.greater)
ea = extrema[0]
peaks = self.fourier[ea]
harm = 0.0
fund = 0.0
peaks.sort()
self.axes.clear()
self.axes.frame()
self.axes.settext("harmonix 1 {0}, {1}".format(-1, peaks[1]), Const.halfW, Const.halfH, 32, True)
self.axes.settext("harmonix 2 {0}, {1}".format(-1, peaks[2]), Const.halfW, Const.halfH+40, 32, True)
self.axes.settext("harmonix 3 {0}, {1}".format(-1, peaks[3]), Const.halfW, Const.halfH+80, 32, True)
pygame.display.update()
return -1
def plotfft(self, bins, withpeaks):
yscale = self.scaleme()
offset = -10
xscale = 1.0 * Const.r2W/bins
for i in range(bins-1):
try:
pygame.draw.line( self.Surface,
Colors.red,
(int(10+xscale*i), Const.rH-yscale * self.fourier[i] + offset),
(int(10+xscale*(i+1)), Const.rH-yscale * self.fourier[i+1]+ offset))
except:
pass
if withpeaks:
#http://stackoverflow.com/questions/4624970/finding-local-maxima-minima-with-numpy-in-a-1d-numpy-array
extrema = argrelextrema(self.fourier, np.greater)
ea=extrema[0]
peaks = self.fourier[ea]
harm = 0.0
fund = 0.0
for i in range(len(peaks)):
pygame.draw.circle( self.Surface,
Colors.blue,
( int(10+Const.xs*ea[i])+1,
int(Const.rH-yscale*peaks[i] + offset)
),
2,
0)
if i==0:
fund = peaks[i]
else:
harm = harm + peaks[i]**2
try:
thd = np.sqrt(harm)/(fund)*100
except:
thd = -1
self.axes.settext("THD: {0}%".format(thd), Const.halfW, 20, 16, False)
try:
self.axes.settext("fund: {0}kHz".format(ea[0]*44.032), 10, 40, 16, False)
except:
pass
for i in range(10):
self.axes.settext(" {0}k ".format(i/10.0*20), i*64, Const.rH, 10, False)
return thd
def rms(self):
i=0
self.axes.clear()#Surface.fill(Colors.background)
self.axes.frame()
ser.write('?')
msg =[]
while ser.inWaiting() > 0:
dummy = ser.read(1)
msg.append(dummy)
i=1
if (i>0):
self.axes.clear()#Saurface.fill(Colors.background)
self.axes.frame()
msgstr = ''.join(msg)
vals = msgstr.split(':')
for i, val in enumerate(vals):
try:
if (i==0):
freq = float(val)
if (i==1):
rms = float(val)
except:
rms = -1
freq=-1
self.axes.settext("RMS @ {0:04d}Hz = {1:.3f}V".format(int(freq), rms), Const.halfW ,Const.halfH, 32, True)
pygame.display.update()
def fft(self, plottoo):
i=0
ser.write('?');#get the fourier data
msg=[]
while ser.inWaiting() > 0:
dummy = ser.read(1)
msg.append(dummy)
i=1
if (i>0):
self.axes.clear()
self.axes.draw()
msgstr = ''.join(msg)
vals = msgstr.split(':')
#self.Bassbar.set(float(vals[0]))
preval =0
for i, val in enumerate(vals):
#the first bytes are header with parameter info
if (i==0):
#freq
try:
freq = float(val)
except:
freq=-1
#print "frequency: {0}Hz\n".format(freq)
self.axes.settext("freq: {0}Hz".format(freq), 10, 10, 32, False)
elif (i==1):
try:
vol = float(val)
except:
vol = -1
self.axes.settext("level: {0}".format(vol), 300,10,32, False)
#the last 512 bytes are FFT data
elif (i<512):
#print val
try:
self.fourier[i] = float(val)
except:
self.fourier[i] = 0.0
if plottoo:
thd = self.plotfft(512, False)
else:
thd = self.plotharmonics()
pygame.display.update()
def sweep(self):
N=128
s=''
for i, fr in enumerate(np.logspace(1.3, 4.3, N)):
ser.write("?{0}".format(int(fr)))
msg=[]
#print "{0} % done".format(1.0*i/N)
time.sleep(.05)
while ser.inWaiting()>0:
dummy = ser.read(1)
msg.append(dummy)
msgstr = ''.join(msg)
vals = msgstr.split(':')
try:
f = int(vals[0])
a = float(vals[1])
self.waveformx[i] = f
self.waveformy[i] += a
except:
pass
if (i & 10 == 0):
self.plotsweep(i, False)
s = self.eventhandler()
if s == 'f':
break
if s == 'r':
break
self.stats = Stats(Stats.bookkeepproduct(self.calibrate,self.waveformy))
self.plotsweep(i, True)
if s=='':
file = open('./tmp/sweep.dat', 'w')
for i, j in enumerate(self.waveformx):
if (i>0):
file.write("{0} {1}\r\n".format(j, self.calibrate[i]*self.waveformy[i]))
file.close()
self.axes.settext("select: s=sweep, c=save calibration, f=back to fft mode", Const.halfW, Const.r2H, 16, True)
pygame.display.update()
plotje = Gnuplotter(['./tmp/sweep.dat'])
plotje.plot()
while s=='':
s = self.eventhandler()
if (s=='c'):
file = open('cal.dat', 'w')
for i, j in enumerate(self.waveformx):
if self.waveformy[i]!=0.0:
self.calibrate[i] = 1.0/self.waveformy[i]
else:
self.calibrate[i]=1.0
file.write("{0} {1}\r\n".format(j, self.calibrate[i]))
self.axes.settext("wrote new calibartion file", Const.halfW, int(0.9*Const.r2H), 16, True)
pygame.display.update()
file.close()
def eventhandler(self):
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
if event.key == K_d:
ser.write('d')
if event.key == K_i:
ser.write('D')
if event.key == K_s:
#got to sweep mode
ser.write('s')
self.modeofoperation = MODE_SWEEP
return 's'
if event.key == K_c:
return 'c'
if event.key == K_r:
ser.write('r')
self.modeofoperation = MODE_RMS
return 'r'
if event.key == K_t:
ser.write('f')
self.modeofoperation = MODE_HARMONICS
return 't'
if event.key == K_EQUALS:
ser.write('+')
return '+'
if event.key == K_MINUS:
ser.write('-')
return '-'
if event.key == K_f:
#go to fft mode
ser.write('f')
self.modeofoperation = MODE_FFT
return 'f'
return ''
def loop(self):
self.axes.clear()
self.axes.draw()
self.axes.settext("Initializing...", 10,10,32, False)
pygame.display.update()
ser.write('f')#make sure we are in default FFT modus
print "Initializing..."
time.sleep(5)
while True:
if (self.modeofoperation == MODE_RMS):
self.rms()
if (self.modeofoperation == MODE_THD):
pass
if (self.modeofoperation == MODE_FFT):
self.fft(True)
if (self.modeofoperation == MODE_HARMONICS):
self.fft(False)
if (self.modeofoperation == MODE_SWEEP):
self.sweep()
self.eventhandler()
self.fps.tick(5)
if __name__ == '__main__':
arghandler = Handleargs(sys.argv[1:])
arghandler.process()
instance = Main()
instance.loop()