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 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:
- https://github.com/Makerfabs/Project_ES ... wm8960.cpp
- https://github.com/vitzaoral/ESP32-INMP ... roller.cpp
- https://github.com/ikostoski/esp32-i2s- ... 2s-slm.ino
Glenn