1
- import time
1
+ import utime
2
2
import math
3
3
import ulab
4
4
import ulab .numerical
5
5
6
+ def timeit (f , * args , ** kwargs ):
7
+ func_name = str (f ).split (' ' )[1 ]
8
+ def new_func (* args , ** kwargs ):
9
+ t = utime .ticks_us ()
10
+ result = f (* args , ** kwargs )
11
+ print ('execution time: ' , utime .ticks_diff (utime .ticks_us (), t ), ' us' )
12
+ return result
13
+ return new_func
14
+
6
15
def mean (values ):
7
16
return sum (values ) / len (values )
8
17
18
+ @timeit
9
19
def normalized_rms (values ):
10
20
minbuf = int (mean (values ))
11
21
samples_sum = sum (
@@ -15,26 +25,37 @@ def normalized_rms(values):
15
25
16
26
return math .sqrt (samples_sum / len (values ))
17
27
28
+ @timeit
18
29
def normalized_rms_ulab (values ):
19
30
minbuf = ulab .numerical .mean (values )
20
31
values = values - minbuf
21
32
samples_sum = ulab .numerical .sum (values * values )
22
33
return math .sqrt (samples_sum / len (values ))
23
34
24
35
36
+ @timeit
37
+ def normalized_std_ulab (values ):
38
+ return ulab .numerical .std (values )
39
+
40
+ @timeit
41
+ def normalized_std_ulab_iterable (values ):
42
+ return ulab .numerical .std (values )
43
+
25
44
# Instead of using sensor data, we generate some data
26
45
# The amplitude is 5000 so the rms should be around 5000/1.414 = 3536
27
46
nums_list = [int (8000 + math .sin (i ) * 5000 ) for i in range (100 )]
28
47
nums_array = ulab .array (nums_list )
29
48
30
- def timeit (s , f , n = 100 ):
31
- t0 = time .monotonic_ns ()
32
- for _ in range (n ):
33
- x = f ()
34
- t1 = time .monotonic_ns ()
35
- r = (t1 - t0 ) * 1e-6 / n
36
- print ("%-20s : %8.3fms [result=%f]" % (s , r , x ))
37
-
38
49
print ("Computing the RMS value of 100 numbers" )
39
- timeit ("traditional" , lambda : normalized_rms (nums_list ))
40
- timeit ("ulab" , lambda : normalized_rms_ulab (nums_array ))
50
+
51
+ print ('in python' )
52
+ normalized_rms (nums_list )
53
+
54
+ print ('\n in ulab, with some implementation in python' )
55
+ normalized_rms_ulab (nums_array )
56
+
57
+ print ('\n in ulab only, with ndarray' )
58
+ normalized_std_ulab (nums_array )
59
+
60
+ print ('\n in ulab only, with list' )
61
+ normalized_std_ulab_iterable (nums_list )
0 commit comments