STM32 decibel meter - Page 1
Forum section: electronics / general

author: reyntjensm
author_rank: Regular Contributor
timestamp: May 03, 2021, 08:23:08 pm
content: I'm trying to design a decibel meter with STM32. I have a few microphones with an I2S interface. Since i have no previous experience with I2S, i'm having some difficulties to understand how this should be implemented. I've found the sound meter library (https://www.st.com/resource/en/user_manual/dm00275616-sound-meter-library-software-expansion-for-stm32cube-stmicroelectronics.pdf). But i don't understand how to implement this. Has anyone made something similar?

author: technix
author_rank: Super Contributor
timestamp: May 05, 2021, 03:52:08 am
content: For getting sound into the chip, just follow the pinout to hook up the I2S bus to the MCU. Then it is all DSP processing so prepare your mind for intense math.Are you going to do weighing? If not, you can just take the peak-to-peak value from the mic (a rolling maximum/rolling minimum operation to stay within the time domain) and run it through a transfer function to get the dB value. If you need weighing (as mostly do) you need to FFT the incoming samples and run that vector through the weighing function and then a transfer function. The purpose of the final transfer function is to allow calibration.

author: reyntjensm
author_rank: Regular Contributor
timestamp: May 17, 2021, 05:32:46 pm
content: So i'm able to receive PDM data from the I2S interface. But it's still unclear to me if the Sound Meter Library can work with PDM or if the signal should be transformed into PCM signal?

author: SiliconWizard
author_rank: Super Contributor
timestamp: May 17, 2021, 05:41:06 pm
content: Quote from: reyntjensm on May 17, 2021, 05:32:46 pmI don't get what you mean. I2S doesn't carry PDM.

author: hugo
author_rank: Regular Contributor
timestamp: May 17, 2021, 06:59:06 pm
content: QuoteAndy Brown is also using an I2S microphone is his latest project:https://andybrown.me.uk/2021/03/13/usb-microphone/The code is available:https://github.com/andysworkshop/usb-microphone

author: bugnate
author_rank: Regular Contributor
timestamp: May 17, 2021, 08:09:28 pm
content: Haven't used this particular library, but have recently completed a hw project using a ST H7 + I2S microphone as a decibel meter and some ML stuffs.technix outlined the high level steps for you pretty well above. If that doesn't make sense to you, you might do some research or ask some questions until it does. I can comment on the low-level stuff a bit...Quote from: reyntjensm on May 17, 2021, 05:32:46 pmAs has been said, if you have an I2S microphone and things are working, you better haveI2SPCM data, not PDM. Are you using DMA? Using the HAL? Have you been able to pull some clips and import them into e.g., Audacity (Import --> Raw Data) and they sound sane?I can think of two things that might be tripping you up. First is that if you have mono data (only one microphone) then half of your samples will be empty because I2S is nominally stereo. You will have to discard the empty channel data unless the library handles this for you (doesn't seem like it). Second, what is the I2S frame format of your microphone and how to do you want to process (e.g., int16, int32)? There is probably a bit of a mismatch. Most I2S mics I've worked with are 24-bit. This means that if you want to process as 4-byte samples you will need to sign extend your data. Unless you are going straight to float, but that wouldn't be compatible with your sound meter library.

author: reyntjensm
author_rank: Regular Contributor
timestamp: May 17, 2021, 09:10:07 pm
content: Quote from: bugnate on May 17, 2021, 08:09:28 pm

author: bugnate
author_rank: Regular Contributor
timestamp: May 17, 2021, 09:22:26 pm
content: Quote from: reyntjensm on May 17, 2021, 09:10:07 pmThis is a non sequitur. What exactly is your microphone (part number)? A PDM microphone is not compatible with an I2S interface. Full stop.

author: reyntjensm
author_rank: Regular Contributor
timestamp: May 17, 2021, 09:27:58 pm
content: Quote from: bugnate on May 17, 2021, 09:22:26 pm

author: bugnate
author_rank: Regular Contributor
timestamp: May 17, 2021, 09:52:34 pm
content: Yup, that is a PDM microphone. You will not be able to use that with an I2S interface in a normal way.It *can* work as a sort of SPI-degenerate case, but you are in for a treat:https://www.st.com/resource/en/application_note/dm00380469-interfacing-pdm-digital-microphones-using-stm32-mcus-and-mpus-stmicroelectronics.pdfIf you have a DFSDM available, you should use that instead.If you really did manage to get the above to work, then yes, you must do a PDM-to-PCM conversion before handing off to your library.

author: reyntjensm
author_rank: Regular Contributor
timestamp: May 17, 2021, 10:07:06 pm
content: Quote from: bugnate on May 17, 2021, 09:52:34 pmwhat?!? could you briefly explain why such a mic can't be used with I2S? I've followed the app note you suggested to get some data over I2S. And this is working. I don't know if this data is correct or meaningful. When using DMA, from what i understand. I should use ISR callback functions to copy the data from DMA into a buffer to start using it. Sadly enough my ISR callback functions are never called by the MCU

author: bugnate
author_rank: Regular Contributor
timestamp: May 17, 2021, 10:13:38 pm
content: Quote from: reyntjensm on May 17, 2021, 10:07:06 pmI said it can work. It is just confusing because it is not really I2S in that case, even if you are using the same pins. Hence the "not in a normal way". If you have followed the app note and gotten good results, then disregard my whinging, and the next step is to do the PDM-to-PCM conversion, which I believe was your original question.

author: reyntjensm
author_rank: Regular Contributor
timestamp: May 17, 2021, 10:17:00 pm
content: Quote from: bugnate on May 17, 2021, 10:13:38 pmOk i see. So far it's clear to me. Thank you for your feedback. So now i need to copy the data from DMA into a buffer and use this buffer with the PDM2PCM conversion right?