- 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)
- 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:
If we zoom in really close it might look like this:
If we simplify the signal a bit and make it a sine wave, we can see the amplitude of the signal over time.
- 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.
- Here is a great resource that explains how root means square works and how it is calculated.
- Below are the "user defined functions" we will later use to calculate the rms value of our samples.
# 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))
Page last edited March 08, 2024
Text editor powered by tinymce.