forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdmin_rms.py
More file actions
43 lines (29 loc) · 866 Bytes
/
pdmin_rms.py
File metadata and controls
43 lines (29 loc) · 866 Bytes
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
import audiobusio
import board
import digitalio
import array
import time
import math
trigger = digitalio.DigitalInOut(board.D4)
trigger.switch_to_output(True)
def mean(values):
return sum(values) / len(values)
def normalized_rms(values):
minbuf = int(mean(values))
samples_sum = sum(float(sample - minbuf) * (sample - minbuf) for sample in values)
return math.sqrt(samples_sum / len(values))
# signed 16 bit
s16 = array.array("H", [0] * 10000)
pdm = audiobusio.PDMIn(clock_pin=board.D11, data_pin=board.D12, sample_rate=24000, bit_depth=16)
print("starting read")
trigger.value = False
count = pdm.record(s16, len(s16))
trigger.value = True
print("read done")
print("recorded {} samples".format(count))
for v in s16[:count]:
print(v)
magnitude = normalized_rms(s16)
print("magnitude", magnitude)
print("count", count)
print("done")