Need Help with Incorrect Decibel Calculation for INMP441 Sensor on ESP32

Cskiwi
Posts: 1
Joined: Sun Jan 21, 2024 2:33 pm

Need Help with Incorrect Decibel Calculation for INMP441 Sensor on ESP32

Postby Cskiwi » Sun Jan 21, 2024 2:52 pm

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

MicroController
Posts: 1217
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

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

Postby MicroController » Fri Feb 16, 2024 10:12 pm

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.

Who is online

Users browsing this forum: No registered users and 138 guests