Skip to content

Commit 6a5a44c

Browse files
authored
Merge pull request #1677 from FoamyGuy/crunch_numbers_fast_ulab
update number_crunching for ulab renames and other fixes
2 parents 695b660 + f5cba4b commit 6a5a44c

5 files changed

Lines changed: 38 additions & 43 deletions

File tree

ulab_Crunch_Numbers_Fast/benchmark.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import time
22
import math
3-
import ulab
4-
import ulab.numerical
3+
from ulab import numpy as np
54

65
def mean(values):
76
return sum(values) / len(values)
@@ -17,15 +16,15 @@ def normalized_rms(values):
1716

1817
def normalized_rms_ulab(values):
1918
# this function works with ndarrays only
20-
minbuf = ulab.numerical.mean(values)
19+
minbuf = np.mean(values)
2120
values = values - minbuf
22-
samples_sum = ulab.numerical.sum(values * values)
21+
samples_sum = np.sum(values * values)
2322
return math.sqrt(samples_sum / len(values))
2423

2524
# Instead of using sensor data, we generate some data
2625
# The amplitude is 5000 so the rms should be around 5000/1.414 = 3536
2726
nums_list = [int(8000 + math.sin(i) * 5000) for i in range(100)]
28-
nums_array = ulab.array(nums_list)
27+
nums_array = np.array(nums_list)
2928

3029
def timeit(s, f, n=100):
3130
t0 = time.monotonic_ns()
@@ -38,5 +37,5 @@ def timeit(s, f, n=100):
3837
print("Computing the RMS value of 100 numbers")
3938
timeit("traditional", lambda: normalized_rms(nums_list))
4039
timeit("ulab, with ndarray, some implementation in python", lambda: normalized_rms_ulab(nums_array))
41-
timeit("ulab only, with list", lambda: ulab.numerical.std(nums_list))
42-
timeit("ulab only, with ndarray", lambda: ulab.numerical.std(nums_array))
40+
timeit("ulab only, with list", lambda: np.std(nums_list))
41+
timeit("ulab only, with ndarray", lambda: np.std(nums_array))

ulab_Crunch_Numbers_Fast/cluebarometer.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import adafruit_bmp280
44
import board
55
import displayio
6-
import ulab
7-
import ulab.filter
6+
from ulab import numpy as np
87

