Code: Select all
import analogio
import board
import math
signal = analogio.AnalogIn(board.A2)
readings = []
def sample_reads(readings):
for i in range(100):
readings.append(signal.value)
return readings
while True:
sample_reads(readings)
sumof = sum(readings)
mean = sumof / 100
print(mean)
readings.clear()
In this benchmark:
https://learn.adafruit.com/ulab-crunch- ... -benchmark
The "ulab only, with ndarray" completes the task much faster, and that's something I'm interested in taking advantage of. Is it as simple as importing numpy and using "np.sum" instead of "sum"? It also mentions that "ulab.numpy.std computes most quickly by moving all operations from Python to ulab". I'm guessing that just means it has the library itself do everything, instead of python?
Lastly, other than condensing some of those lines, like "mean = sum(readings) / 100", in general, might there be a more effective and fast way of finding the average of 100 samples?
EDIT: I just saw that numpy has a function for finding a mean. I'm still not clear on how to properly use it, but I imagine it's faster than using math.