Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .github/workflows/testbench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ jobs:
with:
path: sof

# MFCC with PCAN needs the TFLM microfrontend sources next to sof/,
# use the revision pinned in the west submanifest.
- name: Checkout TFLM microfrontend
run: |
rev=$(awk '/name: tflite-micro/ {f=1} f && /revision:/ {print $2; exit}' \
sof/submanifests/tflm.yml)
git init tflite-micro
cd tflite-micro
git remote add origin https://github.com/tensorflow/tflite-micro
git sparse-checkout set tensorflow/lite/experimental/microfrontend/lib
git fetch --depth 1 --filter=blob:none origin "$rev"
git checkout FETCH_HEAD

# note: libasound-dev needed for testbench build which still
# requires system ALSA headers to be present
- name: apt get
Expand Down
75 changes: 0 additions & 75 deletions scripts/tensorflow-clone.sh

This file was deleted.

1 change: 1 addition & 0 deletions src/arch/host/configs/library_defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CONFIG_COMP_IIR=y
CONFIG_COMP_IGO_NR=y
CONFIG_COMP_LEVEL_MULTIPLIER=y
CONFIG_COMP_MFCC=y
CONFIG_COMP_MFCC_PCAN=y
CONFIG_COMP_MODULE_ADAPTER=y
CONFIG_COMP_MULTIBAND_DRC=y
CONFIG_COMP_MUX=y
Expand Down
20 changes: 19 additions & 1 deletion src/audio/mfcc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
# SPDX-License-Identifier: BSD-3-Clause

if(CONFIG_COMP_MFCC_PCAN)
if(NOT DEFINED TFLM_PATH)
if(DEFINED SOF_ROOT_SOURCE_DIRECTORY AND EXISTS "${SOF_ROOT_SOURCE_DIRECTORY}/../tflite-micro")
set(TFLM_PATH "${SOF_ROOT_SOURCE_DIRECTORY}/../tflite-micro")
elseif(DEFINED sof_top_dir AND EXISTS "${sof_top_dir}/../tflite-micro")
set(TFLM_PATH "${sof_top_dir}/../tflite-micro")
elseif(EXISTS "${CMAKE_CURRENT_LIST_DIR}/../../../../tflite-micro")
set(TFLM_PATH "${CMAKE_CURRENT_LIST_DIR}/../../../../tflite-micro")
endif()
endif()
is_zephyr(zephyr)
if(zephyr)
zephyr_include_directories(${TFLM_PATH})
else()
target_include_directories(sof PRIVATE ${TFLM_PATH})
endif()
endif()

if(CONFIG_COMP_MFCC STREQUAL "m" AND DEFINED CONFIG_LLEXT)
add_subdirectory(llext ${PROJECT_BINARY_DIR}/mfcc_llext)
add_dependencies(app mfcc)
else()
add_local_sources(sof mfcc.c mfcc_setup.c mfcc_common.c mfcc_generic.c mfcc_hifi4.c mfcc_hifi3.c mfcc_vad.c)
add_local_sources(sof mfcc.c mfcc_setup.c mfcc_common.c mfcc_generic.c mfcc_hifi4.c mfcc_hifi3.c mfcc_vad.c mfcc_agc8.c)
if(CONFIG_IPC_MAJOR_4)
add_local_sources(sof mfcc_ipc4.c)
endif()
Expand Down
23 changes: 23 additions & 0 deletions src/audio/mfcc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,26 @@ config COMP_MFCC
The characteristic of the audio features are defined in the binary
control blob. Directory tools/tune/mfcc contains a tool to create
the configurations.

if COMP_MFCC

config COMP_MFCC_PCAN
bool "Enable PCAN (Per-Channel AGC Normalization) in MFCC"
default n
select MATH_PCAN
help
Select to build MFCC with PCAN (Per-Channel Automatic Gain Control
Normalization) support on the Mel filterbank energies. PCAN performs
dynamic per-channel gain control and piecewise polynomial root
compression to suppress stationary noise and enhance transients,
producing normalized features suitable for direct int8 quantization
downstream (e.g. microWakeWord). The topology PCAN configuration
fields are ignored when this option is disabled.

config COMP_MFCC_DEBUG_TRACE
bool "Verbose per-process debug traces in MFCC"
default n
help
Enable MFCC per-process debug prints in the hot path.

endif # COMP_MFCC
104 changes: 89 additions & 15 deletions src/audio/mfcc/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,99 @@
# MFCC Feature Extraction Architecture
# MFCC & PCAN Feature Extraction Architecture

This directory contains the Mel-Frequency Cepstral Coefficients (MFCC) feature extractor.
This directory contains the **Mel-Frequency Cepstral Coefficients (MFCC)** feature extractor and integrated **PCAN (Per-Channel AGC Normalization)** audio pre-processing component for Sound Open Firmware (SOF).

## Overview
---

MFCC extracts audio features commonly used as inputs for machine learning models, such as wake-word detection or speech recognition.
## 1. Overview

## Architecture Diagram
The MFCC module converts raw streaming time-domain PCM audio into compact acoustic feature representations (e.g. 13-bin cepstral coefficients, 40-bin or 80-bin Mel spectrograms) suitable for on-device machine learning models such as wake-word classifiers (TFLM, microWakeWord) and speech recognition engines (Whisper).

