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/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)); 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. */