98
# Blank the screen. Scrolling text causes unwanted delays.
109
d = displayio.Group()
@@ -15,7 +14,7 @@
1514
# Transition bandwidth: 0.16Hz
1615
# Window type: Hamming
1716
# Filter has 311 coefficients
18-
taps = ulab.array([
17+
taps = np.array([
1918
-0.000050679794726066, -0.000041099278318167, -0.000031279920668665,
2019
-0.000021183486597150, -0.000010770285292045, +0.000000000000000000,
2120
+0.000011167446754809, +0.000022770999889941, +0.000034847558259864,
@@ -31,8 +30,8 @@
3130
+0.000489380560741255, +0.000495598812776238, +0.000499367108150093,
3231
+0.000500461794444300, +0.000498660907473236, +0.000493745927786584,
3332
+0.000485503600706003, +0.000473727809671115, +0.000458221492033063,
34-
+0.000438798585855176, +0.000415285995764155, +0.000387525565446236,
35-
+0.000355376044004699, +0.000318715033091691, +0.000277440901501588,
33+
+0.000438798585855176, +0.000415285995764155, +0.000387525565446236] +
34+
[+0.000355376044004699, +0.000318715033091691, +0.000277440901501588,
3635
+0.000231474653767861, +0.000180761739242710, +0.000125273788160487,
3736
+0.000065010261293197, +0.000000000000000000, -0.000069697336247377,
3837
-0.000143989957415198, -0.000222752767634882, -0.000305826338672358,
@@ -63,8 +62,8 @@
6362
+0.013033779302562583, +0.013549501700820601, +0.014054359855790191,
6463
+0.014546911139352909, +0.015025735735186426, +0.015489442135386880,
6564
+0.015936672560369614, +0.016366108277098043, +0.016776474791055797,
66-
+0.017166546887869318, +0.017535153501103896, +0.017881182383493146,
67-
+0.018203584559716979, +0.018501378539810983, +0.018773654273367416,
65+
+0.017166546887869318, +0.017535153501103896, +0.017881182383493146] +
66+
[+0.018203584559716979, +0.018501378539810983, +0.018773654273367416,
6867
+0.019019576825867947, +0.019238389759765797, +0.019429418204303113,
6968
+0.019592071599501125, +0.019725846101288819, +0.019830326636332028,
7069
+0.019905188596781104, +0.019950199166862841, +0.019965218274992248,
@@ -85,8 +84,8 @@
8584
+0.002630616483032971, +0.002203584045179127, +0.001792701398335993,
8685
+0.001398597909331569, +0.001021830060775982, +0.000662880773589522,
8786
+0.000322159059450752, +0.000000000000000001, -0.000303334951952833,
88-
-0.000587657355512251, -0.000852850856865070, -0.001098869965763655,
89-
-0.001325738543930948, -0.001533548017297868, -0.001722455325210333,
87+
-0.000587657355512251, -0.000852850856865070, -0.001098869965763655] +
88+
[-0.001325738543930948, -0.001533548017297868, -0.001722455325210333,
9089
-0.001892680620886389, -0.002044504738458278, -0.002178266442894981,
9190
-0.002294359479963247, -0.002393229444145818, -0.002475370483091317,
9291
-0.002541321857718479, -0.002591664377536210, -0.002627016731069256,
@@ -143,7 +142,7 @@ def sleep_deadline(deadline_ns):
143142

144143
# And our data structures
145144
# The most recent data samples, equal in number to the filter taps
146-
data = ulab.zeros(len(taps))
145+
data = np.zeros(len(taps))
147146
t0 = deadline = time.monotonic_ns()
148147
n = 0
149148
# Take an initial reading to subtract off later, so that the graph in mu
@@ -163,9 +162,9 @@ def sleep_deadline(deadline_ns):
163162
data = data + value
164163
else:
165164
# Otherwise, add it as the next sample
166-
ulab.numerical.roll(data, 1)
165+
data = np.roll(data, 1)
167166
data[-1] = value
168-
filtered = ulab.numerical.sum(data * taps)
167+
filtered = np.sum(data * taps)
169168
# Actually print every 10th value. This prints about 1.6 values per
170169
# second. You can print values more quickly by removing the 'if' and
171170
# making the print unconditional, or change the frequency of prints up

ulab_Crunch_Numbers_Fast/cluepulse.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import adafruit_apds9960.apds9960
44
import board
55
import digitalio
6-
import ulab
7-
import ulab.filter
6+
from ulab import numpy as np
87

98
# Blank the screen. Scrolling text causes unwanted delays.
109
import displayio
@@ -18,7 +17,7 @@
1817
# Window type: Regular
1918
# Number of coefficients: 31
2019
# Manually trimmed to 16 coefficients
21-
taps = ulab.array([
20+
taps = np.array([
2221
+0.861745279666917052/2,
2322
-0.134728583242092248,
2423
-0.124472980501612152,
@@ -89,7 +88,7 @@ def main():
8988

9089
# And our data structures
9190
# The most recent data samples, equal in number to the filter taps
92-
data = ulab.zeros(len(taps))
91+
data = np.zeros(len(taps))
9392
# The filtered value on the previous iteration
9493
old_value = 1
9594
# The times of the most recent pulses registered. Increasing this number
@@ -111,11 +110,11 @@ def main():
111110
deadline += dt
112111
sleep_deadline(deadline)
113112
value = sum(sensor.color_data) # Combination of all channels
114-
ulab.numerical.roll(data, 1)
113+
data = np.roll(data, 1)
115114
data[-1] = value
116115
# Compute the new filtered variable by applying the filter to the
117116
# recent data samples
118-
filtered = ulab.numerical.sum(data * taps)
117+
filtered = np.sum(data * taps)
119118

120119
# We gathered enough data to fill the filters, and
121120
# the light value crossed the zero line in the positive direction

ulab_Crunch_Numbers_Fast/ledwave.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22

33
import board
44
import neopixel
5-
from _pixelbuf import wheel
6-
import ulab
7-
import ulab.filter
5+
from rainbowio import colorwheel as wheel
6+
from ulab import numpy as np
87

98
# Customize your neopixel configuration here...
109
pixel_pin = board.D5
1110
num_pixels = 96
1211
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.1,
1312
auto_write=False, pixel_order=neopixel.RGB)
1413

15-
ddt = ulab.array([1.,-2.,1.])
14+
ddt = np.array([1.,-2.,1.])
1615
def step(u, um, f, n, dx, dt, c):
1716
dt2 = dt*dt
1817
C2 = (c*dt/dx)**2
19-
deriv = ulab.filter.convolve(u, ddt)[1:-1] * C2
18+
deriv = np.convolve(u, ddt)[1:-1] * C2
2019
up = -um + u * 2 + deriv + f * dt2
2120
up[0] = 0
2221
up[n-1] = 0
@@ -29,11 +28,11 @@ def main():
2928
w = [wheel(i) for i in range(256)]
3029

3130
# This sets up the initial wave as a smooth gradient
32-
u = ulab.zeros(num_pixels)
33-
um = ulab.zeros(num_pixels)
34-
f = ulab.zeros(num_pixels)
31+
u = np.zeros(num_pixels)
32+
um = np.zeros(num_pixels)
33+
f = np.zeros(num_pixels)
3534

36-
slope = ulab.linspace(0, 256, num=num_pixels)
35+
slope = np.linspace(0, 256, num=num_pixels)
3736
th = 1
3837

3938
# the first time is always random (is that a contradiction?)

ulab_Crunch_Numbers_Fast/waterfall.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
import board
88
import audiobusio
99
import displayio
10-
import ulab
11-
import ulab.fft
12-
import ulab.vector
10+
from ulab import numpy as np
11+
from ulab.scipy.signal import spectrogram
1312

1413
display = board.DISPLAY
1514

@@ -75,14 +74,14 @@ def main():
7574

7675
while True:
7776
mic.record(samples_bit, len(samples_bit))
78-
samples = ulab.array(samples_bit[3:])
79-
spectrogram1 = ulab.fft.spectrogram(samples)
77+
samples = np.array(samples_bit[3:])
78+
spectrogram1 = spectrogram(samples)
8079
# spectrum() is always nonnegative, but add a tiny value
8180
# to change any zeros to nonzero numbers
82-
spectrogram1 = ulab.vector.log(spectrogram1 + 1e-7)
81+
spectrogram1 = np.log(spectrogram1 + 1e-7)
8382
spectrogram1 = spectrogram1[1:(fft_size//2)-1]
84-
min_curr = ulab.numerical.min(spectrogram1)
85-
max_curr = ulab.numerical.max(spectrogram1)
83+
min_curr = np.min(spectrogram1)
84+
max_curr = np.max(spectrogram1)
8685

8786
if max_curr > max_all:
8887
max_all = max_curr
@@ -94,7 +93,7 @@ def main():
9493
# Plot FFT
9594
data = (spectrogram1 - min_curr) * (51. / (max_all - min_curr))
9695
# This clamps any negative numbers to zero
97-
data = data * ulab.array((data > 0))
96+
data = data * np.array((data > 0))
9897
graph.show(data)
9998

10099
main()

0 commit comments

Comments
 (0)