PebbleOS v4.47.0-1075-g013613c8a
Loading...
Searching...
No Matches
PDM to PCM

Decimation of the 1-bit stream of a PDM microphone to 16-bit PCM. More...

Data Structures

struct  pbl_pdm_filter
 Filter of one microphone. More...
 

Macros

#define PBL_PDM_GAIN_MAX_MDB   42000
 Highest gain, in mdB.
 

Functions

int pbl_pdm_filter_init (struct pbl_pdm_filter *filter, uint32_t decimation, uint32_t settle)
 Initialize a filter, without gain.
 
void pbl_pdm_filter_set_gain (struct pbl_pdm_filter *filter, int32_t gain_mdb)
 Set the gain.
 
size_t pbl_pdm_filter_run (struct pbl_pdm_filter *filter, const uint8_t *in, size_t len, size_t in_stride, int16_t *out, size_t out_stride)
 Filter PDM bytes.
 

Detailed Description

Decimation of the 1-bit stream of a PDM microphone to 16-bit PCM.

Bytes of eight PDM samples go through a fifth-order sinc filter decimating by 8, then an equiripple low-pass FIR decimating by 8 or 16, flat to 0.4 times the output rate and at least 70 dB down from 0.6 times it. A high-pass filter at 1/800 of the output rate, 20 Hz at 16 kHz, then removes the offset of the microphone and its drift as it starts, and a gain of 0 to PBL_PDM_GAIN_MAX_MDB brings speech up from the low levels microphones give it. Without gain, a stream of only ones gives full scale.

One filter runs one microphone. The oldest of the eight samples of a byte is its most significant bit. The first samples are silence: until the FIR has seen as many as it has taps, and for the time the microphone takes to settle once clocked.

static struct pbl_pdm_filter s_filter;
pbl_pdm_filter_init(&s_filter, 64, 0);
pbl_pdm_filter_set_gain(&s_filter, 24000);
// 128 bytes of a 1.024 MHz PDM clock, 1 ms, give 16 samples at 16 kHz
n = pbl_pdm_filter_run(&s_filter, pdm, 128, 1, pcm, 1);
Filter of one microphone.
Definition pdm.h:45
int pbl_pdm_filter_init(struct pbl_pdm_filter *filter, uint32_t decimation, uint32_t settle)
Initialize a filter, without gain.
void pbl_pdm_filter_set_gain(struct pbl_pdm_filter *filter, int32_t gain_mdb)
Set the gain.
size_t pbl_pdm_filter_run(struct pbl_pdm_filter *filter, const uint8_t *in, size_t len, size_t in_stride, int16_t *out, size_t out_stride)
Filter PDM bytes.

Data Structure Documentation

◆ pbl_pdm_filter

struct pbl_pdm_filter

Filter of one microphone.

Macro Definition Documentation

◆ PBL_PDM_GAIN_MAX_MDB

#define PBL_PDM_GAIN_MAX_MDB   42000

Highest gain, in mdB.

Function Documentation

◆ pbl_pdm_filter_init()

int pbl_pdm_filter_init ( struct pbl_pdm_filter *  filter,
uint32_t  decimation,
uint32_t  settle 
)

Initialize a filter, without gain.

Parameters
filterFilter.
decimationPDM samples per output sample: 64 or 128.
settleOutput samples to silence while the microphone settles.
Returns
0, or -EINVAL for another decimation.

◆ pbl_pdm_filter_run()

size_t pbl_pdm_filter_run ( struct pbl_pdm_filter *  filter,
const uint8_t *  in,
size_t  len,
size_t  in_stride,
int16_t *  out,
size_t  out_stride 
)

Filter PDM bytes.

Parameters
filterFilter.
inFirst byte of PDM samples.
lenNumber of bytes.
in_strideDistance between bytes, in bytes, as for the bytes of one microphone of several interleaved.
outWhere to write the samples.
out_strideDistance between samples, in samples, as for one channel of several.
Returns
Number of samples written: len times 8 divided by the decimation, give or take one for the bytes the previous call left over.

◆ pbl_pdm_filter_set_gain()

void pbl_pdm_filter_set_gain ( struct pbl_pdm_filter *  filter,
int32_t  gain_mdb 
)

Set the gain.

Parameters
filterFilter.
gain_mdbGain in mdB, rounded to the dB and clamped to 0 to PBL_PDM_GAIN_MAX_MDB.