From 510adcae573f367673f45cca63b9bb0be3462cb2 Mon Sep 17 00:00:00 2001 From: Seppo Ingalsuo Date: Mon, 30 Nov 2026 14:47:38 +0200 Subject: [PATCH 1/2] audio: mfcc: run VAD on unclamped log Mel spectrum with PCAN In the PCAN mode the output features are computed from the linear Mel energies and the Q9.23 log Mel values are only read by the VAD. The Mel-only mmax tracking, top_db clamp, offset and scale were still applied to them. The PCAN configuration has top_db = 0 and mmax = 0, so every log Mel value below zero was raised to zero and the VAD saw max(ln(Mel power), 0). This made the VAD an absolute level gate: the weighted energy stayed at zero in room noise and quiet speech, and the VAD reported speech only for loud input. MicroWakeWord gates its detections with the VAD flag, so a keyword spoken below about -38 LUFS at the MFCC input was never detected even when the model scored it at 99%. On a PTL SoundWire DMIC device a 3 dB quieter talker turned a reliable detection into a miss, with 27 inferences of the keyword gated. Skip the clamp, offset and scale in the PCAN mode so the VAD sees the unclamped natural log Mel power, and use a separate threshold for it. MFCC_VAD_ENERGY_THRESHOLD_PCAN is 2.2, i.e. 9.6 dB above the tracked noise floor. In sof-testbench4 runs with noise at -56 LUFS the VAD stays off in pink and white noise and detects the keyword down to -53 LUFS. With fan-like exercise bike noise the VAD is active for about 20% of the noise-only time. A threshold of 2.5 avoided that but on the device missed weak words such as "front" with a desk fan running. The int8 PCAN output features are bit-exact with the previous version. The other MFCC modes are not changed. Document the VAD input and thresholds in the README. Signed-off-by: Seppo Ingalsuo --- src/audio/mfcc/README.md | 2 + src/audio/mfcc/mfcc_common.c | 92 +++++++++++++++++---------- src/audio/mfcc/mfcc_setup.c | 8 +++ src/include/sof/audio/mfcc/mfcc_vad.h | 12 ++++ 4 files changed, 81 insertions(+), 33 deletions(-) diff --git a/src/audio/mfcc/README.md b/src/audio/mfcc/README.md index 6c7f598110a9..346c3bf08442 100644 --- a/src/audio/mfcc/README.md +++ b/src/audio/mfcc/README.md @@ -23,6 +23,8 @@ One build supports all output modes, selected at runtime by the configuration bl AGC8 is a light alternative to PCAN without the per-channel noise estimate and the `tflite-micro` dependency. A single gain is added to all Q9.23 Mel log values of a hop. The gain attacks instantly to keep the hop peak at or below +1.0, and releases towards a target with a slower step when the VAD reports speech. The values are clamped to -1.0 .. +1.0 and converted to int8 Q1.7. The blob fields `agc_target`, `agc_floor` (Q8.7) and `agc_release`, `agc_release_speech` (Q9.23 per hop) set the parameters, zero selects the default. +The optional VAD (`enable_vad`) tracks a per-bin noise floor of the Q9.23 Mel log values, which follows the signal down instantly and rises slowly, and reports speech when the A-weighted mean of the values is more than a threshold above the A-weighted floor. In the Mel log and AGC8 modes the VAD sees the values after the `top_db` clamp, `mel_offset` and `mel_scale`, and uses `MFCC_VAD_ENERGY_THRESHOLD` (0.2). In the PCAN mode the output comes from the linear Mel energies, so the clamp, offset and scale are skipped and the VAD sees the unclamped natural log Mel power. It then uses `MFCC_VAD_ENERGY_THRESHOLD_PCAN` (2.2, i.e. 9.6 dB above the floor), which makes the decision independent of the input level. MicroWakeWord gates its detections with the VAD flag. + --- ## 2. Architecture & Data Flow diff --git a/src/audio/mfcc/mfcc_common.c b/src/audio/mfcc/mfcc_common.c index 5af5a4b3179d..5619edfc9d06 100644 --- a/src/audio/mfcc/mfcc_common.c +++ b/src/audio/mfcc/mfcc_common.c @@ -255,6 +255,21 @@ void mfcc_source_copy_s32(struct sof_source *source, struct mfcc_buffer *buf, } #endif /* CONFIG_FORMAT_S32LE */ +/** + * \brief Check if the Mel features are computed with PCAN. + * + * \param[in] state MFCC processing state. + * \return True if PCAN is built in and enabled by the configuration. + */ +static inline bool mfcc_pcan_active(struct mfcc_state *state) +{ +#if CONFIG_COMP_MFCC_PCAN + return state->pcan.enable_pcan; +#else + return false; +#endif +} + /* * The main processing function for MFCC */ @@ -405,44 +420,55 @@ int mfcc_stft_process(struct processing_module *mod, struct mfcc_comp_data *cd) /* In Mel-only mode output Mel log spectra directly */ cc_count += state->dct.num_in; - /* Find peak mel value and track state->mmax in Q9.23 */ - if (config->dynamic_mmax) { - peak = state->mel_log_32[0]; - for (j = 1; j < state->dct.num_in; j++) { - if (state->mel_log_32[j] > peak) - peak = state->mel_log_32[j]; + /* In PCAN mode the output features come from mel_linear and + * mel_log_32 is only read by the VAD. Skip the mmax tracking, + * clamp, offset and scale so that the VAD sees the unclamped + * log Mel spectrum. With the PCAN configuration top_db = 0 the + * clamp would otherwise raise every value below mmax to mmax, + * hiding quiet speech from the VAD. + */ + if (!mfcc_pcan_active(state)) { + /* Find peak mel value and track state->mmax in Q9.23 */ + if (config->dynamic_mmax) { + peak = state->mel_log_32[0]; + for (j = 1; j < state->dct.num_in; j++) { + if (state->mel_log_32[j] > peak) + peak = state->mel_log_32[j]; + } + + /* Jump to peak immediately if higher, decay otherwise */ + if (peak > state->mmax) { + state->mmax = peak; + } else { + /* Q9.23 * Q1.15, result Q9.23. The coefficient + * is small so no need for saturation. + */ + s = (int64_t)peak - state->mmax; + state->mmax += Q_MULTSR_32X32(s, config->mmax_coef, + 23, 15, 23); + } } - /* Jump to peak immediately if higher, decay otherwise */ - if (peak > state->mmax) { - state->mmax = peak; - } else { - /* Q9.23 * Q1.15, result Q9.23. The coefficient is small - * so no need for saturation. - */ - s = (int64_t)peak - state->mmax; - state->mmax += - Q_MULTSR_32X32(s, config->mmax_coef, 23, 15, 23); + /* Clamp Mel values lower than mmax - top_db, add offset, and scale. + * Config top_db and mel_offset are Q9.7, shift to Q9.23. + */ + clamp_value = state->mmax - ((int32_t)config->top_db << 16); + for (j = 0; j < state->dct.num_in; j++) { + mel_value = state->mel_log_32[j]; + if (mel_value < clamp_value) + mel_value = clamp_value; + + /* Q9.23 * Q4.12, result Q9.23 */ + s = (int64_t)mel_value + + ((int32_t)config->mel_offset << 16); + state->mel_log_32[j] = + sat_int32(Q_MULTSR_32X32(s, config->mel_scale, + 23, 12, 23)); } - } - /* Clamp Mel values lower than mmax - top_db, add offset, and scale. - * Config top_db and mel_offset are Q9.7, shift to Q9.23. - */ - clamp_value = state->mmax - ((int32_t)config->top_db << 16); - for (j = 0; j < state->dct.num_in; j++) { - mel_value = state->mel_log_32[j]; - if (mel_value < clamp_value) - mel_value = clamp_value; - - /* Q9.23 * Q4.12, result Q9.23 */ - s = (int64_t)mel_value + ((int32_t)config->mel_offset << 16); - state->mel_log_32[j] = - sat_int32(Q_MULTSR_32X32(s, config->mel_scale, 23, 12, 23)); + /* Enable this to check mmax decay */ + comp_dbg(dev, "state->mmax = %d", state->mmax); } - - /* Enable this to check mmax decay */ - comp_dbg(dev, "state->mmax = %d", state->mmax); } else { /* Convert Q9.23 to Q9.7 for 16-bit DCT */ for (j = 0; j < state->dct.num_in; j++) diff --git a/src/audio/mfcc/mfcc_setup.c b/src/audio/mfcc/mfcc_setup.c index 6d4e132e974c..19c8956d8446 100644 --- a/src/audio/mfcc/mfcc_setup.c +++ b/src/audio/mfcc/mfcc_setup.c @@ -473,6 +473,14 @@ int mfcc_setup(struct processing_module *mod, int max_frames, int sample_rate, i comp_err(dev, "Failed VAD init"); goto free_out_stage; } + +#if CONFIG_COMP_MFCC_PCAN + /* With PCAN the VAD sees the unclamped log Mel spectrum, see + * mfcc_stft_process(), so it needs its own threshold. + */ + if (config->enable_pcan) + cd->vad.energy_threshold = MFCC_VAD_ENERGY_THRESHOLD_PCAN; +#endif } if (config->enable_pcan) { diff --git a/src/include/sof/audio/mfcc/mfcc_vad.h b/src/include/sof/audio/mfcc/mfcc_vad.h index 048ea475eba1..7888b28c1428 100644 --- a/src/include/sof/audio/mfcc/mfcc_vad.h +++ b/src/include/sof/audio/mfcc/mfcc_vad.h @@ -43,6 +43,18 @@ struct processing_module; */ #define MFCC_VAD_ENERGY_THRESHOLD 1677722 +/** + * \brief Energy threshold for speech detection with PCAN in Q9.23 (2.2 * 2^23). + * + * With PCAN the VAD input is the unclamped natural log Mel power, so the + * threshold is 2.2 * 10 * log10(e) = 9.6 dB above the tracked noise floor. + * The noise floor follows the per-frame minimum of stationary noise, which + * makes noise-only frames reach up to about 1.8 (7.8 dB) above it. Fan-like + * noise reaches up to about 2.35, which keeps the VAD active for some of the + * noise-only time, but a higher threshold misses weak speech onsets. + */ +#define MFCC_VAD_ENERGY_THRESHOLD_PCAN 18454938 + /** * \brief Hangover frame count to keep VAD active after last speech detection. */ From 751bfaa1d0a173f8de153d6c5a5f379608f796a2 Mon Sep 17 00:00:00 2001 From: Seppo Ingalsuo Date: Mon, 30 Nov 2026 14:47:46 +0200 Subject: [PATCH 2/2] audio: microwakeword: start debug max VAD energy from INT32_MIN With PCAN the MFCC VAD energy is the weighted natural log Mel power, which is negative. The debug statistics started the maximum from zero, so the shutdown summary always printed max_E=0. Start it from INT32_MIN instead. Signed-off-by: Seppo Ingalsuo --- src/audio/microwakeword/mww.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/audio/microwakeword/mww.c b/src/audio/microwakeword/mww.c index f8f2e6d821e2..307d70395247 100644 --- a/src/audio/microwakeword/mww.c +++ b/src/audio/microwakeword/mww.c @@ -166,7 +166,7 @@ struct mww_comp_data { uint32_t dbg_hop_count; uint32_t dbg_active_hops; int32_t dbg_max_f_max; - int32_t dbg_max_energy; + int32_t dbg_max_energy; /**< Max VAD energy in Q9.23, negative with PCAN */ int32_t dbg_peak_prob; #endif @@ -504,7 +504,7 @@ __cold static int mww_init(struct processing_module *mod) cd->dbg_hop_count = 0; cd->dbg_active_hops = 0; cd->dbg_max_f_max = -128; - cd->dbg_max_energy = 0; + cd->dbg_max_energy = INT32_MIN; cd->dbg_peak_prob = 0; #endif mod->max_sinks = 0; @@ -1023,7 +1023,7 @@ static int mww_reset(struct processing_module *mod) cd->dbg_hop_count = 0; cd->dbg_active_hops = 0; cd->dbg_max_f_max = -128; - cd->dbg_max_energy = 0; + cd->dbg_max_energy = INT32_MIN; cd->dbg_peak_prob = 0; #endif memset(cd->feature_buf, 0, sizeof(cd->feature_buf));