summaryrefslogtreecommitdiff
path: root/bin/mim-vs-keysight.py
blob: c214f2f0d1397a2fd6334c55c8c214c3c3d55788 (plain)
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
#!/usr/bin/env python3

import numpy as np
import sys
import matplotlib.pyplot as plt
from dfatool.dfatool import MIMOSA, KeysightCSV
from dfatool.utils import running_mean

voltage = float(sys.argv[1])
shunt = float(sys.argv[2])

mimfile = "../data/20161114_arb_%d.mim" % shunt
csvfile = "../data/20161114_%d_arb.csv" % shunt

mim = MIMOSA(voltage, shunt)
ks = KeysightCSV()

charges, triggers = mim.load_data(mimfile)
timestamps, currents = ks.load_data(csvfile)


def calfunc330(charge):
    if charge < 140.210488888889:
        return 0
    if charge <= 526.507377777778:
        return float(charge) * 0.0941215500652876 + -13.196828549634
    else:
        return float(charge) * 0.0897304193584184 + -47.2437278033012 + 36.358862


def calfunc82(charge):
    if charge < 126.993600:
        return 0
    if charge <= 245.464889:
        return charge * 0.306900 + -38.974361
    else:
        return charge * 0.356383 + -87.479495 + 36.358862


def calfunc33(charge):
    if charge < 127.000000:
        return 0
    if charge <= 127.211911:
        return charge * 171.576006 + -21790.152700
    else:
        return charge * 0.884357 + -112.500777 + 36.358862


calfuncs = {
    33: calfunc33,
    82: calfunc82,
    330: calfunc330,
}

vcalfunc = np.vectorize(calfuncs[int(shunt)], otypes=[np.float64])

# plt.plot(np.arange(0, 1000, 0.01), vcalfunc(np.arange(0, 1000, 0.01)))
# plt.xlabel('Rohdatenwert')
# plt.ylabel('Strom [µA]')
# plt.show()
# sys.exit(0)

mim_x = np.arange(len(charges) - 199) * 1e-5
mim_y = running_mean(mim.charge_to_current_nocal(charges), 200) * 1e-6
cal_y = running_mean(vcalfunc(charges), 200) * 1e-6
ks_x = timestamps[: len(timestamps) - 9]
ks_y = running_mean(currents, 10)

# look for synchronization opportunity in first 5 seconds
mim_sync_idx = 0
ks_sync_idx = 0
for i in range(0, 500000):
    if mim_sync_idx == 0 and mim_y[i] > 0.001:
        mim_sync_idx = i
    if ks_sync_idx == 0 and ks_y[i] > 0.001:
        ks_sync_idx = i

mim_x = mim_x - mim_x[mim_sync_idx]
ks_x = ks_x - ks_x[ks_sync_idx]

mim_max_start = int(len(mim_y) * 0.4)
mim_max_end = int(len(mim_y) * 0.6)
mim_start_end = int(len(mim_y) * 0.1)
mim_end_start = int(len(mim_y) * 0.9)
mim_max = np.max(mim_y[mim_max_start:mim_max_end])
mim_min1 = np.min(mim_y[:mim_start_end])
mim_min2 = np.min(mim_y[mim_end_start:])
mim_center = 0
mim_start = 0
mim_end = 0
for i, y in enumerate(mim_y):
    if y == mim_max and i / len(mim_y) > 0.4 and i / len(mim_y) < 0.6:
        mim_center = i
    elif y == mim_min1 and i / len(mim_y) < 0.1:
        mim_start = i
    elif y == mim_min2 and i / len(mim_y) > 0.9:
        mim_end = i

plt.plot([mim_x[mim_center]], [mim_y[mim_center]], "yo")
plt.plot([mim_x[mim_start]], [mim_y[mim_start]], "yo")
plt.plot([mim_x[mim_end]], [mim_y[mim_end]], "yo")
#
(mimhandle,) = plt.plot(mim_x, mim_y, "r-", label="MIMOSA")
# calhandle, = plt.plot(mim_x, cal_y, "g-", label='MIMOSA (autocal)')
(kshandle,) = plt.plot(ks_x, ks_y, "b-", label="Keysight")
# plt.legend(handles=[mimhandle, calhandle, kshandle])
plt.xlabel("Zeit [s]")
plt.ylabel("Strom [A]")
plt.grid(True)

ks_steps_up = []
ks_steps_down = []
mim_steps_up = []
mim_steps_down = []

skip = 0
for i, gradient in enumerate(np.gradient(ks_y, 10000)):
    if (
        gradient > 0.5e-9
        and i - skip > 200
        and ks_x[i] < mim_x[mim_center]
        and ks_x[i] > 5
    ):
        plt.plot([ks_x[i]], [ks_y[i]], "go")
        ks_steps_up.append(i)
        skip = i
    elif (
        gradient < -0.5e-9
        and i - skip > 200
        and ks_x[i] > mim_x[mim_center]
        and ks_x[i] < mim_x[mim_end]
    ):
        plt.plot([ks_x[i]], [ks_y[i]], "g*")
        ks_steps_down.append(i)
        skip = i

j = 0
for i, ts in enumerate(mim_x):
    if j < len(ks_steps_up) and ts > ks_x[ks_steps_up[j]]:
        mim_steps_up.append(i)
        j += 1

j = 0
for i, ts in enumerate(mim_x):
    if j < len(ks_steps_down) and ts > ks_x[ks_steps_down[j]]:
        mim_steps_down.append(i)
        j += 1

print(ks_steps_up)
print(mim_steps_up)

