• 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)
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:

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

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

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.


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.

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 08, 2024.

This page (Programming the Microphone) was last updated on Mar 08, 2024.

Text editor powered by tinymce.