PDM Gain in Arduino examples

The PDM mic gain is being set incorrectly in the Arduino examples. Currently PDM.setGain() is called before PDM.begin(). When it is done in this order, it doesn’t appear to change the mic levels. However, if you call setGain() after begin(), the levels change. This can be easily tested out using the PDMSerialPlotter(). I am not sure if this impacts performance at all though, since the audio gets normalized anyhow.

PDM.setBufferSize(4096);

// initialize PDM with:
// - one channel (mono mode)
// - a 16 kHz sample rate
if (!PDM.begin(1, EI_CLASSIFIER_FREQUENCY)) {
    ei_printf("Failed to start PDM!");
    microphone_inference_end();

    return false;
}
PDM.setGain(80);

HERO!

We’ve been looking at the Nano gain for quite a bit when we did the first port and never realized why this wasn’t working. Result is low volume when recording which can be fixed now, and I think it’ll help a bit with inferencing too.

cc @Arjan

Haha! yea, this one was bugging me for a while because it didn’t seem like the gain had any effect. I happened to just try swapping the functions order on a whim. However, I just saw this weekend that the TF Micro speech example sets the Gain after the Begin:

That was exactly what I have been also looking for. Thank you!

One more question. How do you tweak the mic gain when using the edge-impulse-deamon to record audio data? Are you using a custom code to collect the wav recordings in order to train the neural net?

@anasvaf You’d have to tweak https://github.com/edgeimpulse/firmware-arduino-nano-33-ble-sense and rebuild for now. We’ll fix it in the next few days.

Thank you for the prompt response! I appreciate it :slight_smile:
By the way, in my case i placed the gain at “/src/sensors/ei_microphone.cpp” after the PDM.begin() function in order to make it work

1 Like