# How to convert from PDM to PCM ?

Started by September 1, 2015
```I have a client who is using a MEMS microphone (SPM1423HM4H-B, 3.125MSPS). The
output of the microphone is a PDM signal.

Datasheet:
http://media.digikey.com/pdf/Data%20Sheets/Knowles%20Acoustics%20PDFs/SPM1423HM4H-B.pdf

In the client's test setup an emitter emits an acoustic signal. The emitted signal
has frequency components around 40kHz.

When I reviewed the client's setup, I noticed a couple of things:

1. It looks like the max bandwidth of the microphone is 10kHz
2. The client is converting the PDM signal to a PCM signal in the following way:

/* Buffer PDM signal for further processing and decimation */
for (i=0;i<N;i++)
{
tmp=0;
for(imod=0;imod<16;imod++)
{ // Decimation factor 16
tmp+=((pdmSignal[i+imod]>>2)&1);
}

k++;
buffer[k]=tmp;
}

/* Low-Pass Filter Signal To Obtain PCM signal */
for(i=1;i<N;i++)
{
pcmSignal[i]=0;
for(imod=0;imod<10;imod++)
{
pcmSignal[i]+=buffer[i+imod];
}
}

for(i=1;i<N;i++)
{
for(imod=0;imod<15;imod++)
{
pcmSignal[i]+=pcmSignal[i+imod];
}
}

The above procedure for converting from PDM to PCM seems incorrect to me. Can
somebody explain to me the logic behind this conversion routine? Can you list the
generic steps one takes when converting a PDM signal to PCM?

Am I understanding the datasheet for the microphone correctly? It looks like the max
bandwidth of the captured signal is 10000Hz? If so, it doesn't make sense to try and
capture an acoustic signal which has frequency components around 40kHz.

Thank you!

```
```On 02.09.15 00.22, Mauritz Jameson wrote:
> I have a client who is using a MEMS microphone (SPM1423HM4H-B, 3.125MSPS). The
output of the microphone is a PDM signal.
>
> Datasheet:
http://media.digikey.com/pdf/Data%20Sheets/Knowles%20Acoustics%20PDFs/SPM1423HM4H-B.pdf
>
> In the client's test setup an emitter emits an acoustic signal. The emitted signal
has frequency components around 40kHz.

The microphone is not intended to be used above 10 kHz. There may be
resonances or whatever. The high frequency components might be amplified
unexpectedly and might cause overdrive or aliasing or during further
processing.

> When I reviewed the client's setup, I noticed a couple of things:
>
> 1. It looks like the max bandwidth of the microphone is 10kHz

Yep.

> 2. The client is converting the PDM signal to a PCM signal in the following way:
>
> /* Buffer PDM signal for further processing and decimation */
> for (i=0;i<N;i++)
> {
>     tmp=0;
>     for(imod=0;imod<16;imod++)
>     { // Decimation factor 16
>        tmp+=((pdmSignal[i+imod]>>2)&1);

- Only the bit #2 of pdmSignal is taken, hopefully the right port or
whatever pdmSignal contains.
- This is a moving avarage filter over 16 bins.

>     }
>
>     k++;
>     buffer[k]=tmp;

Hopefully k is -1 at initialization. Using arrays from 1..N rarely hits
the nail on the head in C.

> }
>
>
> /* Low-Pass Filter Signal To Obtain PCM signal */
> for(i=1;i<N;i++)
> {
>     pcmSignal[i]=0;
>     for(imod=0;imod<10;imod++)
>     {
>        pcmSignal[i]+=buffer[i+imod];

- Again a moving average filter over 10 bins.
Now you have a trapezoid impulse response with a total width of 26.
- pcmSignal[0] stays uninitialized, while pcmSignal[N] is assigned.

>     }
> }
>
> for(i=1;i<N;i++)
> {
>     for(imod=0;imod<15;imod++)
>     {
>        pcmSignal[i]+=pcmSignal[i+imod];

- Again a moving average filter over 15 bins.
Now you have some impulse response 41 bins length with parabolic slopes.
At least this has not yet been initialized by the above loop.

>     }
> }
>
>
> The above procedure for converting from PDM to PCM seems incorrect to me.  Can
somebody explain to me the logic behind this conversion routine?

In principal it could work. It is just some FIR filter implemented by 3
moving average stages. This is O(N), i.e. fast.

However, unless there is some sophisticated overlap and add algorithm
around this code it will produce undefined results because of the array
out of bounds accesses.

I can't check whether the frequency response of this FIR is well chosen
because I don't know the sampling frequency of the controller.

> Can you list the generic steps one takes when converting a PDM signal to PCM?

All you need is low pass filtering. But you need a high decimation
factor to get a reasonable dynamic range.
If you just take approximately 30 samples as above you should not expect
more that roughly 5 significant bits in the result, i.e. 30 dB.

> Am I understanding the datasheet for the microphone correctly? It looks like the
max bandwidth of the captured signal is 10000Hz?

No, this is only the specified response. Above 10 kHz the microphone
will have undefined behavior. And it looks like the frequency response
shows some resonance in this range. No more, no less.

> If so, it doesn't make sense to try and capture an acoustic signal which has
frequency components around 40kHz.

Well, almost any acoustic signal has some energy around 40 kHz. Usually
very low energy for speech or music.

And well, if you know the complex frequency response of that particular
microphone, e.g. by calibration, and if the microphone is sufficiently
linear at your sound pressure levels, you could apply an inverse
transformation to the recorded signal to get reasonable results. Many
electret capsules work pretty well in the ultrasonic range, although the
frequency response is no longer flat.

Marcel
```
```On Wednesday, September 2, 2015 at 3:07:58 AM UTC-4, Marcel Mueller wrote:
> On 02.09.15 00.22, Mauritz Jameson wrote:
> > I have a client who is using a MEMS microphone (SPM1423HM4H-B, 3.125MSPS). The
output of the microphone is a PDM signal.
>

what is PDM?

Mark
```
```On Wednesday, September 2, 2015 at 9:37:24 AM UTC-4, mako...@yahoo.com wrote:
> On Wednesday, September 2, 2015 at 3:07:58 AM UTC-4, Marcel Mueller wrote:
> > On 02.09.15 00.22, Mauritz Jameson wrote:
> > > I have a client who is using a MEMS microphone (SPM1423HM4H-B, 3.125MSPS). The
output of the microphone is a PDM signal.
> >
>
> what is PDM?
>
> Mark

Mark

```
```There's a chip for that.

Bob
```
```The chip I mentioned will work at a sample rate of 96khz so 40 kHz is still in the
pass-band although it's probably drooping a bit by then.
Another issue is that the delta-sigma modulator used inside the mic has a noise
floor that is rising above 20khz. If you put that together with the fact that the
mic element is rolling off pretty sharply, it's likely that the snr around 40 kHz
will be very low. However if you are doing some form of range-finding or object
detection you can band pass filter the signal around 40khz to pull it out of the
noise. If you have access to the signal that is driving the ultrasonic speaker (if
there is one) then you could do an I/Q down mix and low pass filter which would give
you the envelope (I'm assuming you are trying to measure some time delay).
```
```On Tuesday, September 1, 2015 at 6:22:04 PM UTC-4, Mauritz Jameson wrote:
> I have a client who is using a MEMS microphone (SPM1423HM4H-B, 3.125MSPS). The
output of the microphone is a PDM signal.
>
> Datasheet:
http://media.digikey.com/pdf/Data%20Sheets/Knowles%20Acoustics%20PDFs/SPM1423HM4H-B.pdf
>
> In the client's test setup an emitter emits an acoustic signal. The emitted signal
has frequency components around 40kHz.
>
> When I reviewed the client's setup, I noticed a couple of things:
>
> 1. It looks like the max bandwidth of the microphone is 10kHz
> 2. The client is converting the PDM signal to a PCM signal in the following way:
>
> /* Buffer PDM signal for further processing and decimation */
> for (i=0;i<N;i++)
> {
>    tmp=0;
>    for(imod=0;imod<16;imod++)
>    { // Decimation factor 16
>       tmp+=((pdmSignal[i+imod]>>2)&1);
>    }
>
>    k++;
>    buffer[k]=tmp;
> }
>
>
> /* Low-Pass Filter Signal To Obtain PCM signal */
> for(i=1;i<N;i++)
> {
>    pcmSignal[i]=0;
>    for(imod=0;imod<10;imod++)
>    {
>       pcmSignal[i]+=buffer[i+imod];
>    }
> }
>
> for(i=1;i<N;i++)
> {
>    for(imod=0;imod<15;imod++)
>    {
>       pcmSignal[i]+=pcmSignal[i+imod];
>    }
> }
>
>
> The above procedure for converting from PDM to PCM seems incorrect to me. Can
somebody explain to me the logic behind this conversion routine? Can you list the
generic steps one takes when converting a PDM signal to PCM?
>
> Am I understanding the datasheet for the microphone correctly? It looks like the
max bandwidth of the captured signal is 10000Hz? If so, it doesn't make sense to try
and capture an acoustic signal which has frequency components around 40kHz.
>
> Thank you!

The fact that microphone manufacturers specify frequency response up to 10-15 KHz
doesn't mean that mics are limited to this range.

(My explanation is that it's too much hassle for manufacturers to test mics between
20 KHz and say 100K KHz, as ordinary speakers don't work and they would need to use
ultrasonic transducers - those are high Q thus have very limited bandwidth around
resonant frequency, so they would have to custom manufacture many of those
transducers to cover the entire frequency range)

https://www.digikey.com/Web%20Export/Supplier%20Content/Knowles_423/PDF/knowels-an-ultrasonic-applications.pdf?redirected=1

In fact, even some cheap electret mic capsules show excellent performance at 40 KHz
(of course, you would also need decent preamp and ADC)
Sensitivity does down as frequency goes up, but noise also decreases, thus SNR is
pretty high at 40 KHz (Very easy to see with a scope)

What really annoys me is that manufacturers of those new digital MEMS microphones
apparently don't care to provide decent breakout boards and standard libraries for
standard microprocessors to convert PDM to PCM and to analyze the resulting digital
signals (can't use analog scope with those)

Well, as VLV used to say, this is what you get for free...

Good luck with your client. Make sure he or she pays upfront :-)

```
```angrydude wrote:
> On Tuesday, September 1, 2015 at 6:22:04 PM UTC-4, Mauritz Jameson
> wrote:
>> I have a client who is using a MEMS microphone (SPM1423HM4H-B,
>> 3.125MSPS). The output of the microphone is a PDM signal.
>>
>> Datasheet:
>>
http://media.digikey.com/pdf/Data%20Sheets/Knowles%20Acoustics%20PDFs/SPM1423HM4H-B.pdf
>>
>>
>>
In the client's test setup an emitter emits an acoustic signal. The
emitted signal has frequency components around 40kHz.
>>
>> When I reviewed the client's setup, I noticed a couple of things:
>>
>> 1. It looks like the max bandwidth of the microphone is 10kHz 2.
>> The client is converting the PDM signal to a PCM signal in the
>> following way:
>>
>> /* Buffer PDM signal for further processing and decimation */ for
>> (i=0;i<N;i++) { tmp=0; for(imod=0;imod<16;imod++) { // Decimation
>> factor 16 tmp+=((pdmSignal[i+imod]>>2)&1); }
>>
>> k++; buffer[k]=tmp; }
>>
>>
>> /* Low-Pass Filter Signal To Obtain PCM signal */ for(i=1;i<N;i++)
>> { pcmSignal[i]=0; for(imod=0;imod<10;imod++) {
>> pcmSignal[i]+=buffer[i+imod]; } }
>>
>> for(i=1;i<N;i++) { for(imod=0;imod<15;imod++) {
>> pcmSignal[i]+=pcmSignal[i+imod]; } }
>>
>>
>> The above procedure for converting from PDM to PCM seems incorrect
>> to me. Can somebody explain to me the logic behind this conversion
>> routine? Can you list the generic steps one takes when converting a
>> PDM signal to PCM?
>>
>> Am I understanding the datasheet for the microphone correctly? It
>> looks like the max bandwidth of the captured signal is 10000Hz? If
>> so, it doesn't make sense to try and capture an acoustic signal
>> which has frequency components around 40kHz.
>>
>> Thank you!
>
> The fact that microphone manufacturers specify frequency response up
> to 10-15 KHz doesn't mean that mics are limited to this range.
>
> (My explanation is that it's too much hassle for manufacturers to
> test mics between 20 KHz and say 100K KHz, as ordinary speakers don't
> work and they would need to use ultrasonic transducers - those are
> high Q thus have very limited bandwidth around resonant frequency, so
> they would have to custom manufacture many of those transducers to
> cover the entire frequency range)
>

It's not necessary to use speakers to test microphones. A spark gap
generator will work just fine. These are broadband to the limit of
how air conducts sound.

I know of one case where somebody adapted a camera flash to
this and used it outside to minimize the effect of
room resonance.

>
https://www.digikey.com/Web%20Export/Supplier%20Content/Knowles_423/PDF/knowels-an-ultrasonic-applications.pdf?redirected=1
>
>  In fact, even some cheap electret mic capsules show excellent
> performance at 40 KHz (of course, you would also need decent preamp
> and ADC) Sensitivity does down as frequency goes up, but noise also
> decreases, thus SNR is pretty high at 40 KHz (Very easy to see with a
> scope)
>
> What really annoys me is that manufacturers of those new digital MEMS
> microphones apparently don't care to provide decent breakout boards
> and standard libraries for standard microprocessors to convert PDM to
> PCM and to analyze the resulting digital signals (can't use analog
> scope with those)
>
> Well, as VLV used to say, this is what you get for free...
>
> Good luck with your client. Make sure he or she pays upfront :-)
>
>

--
Les Cargill
```
```On 9/2/2015 9:39 AM, makolber@yahoo.com wrote:
> On Wednesday, September 2, 2015 at 9:37:24 AM UTC-4, mako...@yahoo.com wrote:
>> On Wednesday, September 2, 2015 at 3:07:58 AM UTC-4, Marcel Mueller wrote:
>>> On 02.09.15 00.22, Mauritz Jameson wrote:
>>>> I have a client who is using a MEMS microphone (SPM1423HM4H-B, 3.125MSPS). The
output of the microphone is a PDM signal.
>>>
>>
>> what is PDM?
>>
>> Mark
>
> Google is my friend....never mind

Sometimes it can be hard to find the meaning of abbreviations.  I guess
you found this one ok though.

--

Rick
```
```Most digital microphones with PDM outputs use a 2nd-order delta-sigma modulator
inside, so the noise is rising by 12 db/octave. Usually the rising a/d noise
"crosses over" the thermal noise of the mic preamp near the top of the audio band.

It is conventional to use sinc filters where the order of the sinc is one higher
than the order of the modulator so that the noise is dropping gas a function of
frequency. This is done to avoid aliased noise when you decimate. So the use of 3
cascaded moving-average filters in this application is standard, although the fact
that they are all different lengths is a bit unusual.

Also the code you showed only does filtering and not decimation. That may happen
somewhere farther down the line. It's still possible to do the signal processing at
the full rate but it's quite wasteful of mips.
```