1
- from __future__ import print_function
2
- import time
1
+ import math
2
+ from typing import Tuple
3
3
4
4
import numpy as np
5
5
6
+
6
7
class analyticsClass ():
7
8
"""
8
9
This class contains methods that allow mathematical analysis such as curve fitting
@@ -37,13 +38,6 @@ def __init__(self):
37
38
else :
38
39
self .signal = signal
39
40
40
- try :
41
- from PSL .commands_proto import applySIPrefix as applySIPrefix
42
- except ImportError :
43
- self .applySIPrefix = None
44
- else :
45
- self .applySIPrefix = applySIPrefix
46
-
47
41
def sineFunc (self , x , a1 , a2 , a3 , a4 ):
48
42
return a4 + a1 * np .sin (abs (a2 * (2 * np .pi )) * x + a3 )
49
43
@@ -259,11 +253,11 @@ def sineFitAndDisplay(self, chan, displayObject):
259
253
if fitres :
260
254
amp , freq , offset , phase = fitres
261
255
if amp > 0.05 : fit = 'Voltage=%s\n Frequency=%s' % (
262
- self . applySIPrefix (amp , 'V' ), self . applySIPrefix (freq , 'Hz' ))
256
+ apply_si_prefix (amp , 'V' ), apply_si_prefix (freq , 'Hz' ))
263
257
except Exception as e :
264
- fires = None
258
+ fitres = None
265
259
266
- if not fitres or len (fit ) == 0 : fit = 'Voltage=%s\n ' % (self . applySIPrefix (np .average (chan .get_yaxis ()), 'V' ))
260
+ if not fitres or len (fit ) == 0 : fit = 'Voltage=%s\n ' % (apply_si_prefix (np .average (chan .get_yaxis ()), 'V' ))
267
261
displayObject .setValue (fit )
268
262
if fitres :
269
263
return fitres
@@ -278,7 +272,7 @@ def rmsAndDisplay(self, data, displayObject):
278
272
Fits against a sine function, and writes to the object
279
273
'''
280
274
rms = self .RMS (data )
281
- displayObject .setValue ('Voltage=%s' % (self . applySIPrefix (rms , 'V' )))
275
+ displayObject .setValue ('Voltage=%s' % (apply_si_prefix (rms , 'V' )))
282
276
return rms
283
277
284
278
def RMS (self , data ):
@@ -298,3 +292,71 @@ def butter_notch_filter(self, data, lowcut, highcut, fs, order=5):
298
292
b , a = self .butter_notch (lowcut , highcut , fs , order = order )
299
293
y = lfilter (b , a , data )
300
294
return y
295
+
296
+
297
+ SI_PREFIXES = {k : v for k , v in zip (range (- 24 , 25 , 3 ), "yzafpnµm kMGTPEZY" )}
298
+ SI_PREFIXES [0 ] = ""
299
+
300
+
301
+ def frexp10 (x : float ) -> Tuple [float , int ]:
302
+ """Return the base 10 fractional coefficient and exponent of x, as pair (m, e).
303
+
304
+ This function is analogous to math.frexp, only for base 10 instead of base 2.
305
+ If x is 0, m and e are both 0. Else 1 <= abs(m) < 10. Note that m * 10**e is not
306
+ guaranteed to be exactly equal to x.
307
+
308
+ Parameters
309
+ ----------
310
+ x : float
311
+ Number to be split into base 10 fractional coefficient and exponent.
312
+
313
+ Returns
314
+ -------
315
+ (float, int)
316
+ Base 10 fractional coefficient and exponent of x.
317
+
318
+ Examples
319
+ --------
320
+ >>> frexp10(37)
321
+ (3.7, 1)
322
+ """
323
+ if x == 0 :
324
+ coefficient , exponent = 0.0 , 0
325
+ else :
326
+ log10x = math .log10 (abs (x ))
327
+ exponent = int (math .copysign (math .floor (log10x ), log10x ))
328
+ coefficient = x / 10 ** exponent
329
+
330
+ return coefficient , exponent
331
+
332
+
333
+ def apply_si_prefix (value : float , unit : str , precision : int = 2 ) -> str :
334
+ """Scale :value: and apply appropriate SI prefix to :unit:.
335
+
336
+ Parameters
337
+ ----------
338
+ value : float
339
+ Number to be scaled.
340
+ unit : str
341
+ Base unit of :value: (without prefix).
342
+ precision : int, optional
343
+ :value: will be rounded to :precision: decimal places. The default value is 2.
344
+
345
+ Returns
346
+ -------
347
+ str
348
+ "<scaled> <prefix><unit>", such that 1 <= <scaled> < 1000.
349
+
350
+ Examples
351
+ -------
352
+ apply_si_prefix(0.03409, "V")
353
+ '34.09 mV'
354
+ """
355
+ coefficient , exponent = frexp10 (value )
356
+ si_exponent = exponent - (exponent % 3 )
357
+ si_coefficient = coefficient * 10 ** (exponent % 3 )
358
+
359
+ if abs (si_exponent ) > max (SI_PREFIXES ):
360
+ raise ValueError ("Exponent out of range of available prefixes." )
361
+
362
+ return f"{ si_coefficient :.{precision }f} { SI_PREFIXES [si_exponent ]} { unit } "
0 commit comments