forked from fossasia/pslab-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyticsClass.py
362 lines (307 loc) · 12.1 KB
/
analyticsClass.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
import math
from typing import Tuple
import numpy as np
class analyticsClass():
"""
This class contains methods that allow mathematical analysis such as curve fitting
"""
def __init__(self):
try:
import scipy.optimize as optimize
except ImportError:
self.optimize = None
else:
self.optimize = optimize
try:
import scipy.fftpack as fftpack
except ImportError:
self.fftpack = None
else:
self.fftpack = fftpack
try:
from scipy.optimize import leastsq
except ImportError:
self.leastsq = None
else:
self.leastsq = leastsq
try:
import scipy.signal as signal
except ImportError:
self.signal = None
else:
self.signal = signal
def sineFunc(self, x, a1, a2, a3, a4):
return a4 + a1 * np.sin(abs(a2 * (2 * np.pi)) * x + a3)
def squareFunc(self, x, amp, freq, phase, dc, offset):
return offset + amp * self.signal.square(2 * np.pi * freq * (x - phase), duty=dc)
# -------------------------- Exponential Fit ----------------------------------------
def func(self, x, a, b, c):
return a * np.exp(-x / b) + c
def fit_exp(self, t, v): # accepts numpy arrays
size = len(t)
v80 = v[0] * 0.8
for k in range(size - 1):
if v[k] < v80:
rc = t[k] / .223
break
pg = [v[0], rc, 0]
po, err = self.optimize.curve_fit(self.func, t, v, pg)
if abs(err[0][0]) > 0.1:
return None, None
vf = po[0] * np.exp(-t / po[1]) + po[2]
return po, vf
def squareFit(self, xReal, yReal):
N = len(xReal)
mx = yReal.max()
mn = yReal.min()
OFFSET = (mx + mn) / 2.
amplitude = (np.average(yReal[yReal > OFFSET]) - np.average(yReal[yReal < OFFSET])) / 2.0
yTmp = np.select([yReal < OFFSET, yReal > OFFSET], [0, 2])
bools = abs(np.diff(yTmp)) > 1
edges = xReal[bools]
levels = yTmp[bools]
frequency = 1. / (edges[2] - edges[0])
phase = edges[0] # .5*np.pi*((yReal[0]-offset)/amplitude)
dc = 0.5
if len(edges) >= 4:
if levels[0] == 0:
dc = (edges[1] - edges[0]) / (edges[2] - edges[0])
else:
dc = (edges[2] - edges[1]) / (edges[3] - edges[1])
phase = edges[1]
guess = [amplitude, frequency, phase, dc, 0]
try:
(amplitude, frequency, phase, dc, offset), pcov = self.optimize.curve_fit(self.squareFunc, xReal,
yReal - OFFSET, guess)
offset += OFFSET
if (frequency < 0):
# print ('negative frq')
return False
freq = 1e6 * abs(frequency)
amp = abs(amplitude)
pcov[0] *= 1e6
# print (pcov)
if (abs(pcov[-1][0]) > 1e-6):
False
return [amp, freq, phase, dc, offset]
except:
return False
def sineFit(self, xReal, yReal, **kwargs):
N = len(xReal)
OFFSET = (yReal.max() + yReal.min()) / 2.
yhat = self.fftpack.rfft(yReal - OFFSET)
idx = (yhat ** 2).argmax()
freqs = self.fftpack.rfftfreq(N, d=(xReal[1] - xReal[0]) / (2 * np.pi))
frequency = kwargs.get('freq', freqs[idx])
frequency /= (2 * np.pi) # Convert angular velocity to freq
amplitude = kwargs.get('amp', (yReal.max() - yReal.min()) / 2.0)
phase = kwargs.get('phase', 0) # .5*np.pi*((yReal[0]-offset)/amplitude)
guess = [amplitude, frequency, phase, 0]
try:
(amplitude, frequency, phase, offset), pcov = self.optimize.curve_fit(self.sineFunc, xReal, yReal - OFFSET,
guess)
offset += OFFSET
ph = ((phase) * 180 / (np.pi))
if (frequency < 0):
# print ('negative frq')
return False
if (amplitude < 0):
ph -= 180
if (ph < 0):
ph = (ph + 720) % 360
freq = 1e6 * abs(frequency)
amp = abs(amplitude)
pcov[0] *= 1e6
# print (pcov)
if (abs(pcov[-1][0]) > 1e-6):
return False
return [amp, freq, offset, ph]
except:
return False
def find_frequency(self, v, si): # voltages, samplimg interval is seconds
from numpy import fft
NP = len(v)
v = v - v.mean() # remove DC component
frq = fft.fftfreq(NP, si)[:NP / 2] # take only the +ive half of the frequncy array
amp = abs(fft.fft(v)[:NP / 2]) / NP # and the fft result
index = amp.argmax() # search for the tallest peak, the fundamental
return frq[index]
def sineFit2(self, x, y, t, v):
freq = self.find_frequency(y, x[1] - x[0])
amp = (y.max() - y.min()) / 2.0
guess = [amp, freq, 0, 0] # amplitude, freq, phase,offset
# print (guess)
OS = y.mean()
try:
par, pcov = self.optimize.curve_fit(self.sineFunc, x, y - OS, guess)
except:
return None
vf = self.sineFunc(t, par[0], par[1], par[2], par[3])
diff = sum((v - vf) ** 2) / max(v)
if diff > self.error_limit:
guess[2] += np.pi / 2 # try an out of phase
try:
# print 'L1: diff = %5.0f frset= %6.3f fr = %6.2f phi = %6.2f'%(diff, res,par[1]*1e6,par[2])
par, pcov = self.optimize.curve_fit(self.sineFunc, x, y, guess)
except:
return None
vf = self.sineFunc(t, par[0], par[1], par[2], par[3])
diff = sum((v - vf) ** 2) / max(v)
if diff > self.error_limit:
# print 'L2: diff = %5.0f frset= %6.3f fr = %6.2f phi = %6.2f'%(diff, res,par[1]*1e6,par[2])
return None
else:
pass
# print 'fixed ',par[1]*1e6
return par, vf
def amp_spectrum(self, v, si, nhar=8):
# voltages, samplimg interval is seconds, number of harmonics to retain
from numpy import fft
NP = len(v)
frq = fft.fftfreq(NP, si)[:NP / 2] # take only the +ive half of the frequncy array
amp = abs(fft.fft(v)[:NP / 2]) / NP # and the fft result
index = amp.argmax() # search for the tallest peak, the fundamental
if index == 0: # DC component is dominating
index = amp[4:].argmax() # skip frequencies close to zero
return frq[:index * nhar], amp[:index * nhar] # restrict to 'nhar' harmonics
def dampedSine(self, x, amp, freq, phase, offset, damp):
"""
A damped sine wave function
"""
return offset + amp * np.exp(-damp * x) * np.sin(abs(freq) * x + phase)
def getGuessValues(self, xReal, yReal, func='sine'):
if (func == 'sine' or func == 'damped sine'):
N = len(xReal)
offset = np.average(yReal)
yhat = self.fftpack.rfft(yReal - offset)
idx = (yhat ** 2).argmax()
freqs = self.fftpack.rfftfreq(N, d=(xReal[1] - xReal[0]) / (2 * np.pi))
frequency = freqs[idx]
amplitude = (yReal.max() - yReal.min()) / 2.0
phase = 0.
if func == 'sine':
return amplitude, frequency, phase, offset
if func == 'damped sine':
return amplitude, frequency, phase, offset, 0
def arbitFit(self, xReal, yReal, func, **args):
N = len(xReal)
guess = args.get('guess', [])
try:
results, pcov = self.optimize.curve_fit(func, xReal, yReal, guess)
pcov[0] *= 1e6
return True, results, pcov
except:
return False, [], []
def fft(self, ya, si):
'''
Returns positive half of the Fourier transform of the signal ya.
Sampling interval 'si', in milliseconds
'''
ns = len(ya)
if ns % 2 == 1: # odd values of np give exceptions
ns -= 1 # make it even
ya = ya[:-1]
v = np.array(ya)
tr = abs(np.fft.fft(v)) / ns
frq = np.fft.fftfreq(ns, si)
x = frq.reshape(2, ns // 2)
y = tr.reshape(2, ns // 2)
return x[0], y[0]
def sineFitAndDisplay(self, chan, displayObject):
'''
chan : an object containing a get_xaxis, and a get_yaxis method.
displayObject : an object containing a setValue method
Fits against a sine function, and writes to the object
'''
fitres = None
fit = ''
try:
fitres = self.sineFit(chan.get_xaxis(), chan.get_yaxis())
if fitres:
amp, freq, offset, phase = fitres
if amp > 0.05: fit = 'Voltage=%s\nFrequency=%s' % (
apply_si_prefix(amp, 'V'), apply_si_prefix(freq, 'Hz'))
except Exception as e:
fitres = None
if not fitres or len(fit) == 0: fit = 'Voltage=%s\n' % (apply_si_prefix(np.average(chan.get_yaxis()), 'V'))
displayObject.setValue(fit)
if fitres:
return fitres
else:
return 0, 0, 0, 0
def rmsAndDisplay(self, data, displayObject):
'''
data : an array of numbers
displayObject : an object containing a setValue method
Fits against a sine function, and writes to the object
'''
rms = self.RMS(data)
displayObject.setValue('Voltage=%s' % (apply_si_prefix(rms, 'V')))
return rms
def RMS(self, data):
data = np.array(data)
return np.sqrt(np.average(data * data))
def butter_notch(self, lowcut, highcut, fs, order=5):
from scipy.signal import butter
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='bandstop')
return b, a
def butter_notch_filter(self, data, lowcut, highcut, fs, order=5):
from scipy.signal import lfilter
b, a = self.butter_notch(lowcut, highcut, fs, order=order)
y = lfilter(b, a, data)
return y
SI_PREFIXES = {k: v for k, v in zip(range(-24, 25, 3), "yzafpnµm kMGTPEZY")}
SI_PREFIXES[0] = ""
def frexp10(x: float) -> Tuple[float, int]:
"""Return the base 10 fractional coefficient and exponent of x, as pair (m, e).
This function is analogous to math.frexp, only for base 10 instead of base 2.
If x is 0, m and e are both 0. Else 1 <= abs(m) < 10. Note that m * 10**e is not
guaranteed to be exactly equal to x.
Parameters
----------
x : float
Number to be split into base 10 fractional coefficient and exponent.
Returns
-------
(float, int)
Base 10 fractional coefficient and exponent of x.
Examples
--------
>>> frexp10(37)
(3.7, 1)
"""
if x == 0:
coefficient, exponent = 0.0, 0
else:
log10x = math.log10(abs(x))
exponent = int(math.copysign(math.floor(log10x), log10x))
coefficient = x / 10 ** exponent
return coefficient, exponent
def apply_si_prefix(value: float, unit: str, precision: int = 2) -> str:
"""Scale :value: and apply appropriate SI prefix to :unit:.
Parameters
----------
value : float
Number to be scaled.
unit : str
Base unit of :value: (without prefix).
precision : int, optional
:value: will be rounded to :precision: decimal places. The default value is 2.
Returns
-------
str
"<scaled> <prefix><unit>", such that 1 <= <scaled> < 1000.
Examples
-------
apply_si_prefix(0.03409, "V")
'34.09 mV'
"""
coefficient, exponent = frexp10(value)
si_exponent = exponent - (exponent % 3)
si_coefficient = coefficient * 10 ** (exponent % 3)
if abs(si_exponent) > max(SI_PREFIXES):
raise ValueError("Exponent out of range of available prefixes.")
return f"{si_coefficient:.{precision}f} {SI_PREFIXES[si_exponent]}{unit}"