-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
executable file
·209 lines (168 loc) · 6.25 KB
/
graph.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
import matplotlib.pyplot as plt
import numpy as np
import csv
import math
import argparse
import io
REFERENCE_LEVEL_DEFAULT=0.775
def load_csv(measurement, filecontent, reference_level):
x = []
y = []
if (reference_level is None):
reference_level = REFERENCE_LEVEL_DEFAULT
csvcontent = io.StringIO(filecontent)
plots = csv.reader(filter(lambda row: row[0]!='#', csvcontent), delimiter=";")
for row in plots:
if measurement == "LVL_FRQ":
if (row[1] == 'None' or row[2] == 'None'):
continue
freq = float(row[1])
voltage = float(row[2])
level = 20 * math.log10(voltage/reference_level)
x.append(freq)
y.append(level)
if measurement == "THD_LVL" or measurement == "THDLV_LVL" or measurement == "SNR_LVL":
if (row[0] == 'None' or row[2] == 'None'):
continue
out_volt = float(row[0])
in_thd = float(row[2])
out_level = 20 * math.log10(out_volt/reference_level)
in_level = 20 * math.log10(in_thd/100)
x.append(out_volt)
y.append(in_level)
if measurement == "THD_FRQ" or measurement == "THDLV_FRQ":
if (row[0] == 'None' or row[2] == 'None'):
continue
freq = float(row[0])
in_thd = float(row[2])
in_level = 20 * math.log10(in_thd/100)
x.append(freq)
y.append(in_level)
return (x,y)
def load_calibration(filename):
if filename is None:
return None
else:
return load_csv(filename, 1)
def apply_calibration(calibration, measure):
if calibration is None:
return measure
x = []
y = []
for i in range(0, len(measure[0])):
xm = measure[0][i]
ym = measure[1][i]
calibration_deltas = np.abs(np.asarray(calibration[0]) - xm)
position_ym = calibration_deltas.argmin()
delta_y = 0
if position_ym != None:
delta_y = calibration[1][position_ym]
x.append(xm)
y.append(ym - delta_y)
return (x, y)
def define_db_scale(plt, axis, values) -> None:
min_db = (math.floor(min(values) / 6) * 6) - 6
max_db = (math.ceil(max(values) / 6) * 6) + 6
if axis == "y":
plt.yticks(np.arange(min_db, max_db, 6.0))
plt.ylim((min_db, max_db))
else:
plt.xticks(np.arange(min_db, max_db, 6.0))
plt.xlim((min_db, max_db))
def configure_plot_lvl_frq(plt, calibration, x, y):
plt.xscale("log")
plt.xlabel('Frequency [Hz]')
if calibration is not None:
plt.ylabel('Level [dB]')
else:
plt.ylabel('Level [dBu]')
define_db_scale(plt, "y", y)
def configure_plot_thd_lvl(plt, x, y, levelType):
plt.xlabel('Generator Level [dBu]')
plt.ylabel(f"{levelType} [dB]")
define_db_scale(plt, "x", x)
define_db_scale(plt, "y", y)
def configure_plot_thd_frq(plt, x, y, levelType):
plt.xscale("log")
plt.xlabel('Frequency [Hz]')
plt.ylabel(f"{levelType} [dB]")
define_db_scale(plt, "y", y)
def create_graph(measure, file_contents, output_format, output_buffer, calibration, graph_style, reference_level, title) -> None:
measurement = load_csv(measure, file_contents, reference_level)
(x, y) = apply_calibration(calibration, measurement)
plt.figure()
plt.style.use(graph_style)
if measure == "LVL_FRQ":
configure_plot_lvl_frq(plt, calibration, x, y)
elif measure == "THD_LVL":
configure_plot_thd_lvl(plt, x, y, "THD")
elif measure == "THD_FRQ":
configure_plot_thd_frq(plt, x, y, "THD")
elif measure == "THDLV_LVL":
configure_plot_thd_lvl(plt, x, y, "THD Level")
elif measure == "THDLV_FRQ":
configure_plot_thd_frq(plt, x, y, "THD Level")
elif measure == "SNR_LVL":
configure_plot_thd_lvl(plt, x, y, "SNR")
plt.grid()
plt.plot(x, y, label=measure)
plt.title(title)
plt.legend()
if (output_format is None):
plt.show()
else:
plt.savefig(output_buffer, format=output_format)
#plt.savefig(file + "." + output_format)
plt.close()
def create_graphs(measure, files, output_format, calibration_file, graph_style, reference_level) -> None:
calibration = load_calibration(calibration_file)
if calibration != None and measure != "LVL_FRQ":
raise "Calibration currently only possible for LVL_FRQ measurements!"
for filename in files:
with open(filename, 'r') as csvfile:
file_content = csvfile.read()
output_buffer = io.BytesIO()
create_graph(measure, file_content, output_format, output_buffer, calibration, graph_style, reference_level, filename)
output_buffer.seek(0)
with open(f"{filename}.{output_format}", "wb") as f:
f.write(output_buffer.getbuffer())
output_buffer.close()
def init_argparse() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
usage="%(prog)s [options] [files]...",
description="Plots measurements created with the HP8903 - currently only as Frequency Graphs"
)
parser.add_argument(
"-m", "--measure",
action='store',
default="LVL_FRQ",
choices=["LVL_FRQ", "THD_LVL", "THD_FRQ", "THDLV_LVL", "THDLV_FRQ", "SNR_LVL"],
help="What type measurement to graph"
)
parser.add_argument(
"-o", "--output-format",
action='store',
choices=["png", "svg", "pdf"],
help="Output to file of specified format"
)
parser.add_argument(
"-r", "--reference-level",
action='store',
type=float,
metavar='REFERENCE_VOLTAGE',
help="What reference level shall be used for dB calculations? (Default=0.775V = dBu, not used when you provide a calibration file)"
)
parser.add_argument(
"-c", "--calibration-file",
action='store',
metavar='CSVFILE',
help="Provide a calibration file from a Loop-Measurement (Straight cable from in to out)"
)
parser.add_argument('files', nargs='+')
return parser
def main() -> None:
parser = init_argparse()
args = parser.parse_args()
create_graphs(args.measure, args.files, args.output_format, args.calibration_file, 'style/theta.mplstyle', args.reference_level)
if __name__ == "__main__":
main()