Guest User

Untitled

a guest
Feb 15th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. import array
  2. import math
  3. import time
  4.  
  5. import audiobusio
  6. import board
  7.  
  8.  
  9. def mean(values):
  10. return sum(values) / len(values)
  11.  
  12.  
  13. def normalized_rms(values):
  14. minbuf = int(mean(values))
  15. sum_of_samples = sum(
  16. float(sample - minbuf) * (sample - minbuf)
  17. for sample in values
  18. )
  19.  
  20. return math.sqrt(sum_of_samples / len(values))
  21.  
  22.  
  23. mic = audiobusio.PDMIn(
  24. board.MICROPHONE_CLOCK,
  25. board.MICROPHONE_DATA,
  26. sample_rate=16000,
  27. bit_depth=16
  28. )
  29. samples = array.array('H', [0] * 160)
  30. mic.record(samples, len(samples))
  31.  
  32. while True:
  33. mic.record(samples, len(samples))
  34. magnitude = normalized_rms(samples)
  35. print(((magnitude),))
  36. time.sleep(0.1)
Add Comment
Please, Sign In to add comment