• First we initialize the mic and give it a variable so we can access it later. This is done through something called PDMIn. PDM is the type of microphone we're using.
  • In order to record samples of audio later we need to prepare a buffer of sorts to record that audio into. We will make a blank array called samples that will later be filled with values from our microphones input.
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, frequency=16000, bit_depth=16)
samples = array.array('H', [0] * NUM_SAMPLES)
led_pixels_image_VY0eqSaw6H.jpeg
The microphone on the Circuit Playground Express
  • Next we need to create a way to calculate the loudness of the sounds coming into the microphone. How do we do this?

Let's look at potential sound that might come into the microphone:

led_pixels_image_6GukXr6HO9.jpeg
Sound signal of a human voice. Source: Signal Processing Stack Exchange

If we zoom in really close it might look like this:

led_pixels_image_XiwpmSLliP.jpeg
Source: The SAMI Group

If we simplify the signal a bit and make it a sine wave, we can see the amplitude of the signal over time.

 

led_pixels_image_wlwDjJebCj.jpeg
Sine wave. Source: SFU Acoustics Department
  • To find the "loudness" of a sound we need to find the amplitude of the signal
  • We can calculate the amplitude of one peak pretty easily, but how do we find the average of the amplitude of all the peaks in a signal?
  • Well there are a couple ways:

Peak to peak will give you the average maximum of the peak values but unfortunately is not as accurate because of irregularities in the samples.

Root means square is a better method of calculating the average amplitude of a sound because it throws out the irregularities in the samples.

led_pixels_image_h4QazVqfbt.jpeg
RMS vs. peak to peak. Source: dosits.org
  • Here is a great resource that explains how root means square works and how it is calculated.
# Remove DC bias before computing RMS.
def normalized_rms(values):
   minbuf = int(mean(values))
   return math.sqrt(sum(float((sample - minbuf) * (sample - minbuf)) for sample in values) / len(values))
def mean(values):
   return (sum(values) / len(values))

This guide was first published on Mar 28, 2018. It was last updated on Mar 28, 2018.

This page (Programming the Microphone) was last updated on Mar 25, 2018.

Text editor powered by tinymce.