mim_values = []
cal_values = []
ks_values = []

for i in range(1, len(ks_steps_up)):
    mim_values.append(np.mean(mim_y[mim_steps_up[i - 1] : mim_steps_up[i]]))
    cal_values.append(np.mean(cal_y[mim_steps_up[i - 1] : mim_steps_up[i]]))
    ks_values.append(np.mean(ks_y[ks_steps_up[i - 1] : ks_steps_up[i]]))
    print(
        "step %d avg %5.3f vs %5.3f vs %5.3f mA"
        % (
            i,
            np.mean(ks_y[ks_steps_up[i - 1] : ks_steps_up[i]]) * 1e3,
            np.mean(mim_y[mim_steps_up[i - 1] : mim_steps_up[i]]) * 1e3,
            np.mean(cal_y[mim_steps_up[i - 1] : mim_steps_up[i]]) * 1e3,
        )
    )
for i in range(1, len(ks_steps_down)):
    mim_values.append(np.mean(mim_y[mim_steps_down[i - 1] : mim_steps_down[i]]))
    cal_values.append(np.mean(cal_y[mim_steps_down[i - 1] : mim_steps_down[i]]))
    ks_values.append(np.mean(ks_y[ks_steps_down[i - 1] : ks_steps_down[i]]))
    print(
        "step %d avg %5.3f vs %5.3f vs %5.3f mA"
        % (
            i,
            np.mean(ks_y[ks_steps_down[i - 1] : ks_steps_down[i]]) * 1e3,
            np.mean(mim_y[mim_steps_down[i - 1] : mim_steps_down[i]]) * 1e3,
            np.mean(cal_y[mim_steps_down[i - 1] : mim_steps_down[i]]) * 1e3,
        )
    )

mim_values = np.array(mim_values)
cal_values = np.array(cal_values)
ks_values = np.array(ks_values)

plt.show()

plt.hist(
    ks_y[ks_steps_up[48] : ks_steps_up[49]] * 1e3,
    100,
    normed=0,
    facecolor="blue",
    alpha=0.8,
)
plt.xlabel("mA Keysight")
plt.ylabel("#")
plt.grid(True)
plt.show()
plt.hist(
    mim_y[mim_steps_up[48] : mim_steps_up[49]] * 1e3,
    100,
    normed=0,
    facecolor="blue",
    alpha=0.8,
)
plt.xlabel("mA MimosaGUI")
plt.ylabel("#")
plt.grid(True)
plt.show()

(mimhandle,) = plt.plot(
    ks_values * 1e3, mim_values * 1e3, "ro", label="Unkalibriert", markersize=4
)
(calhandle,) = plt.plot(
    ks_values * 1e3, cal_values * 1e3, "bs", label="Kalibriert", markersize=4
)
plt.legend(handles=[mimhandle, calhandle])
plt.xlabel("mA Keysight")
plt.ylabel("mA MIMOSA")
plt.grid(True)
plt.show()

(mimhandle,) = plt.plot(
    ks_values * 1e3,
    (mim_values - ks_values) * 1e3,
    "ro",
    label="Unkalibriert",
    markersize=4,
)
(calhandle,) = plt.plot(
    ks_values * 1e3,
    (cal_values - ks_values) * 1e3,
    "bs",
    label="Kalibriert",
    markersize=4,
)
plt.legend(handles=[mimhandle, calhandle])
plt.xlabel("Sollstrom [mA]")
plt.ylabel("Messfehler MIMOSA [mA]")
plt.grid(True)
plt.show()

(mimhandle,) = plt.plot(
    ks_values * 1e3, (mim_values - ks_values) * 1e3, "r--", label="Unkalibriert"
)
(calhandle,) = plt.plot(
    ks_values * 1e3, (cal_values - ks_values) * 1e3, "b-", label="Kalibriert"
)
plt.legend(handles=[mimhandle, calhandle])
plt.xlabel("Sollstrom [mA]")
plt.ylabel("Messfehler MIMOSA [mA]")
plt.grid(True)
plt.show()

(mimhandle,) = plt.plot(
    ks_values * 1e3,
    (mim_values - ks_values) / ks_values * 100,
    "ro",
    label="Unkalibriert",
    markersize=4,
)
(calhandle,) = plt.plot(
    ks_values * 1e3,
    (cal_values - ks_values) / ks_values * 100,
    "bs",
    label="Kalibriert",
    markersize=4,
)
plt.legend(handles=[mimhandle, calhandle])
plt.xlabel("Sollstrom [mA]")
plt.ylabel("Messfehler MIMOSA [%]")
plt.grid(True)
plt.show()

(mimhandle,) = plt.plot(
    ks_values * 1e3,
    (mim_values - ks_values) / ks_values * 100,
    "r--",
    label="Unkalibriert",
)
(calhandle,) = plt.plot(
    ks_values * 1e3,
    (cal_values - ks_values) / ks_values * 100,
    "b-",
    label="Kalibriert",
)
plt.legend(handles=[mimhandle, calhandle])
plt.xlabel("Sollstrom [mA]")
plt.ylabel("Messfehler MIMOSA [%]")
plt.grid(True)
plt.show()

# mimhandle, = plt.plot(mim_x, np.gradient(mim_y, 10000), "r-", label='MIMOSA')
# kshandle, = plt.plot(ks_x, np.gradient(ks_y, 10000), "b-", label='Keysight')
# plt.legend(handles=[mimhandle, kshandle])
# plt.xlabel('Zeit [s]')
# plt.ylabel('Strom [A]')
# plt.show()