Page 1 of 1

Need Help with Incorrect Decibel Calculation for INMP441 Sensor on ESP32

Posted: Sun Jan 21, 2024 2:52 pm
by Cskiwi
Hi,

I'm currently working on a project where I need to record the decibels from an INMP441 sensor. I've implemented the following function to calculation it, but it's not producing accurate results.

Code: Select all

float DecibelController::calculateDecibel(SAMPLE_T *sampleBuffer)
{
  // get sample size
  int sample_size = sizeof(sampleBuffer) / sizeof(sampleBuffer[0]);
  
  // get highest peak
  float highest_peak = 0;
  for (int i = 0; i < sample_size; i += 2)
  {
    int16_t sample = (int16_t)((sampleBuffer[i + 1] << 8) | sampleBuffer[i]);
    float sample_abs = abs(sample);
    if (sample_abs > highest_peak)
    {
      highest_peak = sample_abs;
    }
  }

  // calculate dbA
  float rms = sqrt(highest_peak / (sample_size / 2));
  float dbA = 20 * log10(rms / REFERENCE_SOUND_PRESSURE);

  return dbA;
}

The full code base can be found here: https://github.com/cskiwi/tracker-v2

The problem is that the calculated decibel value doesn't match the expected values. For instance, when measuring with my phone, I get around 50dB, but the project outputs around 80dB.

But then the saved sound file is very quiet, which would made me think there is a couple of things wrong.

I've looked into various examples and repositories on the internet, but I couldn't find a solution. Some examples I tried include: I'm running out of ideas and things to try. Any assistance would be greatly appreciated.

Glenn

Re: Need Help with Incorrect Decibel Calculation for INMP441 Sensor on ESP32

Posted: Fri Feb 16, 2024 10:12 pm
by MicroController
Somewhat late, but:
Instead of

Code: Select all

float rms = sqrt(highest_peak / (sample_size / 2));
try

Code: Select all

float rms = sqrt(highest_peak * 2);
The "mean" of highest_peak in this case is highest_peak/1, not highest_peak/sample_size.