When enabled, **PCAN** applies per-channel adaptive dynamic gain control and piecewise root dynamic range compression across time to suppress stationary background noise and normalize channel energy before feature quantization.

### Output modes

One build supports all output modes, selected at runtime by the configuration blob:

| Mode | Blob settings | Output per hop |
|---|---|---|
| Cepstral coefficients | `num_ceps > 0` | int16 Q9.7 widened to int32 |
| Mel log spectra | `num_ceps == 0` | int32 Q9.23 |
| PCAN Mel | `num_ceps == 0`, `enable_pcan` (needs `CONFIG_COMP_MFCC_PCAN`) | int8 |
| Soft AGC Mel (AGC8) | `num_ceps == 0`, `enable_agc8` | int8 Q1.7 |

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.

---

## 2. Architecture & Data Flow

```mermaid
graph LR
In[Audio Frame] --> Win[Windowing]
Win --> FFT[FFT]
FFT --> Mel[Mel Filterbank]
Mel --> DCT[DCT]
DCT --> Out[MFCC Output Features]
In[Audio PCM Frame] --> Win[Windowing: Hamming/Hann]
Win --> FFT[FFT: 512-pt]
FFT --> Mel[Mel Filterbank: 40/80 Bins]
Mel --> PCAN[PCAN Gain Normalization]
PCAN --> Log[Log Scaling / dB]
Log --> VAD[VAD / DTX Silence Gating]
Log --> AGC8[Soft AGC int8]
VAD --> AGC8
Log --> DCT[DCT: Cepstral Matrix]
DCT --> Lifter[Cepstral Lifter]
Lifter --> Out[MFCC Output Features]
```

---

## 3. Dependencies

| Dependency | Purpose |
|---|---|
| **West Manifest (`west.yml`)** | Manages workspace dependencies including `tflite-micro` |
| **`tflite-micro`** | Provides Google's upstream Apache-2.0 `microfrontend` PCAN library (`pcan_gain_control.c`, `noise_reduction.c`) |
| **GNU Octave ($\ge 5.0$)** | Required for topology configuration generation (`tune/setup_mfcc.m`) and reference vector export |
| **CMake ($\ge 3.13$)** | SOF build system |
| **Zephyr SDK / Xtensa XCC** | DSP cross-compilation toolchains for Cadence HiFi3 / HiFi4 SIMD kernels |
| **CMocka** | Host unit testing framework |

---

## 4. Build & Tuning Instructions

### 4.1 Synchronize Dependencies via West
```bash
west update
```

### 4.2 Build Host Unit Tests
```bash
# Configure unit tests
cmake -S sof -B build_ut -DBUILD_UNIT_TESTS=ON -DBUILD_UNIT_TESTS_HOST=ON -DINIT_CONFIG=unit_test_defconfig

# Build all math and MFCC tests
cmake --build build_ut --target pcan
ctest --test-dir build_ut -R "pcan|auditory|dct|matrix|window|fft" --output-on-failure
```

## Configuration and Scripts
### 4.3 Build Target DSP Firmware
```bash
west build -b intel_adsp_ace15_mtpm app
```

### 4.4 Generate Topology Configurations (Octave)
The tuning script generates binary configuration blobs and ALSA topology configurations:
```bash
cd src/audio/mfcc/tune
octave-cli --eval "setup_mfcc"
```
Available presets generated:
- `ceps13_compress_dtx.conf`: 13-bin cepstral features with VAD and DTX silence suppression.
- `mel80_compress.conf`: 80-bin linear Mel spectrogram features.
- `mel80_compress_dtx.conf`: 80-bin Mel spectrogram with DTX gating.
- `mel80_pcan_compress.conf`: 80-bin Mel spectrogram with Google PCAN dynamic AGC normalization.

---

## 5. Configuration Options

- **Kconfig**: Enables the MFCC component (`COMP_MFCC`) which selects necessary math libraries (`MATH_FFT`, `MATH_DCT`, `MATH_16BIT_MEL_FILTERBANK`, etc.). Depends on `COMP_MODULE_ADAPTER`.
- **CMakeLists.txt**: Compiles generic, common, and HIFI implementations (`mfcc_hifi3.c`, `mfcc_hifi4.c`). Provides support for Zephyr loadable extensions (`llext`).
- **mfcc.toml**: Specifies the topology configuration for the MFCC module (UUID, affinity, memory parameters, and pin formats).
- **Topology (.conf)**: Derived from `tools/topology/topology2/include/components/mfcc.conf`, configuring a `mfcc` widget object of type `effect` with UUID `73:a7:10:db:a4:1a:ea:4c:a2:1f:2d:57:a5:c9:82:eb`.
- **`CONFIG_COMP_MFCC`**: Enables the MFCC component and automatically selects required math libraries (`CONFIG_MATH_FFT`, `CONFIG_MATH_AUDITORY`, `CONFIG_MATH_DCT`, `CONFIG_MATH_PCAN`, `CONFIG_MATH_WINDOW`).
- **`CONFIG_COMP_MODULE_ADAPTER`**: Module adapter infrastructure for SOF processing components.
3 changes: 3 additions & 0 deletions src/audio/mfcc/llext/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ sof_llext_build("mfcc"
../mfcc_generic.c
../mfcc_hifi3.c
../mfcc_hifi4.c
../mfcc_vad.c
../mfcc_agc8.c
../mfcc_ipc4.c
LIB openmodules
)
Loading
Loading