-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw_signal.py
126 lines (116 loc) · 3.63 KB
/
draw_signal.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
import svgwrite
import random
def indefinito(ran, vscale):
dati = [(0, 0)]
for x in ran:
rnumb = random.random() * vscale
dati.append((x, rnumb))
return dati
def draw_vec(s, sig, rng, totl, vscale, hscale, name, labels):
outer = s.g()
g = s.g()
font_size= 10
if sig[0][0] != 0:
sig = [(0, 'undefined sig.')] + sig
count = 0
inc = 3
pathd = ""
oldtime = 0
txtg = s.g()
for point in sig:
#top part and descending line
pathd += "M " + str(oldtime + inc) + " 0"
pathd += "L " + str(max(point[0] * hscale - inc, 0)) + " 0"
if count > 0:
pathd += "L" + str(point[0] * hscale + inc) + " " + str(vscale)
#bottom part and rising line
pathd += "M " + str(oldtime + inc) + " " + str(vscale)
pathd += "L " + str(max(point[0] * hscale - inc, 0)) + " " + str(vscale)
if count > 0:
pathd += "L" + str(point[0] * hscale + inc) + " 0"
#get center:
try:
fine = sig[count+1][0]
except IndexError:
fine = totl
center = (fine - point[0]) * hscale / 2
#add text with signal value
if rng != []:
value_txt = "".join([point[1][x] for x in rng])
else:
value_txt = str(point[1])
txt = s.text(value_txt, (point[0] * hscale + center, vscale/2 + font_size*.4), font_family="Roboto", font_size= font_size, text_anchor = "middle")
txtg.add(txt)
oldtime = point[0] * hscale
count += 1
#conclude disegno
pathd += "M " + str(oldtime + inc) + " 0"
pathd += "L " + str(totl*hscale) + " 0"
pathd += "M " + str(oldtime + inc) + " " + str(vscale)
pathd += "L " + str(totl*hscale) + " " + str(vscale)
path = s.path(d = pathd, stroke="#000000", stroke_width = "1", fill="none")
decorate(s, name, totl, vscale, hscale, None, None, g, labels)
g.add(path)
g.add(txtg)
return g
def draw_bin_signal(s, sig, ind, totl, vscale, hscale, name, labels):
g = s.g()
font_size= 10
#normalize data points in 1:0 for drawing purposes
punti = [float(x[1][ind]) for x in sig]
sigmin = min(punti)
punti = [x - sigmin for x in punti]
sigmax = max(punti)
if sigmax == 0:
sigmax = 1 #hack
punti = [(1 - (x / sigmax)) * vscale for x in punti]
oldv = 0
if sig[0][0] != 0:
#segnale indefinito
try:
fine = sig[0][0]
except IndexError:
fine = totl
rumore = indefinito(range(0, fine, int(5/hscale)), vscale)
punti = [x[1] for x in rumore] + punti
sig = rumore + sig
oldv = rumore[-1][1]
count = 0
pathd = "M0 1"
for point in sig:
pathd += " L" + str(point[0] * hscale) + " " + str(oldv)
pathd += " L" + str(point[0] * hscale) + " " + str(punti[count])
oldv = punti[count]
count += 1
pathd += " L" + str(totl * hscale) + " " + str(punti[-1])
path = s.path(d = pathd, stroke="#000000", stroke_width = "1", fill="none")
decorate(s, name, totl, vscale, hscale, sigmin, sigmax, g, labels)
g.add(path)
return g
def decorate(s, name, totl, vscale, hscale, sigmin, sigmax, g, labels):
font_size = 10
font = "Roboto"
rect = s.rect((0, 0), (totl*hscale, vscale),
stroke="#777777",
stroke_width=1,
fill = "#ffffff",
stroke_dasharray="1,2")
if sigmin is not None:
min_label = s.text(str(sigmin),(- 5, vscale),
text_anchor = "end",
font_size = font_size,
font_family= font)
g.add(min_label)
if sigmax is not None:
max_label = s.text(str(sigmax),(- 5, font_size*.8),
text_anchor = "end",
font_size = font_size,
font_family=font)
g.add(max_label)
name_label = s.text(name, (-5, vscale/2 + font_size*.4),
text_anchor ="end",
font_size = font_size*1.2,
font_family=font)
g.add(rect)
g.translate(labels, 0)
g.add(name_label)