PDM audio software decoding on STM32 microcontrollers
1 Introduction
This application note presents the algorithms and architecture of an optimized software
implementation for PDM signal decoding and audio signal reconstruction when connecting
an ST MP45DT02 MEMS microphone with an STM32 microcontroller. It can directly take the
Pulse Density Modulated (PDM) data output from the microphone and convert it to 16-bit
pulse-code modulation (PCM) format.
This document also provides quick start information describing how to implement the PDM
Library for single microphone acquisition via I2S based on the STM32F4 microcontroller and
STM32F4DISCOVERY board.
For more details about this implementation, please refer to AN3997 Audio playback and recording using the STM32F4DISCOVERY.
Pulse density modulation, or PDM, is a form of modulation used to represent an analog
signal in the digital domain.
In a PDM signal, specific amplitude values are not encoded into pulses as they would be in
PCM. Instead it is the relative density of the pulses that corresponds to the analog signal's
amplitude.
To get the framed data from the PDM bit stream, decimation filters are usually used. The
first stage of decimation is used to reduce the sampling frequency, followed by a high pass
filter to remove the signal DC offset.
4/10Doc ID 022391 Rev 1
AN3998Hardware interface: microphone connection and acquisition
MS19892V1
Internal
Flash
I2S
PDM Lib
Microphone
MEMS
USB key
STM32
I2S clk to MIC clk
MIC Data to I2S SD
3 Hardware interface: microphone connection and
acquisition
The MP45DT02 MEMS microphone outputs a PDM signal, which is a high frequency (1 to
3.25 MHz) stream of 1-bit digital samples.
This output is acquired in blocks of 8 samples by using a synchronous serial port (SPI or
I2S) of the STM32 microcontroller. The microphone's PDM output is synchronous with its
input clock; therefore an STM32 SPI/ I2S peripheral generates a clock signal for the
microphone.
Figure 1.Block diagram of a microphone connection to an STM32.
Doc ID 022391 Rev 15/10
Software interface: digital signal processingAN3998
MS19893V1
PDM digital filtering
and decimation
Digital signal
conditioning
PDM in
Audio out
4 Software interface: digital signal processing
The data coming from the microphone is sent to the decimation process, which consists of
two parts: a decimation filter converting 1-bit PDM data to PCM data, followed by two
individually configurable IIR filters (low pass and high pass). The reconstructed audio is in
16-bit pulse-code modulation (PCM) format. After the conversion, it produces raw data that
can be handled depending on the application implementation (stored as wave/compressed
data in a mass storage media, transferred to an external audio codec DAC through I2S
peripheral...).
Figure 2.Digital signal processing
4.1 PDM digital filtering and decimation
The PDM signal from the microphone is filtered and decimated in order to obtain a sound
signal at the required frequency and resolution.
The frequency of the PDM data output from the microphone (which is the clock input to the
microphone) must be a multiple of the final audio output needed from the system. For
example, to perform a decimation of 80, for the output rate of 30 kHz, we need to provide a
clock frequency 2.4MHz to the microphone.
The output of the filter pipeline is a 16-bit value, we consider [-32768, 32767] as the output
range for a unitary gain (0 dB).
4.2 Digital signal conditioning
The digital audio signal resulting from the previous filter pipeline is further processed for
proper signal conditioning.
The first stage is a high pass filter designed mainly to remove the signal DC offset. It has
been implemented via an IIR filter with a cut-off frequency below the audible frequency
range in order to preserve signal quality.
The second stage is a low pass filter implemented using an IIR filter.
Both filters can be enabled/disabled and configured (cut-off frequencies) by using the filter
initialization function.Gain can be controlled by an external integer variable (MicGain) as
shown in the following equation: G = MicGain/64.
The PDM library is composed of a structure and the implementation of four PDM filter
functions. The library uses two buffers, the PDM Input buffer and the PCM Output buffer; the
application must define these buffers in the main program.
●Input buffer (data) is a uint8 variable with a length equal to (Output frequency / 1000 *
decimation factor * Input Microphone Channels / 8) at least.
●Output buffer (dataOut) is a uint16 variable with a length equal to (Output frequency /
1000 * Output Microphone Channels) at least.
The structure is defined in the pdm_filter.h file and is used to configure the filter; it is
composed as follows:
typedef struct {
uint16_t Fs;
float LP_HZ;
float HP_HZ;
uint16_t Out_MicChannels;
char InternalFilter[34];
} PDMFilter_InitStruct;
●
Fs: Defines the frequency output of the filter in Hz.
●LP_HZ: Defines the low pass filter cut-off frequency. If 0, the low pass filter is disabled.
●HP_HZ: Defines the high pass filter cut frequency. If 0, the high pass filter is disabled.
●In_MicChannels: Define the number of microphones in the input stream. This
parameter is used to specify the interlacing of microphones in the input buffer. The
PDM samples are grouped eight by eight in u8 format (8-bit).
●Out_MicChannels: Defines the number of microphones in the output stream; this
parameter is used to interlace different microphones in the output buffer. Each sample
is a 16-bit value.
●InternalFilter: Defines a 34 Byte memory used internally by the library during the PDM
decimation step.
The PDM audio software decoding library includes one header file pdm_filter.h and
binary/object codes for the following platforms:
●libPDMFilter_IAR.a : for IAR compiler
●libPDMFilter_Keil.lib : for ARM compiler
●libPDMFilter_GCC.a : for GNU compiler
This Library is provided in the "STM32F4-DISCOVERY board firmware package" (V1.10
and later) under the following path 'Utilities\STM32F4-Discovery'
5.1 PDM_Filter_Init
This function is used to initialize the filter of a single output channel. It is defined as follows:
Before calling the PDM_Filter_Init() function, the application code must enable the clock of
the STM32 microcontroller CRC peripheral (configuration done in RCC register).
Otherwise, the PDM_Filter_Init() function will fail and go into an infinite loop.
5.2 PDM_Filter_xx_xx
These functions are used to process a millisecond of PDM data from a single microphone.
They return a number of PCM samples equal to the frequency defined in the filter
initialization, divided by 1000. In case of frequencies that are not multiple of 1000 (like
44100 Hz or 22050 Hz), the samples returned by the function are equal to the floor division
of the frequencies (44 and 22).
data: is the input buffer containing the PDM; the application must pass to the function
the pointer to the first input sample of the microphone that must be processed.
●dataOut: is the output buffer processed by the PDM_Filter function. The application
must pass to the function the pointer to the first sample of the channel to be obtained.
●Volu me : is a value between 0 and 64 used to specify the microphone gain.
●Filter: is the structure containing all the filter parameters specified by the user using the
PDM_Filter_Init function.
There are four different implementations of this function depending on the decimation factor
(64 or 80) and on the LSB or MSB representation of the input buffer.
8/10Doc ID 022391 Rev 1
AN3998Revision history
6 Revision history
Table 1.Document revision history
DateRevisionChanges
27-Oct-20111Initial release.
Doc ID 022391 Rev 19/10
AN3998
Please Read Carefully:
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the
right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any
time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no
liability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this
document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products
or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such
third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED
WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS
OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY TWO AUTHORIZED ST REPRESENTATIVES, ST PRODUCTS ARE NOT
RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING
APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY,
DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE
GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void
any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any
liability of ST.
ST and the ST logo are trademarks or registered trademarks of ST in various countries.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.