Author Topic: STM32 decibel meter  (Read 2577 times)

0 Members and 1 Guest are viewing this topic.

Offline reyntjensmTopic starter

  • Regular Contributor
  • *
  • Posts: 119
  • Country: be
STM32 decibel meter
« on: May 03, 2021, 08:23:08 pm »
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?
 

Offline technix

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: STM32 decibel meter
« Reply #1 on: May 05, 2021, 03:52:08 am »
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.
 

Offline reyntjensmTopic starter

  • Regular Contributor
  • *
  • Posts: 119
  • Country: be
Re: STM32 decibel meter
« Reply #2 on: May 17, 2021, 05:32:46 pm »
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?
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14309
  • Country: fr
Re: STM32 decibel meter
« Reply #3 on: May 17, 2021, 05:41:06 pm »
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?

I don't get what you mean. I2S doesn't carry PDM.
 

Online hugo

  • Regular Contributor
  • *
  • Posts: 165
  • Country: ca
Re: STM32 decibel meter
« Reply #4 on: May 17, 2021, 06:59:06 pm »
Quote
Has anyone made something similar?

Andy 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
 
The following users thanked this post: boB

Offline bugnate

  • Regular Contributor
  • *
  • Posts: 58
  • Country: us
Re: STM32 decibel meter
« Reply #5 on: May 17, 2021, 08:09:28 pm »
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...

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?

As has been said, if you have an I2S microphone and things are working, you better have I2S PCM 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.
« Last Edit: May 17, 2021, 08:24:04 pm by bugnate »
 

Offline reyntjensmTopic starter

  • Regular Contributor
  • *
  • Posts: 119
  • Country: be
Re: STM32 decibel meter
« Reply #6 on: May 17, 2021, 09:10:07 pm »
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...

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?

According to STM datasheets. The signal from the microphone is a PDM signal. So i guess i will have to convert this into a PCM signal before i can do any math on it or use it with the sound meter library( the input for the sound meter library is " a audio stream"). I have configured DMA as suggested in the STM app notes but i have no idea on how to use and implement the callback functions. The I2S interface is set to 16bit data on 32bit frame length.
As has been said, if you have an I2S microphone and things are working, you better have I2S PCM 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.
 

Offline bugnate

  • Regular Contributor
  • *
  • Posts: 58
  • Country: us
Re: STM32 decibel meter
« Reply #7 on: May 17, 2021, 09:22:26 pm »
According to STM datasheets. The signal from the microphone is a PDM signal. So i guess i will have to convert this into a PCM signal before i can do any math on it or use it with the sound meter library( the input for the sound meter library is " a audio stream"). I have configured DMA as suggested in the STM app notes but i have no idea on how to use and implement the callback functions. The I2S interface is set to 16bit data on 32bit frame length.

This is a non sequitur. What exactly is your microphone (part number)? A PDM microphone is not compatible with an I2S interface. Full stop.
« Last Edit: May 17, 2021, 09:26:16 pm by bugnate »
 

Offline reyntjensmTopic starter

  • Regular Contributor
  • *
  • Posts: 119
  • Country: be
Re: STM32 decibel meter
« Reply #8 on: May 17, 2021, 09:27:58 pm »
According to STM datasheets. The signal from the microphone is a PDM signal. So i guess i will have to convert this into a PCM signal before i can do any math on it or use it with the sound meter library( the input for the sound meter library is " a audio stream"). I have configured DMA as suggested in the STM app notes but i have no idea on how to use and implement the callback functions. The I2S interface is set to 16bit data on 32bit frame length.

This is a non sequitur. What exactly is your microphone (part number)? A PDM microphone is not compatible with an I2S interface. Full stop.

 

Offline bugnate

  • Regular Contributor
  • *
  • Posts: 58
  • Country: us
Re: STM32 decibel meter
« Reply #9 on: May 17, 2021, 09:52:34 pm »
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.pdf

If 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.

 

Offline reyntjensmTopic starter

  • Regular Contributor
  • *
  • Posts: 119
  • Country: be
Re: STM32 decibel meter
« Reply #10 on: May 17, 2021, 10:07:06 pm »
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.pdf

If 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.

what?!? 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
« Last Edit: May 17, 2021, 10:11:46 pm by reyntjensm »
 

Offline bugnate

  • Regular Contributor
  • *
  • Posts: 58
  • Country: us
Re: STM32 decibel meter
« Reply #11 on: May 17, 2021, 10:13:38 pm »
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.
what!?! could you explain me why the PDM mic's can't be used with I2S? I have managed to receive some data from the mic with I2S after following this app note:

I 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.
« Last Edit: May 17, 2021, 10:21:25 pm by bugnate »
 

Offline reyntjensmTopic starter

  • Regular Contributor
  • *
  • Posts: 119
  • Country: be
Re: STM32 decibel meter
« Reply #12 on: May 17, 2021, 10:17:00 pm »
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.
what!?! could you explain me why the PDM mic's can't be used with I2S? I have managed to receive some data from the mic with I2S after following this app note:

I 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, and the next step is to do the PDM-to-PCM conversion, which I believe was your original question.

Ok 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?
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf