Wakeword and Keyword Spotting

This application demonstrates wakeword detection and keyword spotting from a digital microphone stream using nRF Edge AI Library and Axon-based inference. It also features integrated model observability, allowing you to monitor and analyze inference metrics during operation.

Application overview

The application samples single-channel 16 kHz audio from a PDM microphone and feeds it to nRF Edge AI models. The wakeword phrase used by the bundled model is “Okay Nordic”. In wakeword detection stage, model output is postprocessed with a predictions history window. Parameters used for postprocessing are prediction probability threshold, predictions history length and number of predictions above threshold in predictions history. When a wakeword is detected, the application switches to keyword detection stage.

The bundled keyword spotting model supports the following keywords:

  • Go

  • Stop

  • Up

  • Down

  • Yes

  • No

  • On

  • Off

  • Right

  • Left

In the keyword spotting stage, the application smooths class probabilities using an exponential moving average and reports a detection when class-specific criteria are met. After period without any keywords spotted the application switches back to wakeword stage.

You can also configure the application to stay in one of these stages using the application-specific Kconfig options.

Observability

The application collects runtime observability metrics using the nRF Edge AI Observability Library.

Enable metrics for each model independently (either option, or both):

  • CONFIG_MODELS_OBSERVABILITY_WW - Wakeword model

  • CONFIG_MODELS_OBSERVABILITY_KWS - Keyword spotting model

Each model has its own observability file: src/ww/ww_obsv.c and src/kws/kws_obsv.c. Each one creates the observability context, sizes and registers the metrics selected in observability.conf for its own class and feature counts, and binds the Memfault transport. The model files (src/ww/wakeword.c and src/kws/kws.c) only feed samples to it. Use either file as the starting point for observing your own model.

The wakeword model emits a single probability p. The probability metrics are defined over a distribution, so the score is expanded into a two-class [1 - p, p] vector inside src/ww/ww_obsv.c.

Metrics are collected periodically and sent to Memfault as Custom Data Recording. You can change the interval using the CONFIG_NRF_EDGEAI_OBSV_MEMFAULT_AUTO_COLLECT_INTERVAL_SEC Kconfig option. Memfault accepts only one Custom Data Recording per device per day, unless the device is in Developer Mode.

The data is sent using one of the following transports:

  • Bluetooth LE - The device exposes the Memfault Diagnostic Service (MDS). A gateway, such as a mobile phone or a computer with a web browser, connects to the device and forwards the data to Memfault.

  • Wi-Fi (nRF7120 DK only) - The device connects to a Wi-Fi access point and uploads the data directly to Memfault over HTTPS, without a gateway.

You can select the transport in your configuration file. Each transport configuration file sets its option of the MODELS_OBSERVABILITY_TRANSPORT Kconfig choice. For details, see Configuration files.

The Wi-Fi connection handling is implemented in src/wifi/memfault_http_obsv.c and is based on the Memfault sample.

Replacing models

You can replace the bundled models using the Text to Wake Word Detection feature of the Nordic Edge AI Lab or one of ready-to-use models.

To replace the model used in wakeword detection stage complete the following steps:

  1. Replace the files inside src/ww/nrf_edgeai_generated directory with files downloaded from Nordic Edge AI Lab.

  2. In ww_init(), set the ww_model pointer using the model getter from the generated files.

  3. Adjust the Kconfig options to tune wakeword detection postprocessing to your model.

Requirements

The application supports the following development kits:

Hardware platforms

PCA number

Board name

Board target

nRF7120 DK

PCA10217

nrf7120dk

nrf7120dk/nrf7120e/cpuapp

nRF54LM20 DK

PCA10184

nrf54lm20dk

nrf54lm20dk/nrf54lm20b/cpuapp

The application also requires PDM digital microphone connected to the pins specified in the board overlay file (boards/nrf54lm20dk_nrf54lm20b_cpuapp.overlay or boards/nrf7120dk_nrf7120e_cpuapp.overlay). The application is expecting audio data to be provided on left channel.

Pin mapping

The application was tested with an Adafruit PDM MEMS Microphone module. It can be powered from 1.8V VDD:IO supply. The following table show how to connect this module to the DK:

Adafruit DMIC

nRF54LM20 DK

nRF7120 DK

3V

VDD:IO

VDD:IO

GND

GND

GND

SEL

GND

GND

CLK

P1.4

P1.4

DAT

P1.5

P1.5

The SEL pin is responsible for selecting audio channel and connecting it to ground selects left channel.

To use other microphones, adapt configuration parameters of PDM in the src/dmic.c file.

User interface

LED0:

Behavior depends on the selected application mode:

  • Lit when keyword spotting stage is active in wakeword-gated keyword spotting mode.

  • Blinks for one second when wakeword is detected in wakeword only mode.

  • Stays off in keyword spotting only mode.

LED1:

Blinks for one second on each keyword spotted.

UART30 (VCOM0) on the nRF54LM20 DK, UART21 on the nRF7120 DK:

Prints runtime state messages:

  • Waiting for wakeword

  • Wakeword detected

  • Waiting for keywords

  • Keyword spotted: <name>

  • Keyword spotting window timeout

Configuration

See Configuring and building for information about how to permanently or temporarily change the configuration.

Configuration options

The following application-specific Kconfig options are used in this application:

CONFIG_APP_MODE_WW_GATED_KWS

(bool) Wakeword-gated keyword spotting mode

Run keyword spotting model only after wakeword was detected. After configurable duration without any keyword spotted switch back to wakeword detection.

CONFIG_APP_MODE_WW_ONLY

(bool) Wakeword detection only mode

Run wakeword detection continuously without switching to keyword spotting.

CONFIG_APP_MODE_KWS_ONLY

(bool) Keyword spotting only mode

Run keyword spotting continuously without the need to detect wakeword first.

CONFIG_WW_PROBABILITY_THRESHOLD

(int) Wakeword probability threshold

Probability threshold for wakeword detection model. The value is expressed in units of 1/1000.

CONFIG_WW_HISTORY_SIZE

(int) Size of wakeword predictions history

Size of history of previous results from wakeword detection model. Each entry corresponds to 30 milliseconds of past audio.

CONFIG_WW_COUNT_THRESHOLD

(int) Threshold for wakeword detection from predictions history

Number of predictions above threshold in predictions history required to confirm wakeword detection.

CONFIG_KWS_PERIOD_MS

(int) Length of keyword spotting window in wakeword-gated mode

CONFIG_KWS_EMA_ALPHA

(int) Exponential moving average coefficient for class probability

Coefficient for new entries in exponential moving average calculation for average class probability. The value is expressed in units of 1/1000.

CONFIG_MODELS_OBSERVABILITY_WW

(bool) Edge AI observability for the wakeword model

Collect observability metrics for the wakeword model. Its single output is expanded into a synthetic 2-class [1 - p, p] distribution so the probability metrics apply to it.

CONFIG_MODELS_OBSERVABILITY_KWS

(bool) Edge AI observability for the keyword spotting model

Collect observability metrics for the keyword spotting model. Enabling it together with MODELS_OBSERVABILITY_WW (wakeword-gated mode runs both models) registers two Memfault contexts, so NRF_EDGEAI_OBSV_MEMFAULT_MAX_CONTEXTS must be >= 2.

CONFIG_MODELS_OBSERVABILITY

(bool)

Auto-selected umbrella: set when observability is enabled for at least one model. Each model has its own observability file (src/ww/ww_obsv.c, src/kws/kws_obsv.c) gated by its own option; this symbol only gates what is shared, such as the observability transport.

CONFIG_MODELS_OBSERVABILITY_MDS

(bool) Bluetooth LE with the Memfault Diagnostic Service

Expose the Memfault Diagnostic Service. A gateway, such as a phone or a web browser, connects to the device and forwards the observability data to Memfault.

CONFIG_MODELS_OBSERVABILITY_MEMFAULT_HTTP

(bool) Direct upload to Memfault over HTTPS

Keep a network (for example Wi-Fi) connection up, and let the Memfault SDK upload the observability data directly to Memfault over HTTPS.

Additional configuration

Check and configure the following library options that are used by the application:

  • CONFIG_MEMFAULT_PROJECT_KEY - A key to your Memfault project.

  • CONFIG_MEMFAULT_NCS_DEVICE_ID_HW_ID - Uses the SoC device ID as the Memfault device ID. Set this option in the observability.conf file.

  • CONFIG_NRF_EDGEAI_OBSV_MAX_CLASSES - Number of classes the observed model predicts.

  • CONFIG_NRF_EDGEAI_OBSV_MEMFAULT_MAX_CONTEXTS - Number of observability contexts, one per observed model.

  • CONFIG_NRF_EDGEAI_OBSV_MEMFAULT_AUTO_COLLECT_INTERVAL_SEC - Interval of automatic metrics collection.

  • CONFIG_MEMFAULT_PERIODIC_UPLOAD_INTERVAL_SECS - Interval of the upload to Memfault with the Wi-Fi transport.

Build types

For build type overview and guidance on how to run them, see Custom build types and Providing CMake options. The application supports the following build types:

Wakeword and Keyword Spotting build types

Build type

File name

Description

Debug (default)

prj.conf

Debug version of the application with logging and assertions enabled.

Release

prj_release.conf

Release version of the application with logging disabled and compiler optimizations.

Configuration files

Enable observability by combining observability.conf with exactly one transport configuration:

Wakeword and Keyword Spotting observability configuration files

File name

Description

observability.conf

Enables observability for both bundled models (CONFIG_MODELS_OBSERVABILITY_WW and CONFIG_MODELS_OBSERVABILITY_KWS), the metrics, and the Memfault SDK. It does not enable any transport.

observability_ble.conf

Bluetooth LE transport. Enables the Memfault Diagnostic Service and bonding.

observability_wifi.conf

Wi-Fi transport (nRF7120 DK only). Enables the Wi-Fi station, TLS, and direct upload to Memfault over HTTPS. It also enables the shell for provisioning Wi-Fi credentials, also in the release build type.

observability_wifi.overlay

Devicetree overlay required by the Wi-Fi transport. Reserves the Wi-Fi ROM patch regions and increases the DMIC queue size. Because the resulting image exceeds 1 MB, the model data is placed above the patch regions.

To observe only one model, disable the other model option in observability.conf. The file also sets the memory sizing for the enabled scope: CONFIG_NRF_EDGEAI_OBSV_MAX_CLASSES, CONFIG_NRF_EDGEAI_OBSV_MEMFAULT_MAX_CONTEXTS and the shared CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE.

Include the configuration files using the EXTRA_CONF_FILE variable. For the Wi-Fi transport, also include the overlay using the EXTRA_DTC_OVERLAY_FILE variable. See Providing CMake options for details.

west build -b nrf54lm20dk/nrf54lm20b/cpuapp -- -DEXTRA_CONF_FILE="observability.conf;observability_ble.conf"

Note

Set the CONFIG_MEMFAULT_PROJECT_KEY Kconfig option to your Memfault project key to successfully send metrics to Memfault.

Building and running

This application can be found under applications/ww_kws in the Edge AI Add-on folder structure.

To build the application, follow the instructions in Building an application for your preferred building environment. See also Programming an application for programming steps and Testing and optimization for general information about testing and debugging in the nRF Connect SDK.

Testing

After programming the application to your development kit, complete the following steps to test it:

  1. Connect the kit to the computer using a USB cable. The kit is assigned a serial port. Serial ports are referred to as COM ports on Windows, /dev/ttyACM devices on Linux, and /dev/tty devices on macOS. To list Nordic Semiconductor devices connected to your computer together with their serial ports, open a terminal and run the nrfutil device list command. Alternatively, check your operating system’s device manager or its equivalent.

  2. Connect to the kit with a terminal emulator (for example, the Serial Terminal app). See Testing and optimization for the required settings and steps. The application is using both serial ports.

  3. Open one terminal for Zephyr logs and one for control output from UART30 (nRF54LM20 DK) or UART21 (nRF7120 DK).

  4. Say the wakeword phrase “Okay Nordic”.

  5. Observe LED0 light up.

  6. Say one of the bundled model keywords (for example, “Yes” or “No”).

  7. Observe that LED1 blinks for one second.

  8. Stop speaking and wait for timeout.

If you have enabled the model observability, also complete the following steps:

  1. Connect the device to Memfault using the selected transport:

    Connect gateway to your device, for example, nRF Connect Device Manager or Memfault WebBluetooth Client. See the Testing section of Bluetooth: Peripheral Memfault Diagnostic Service (MDS) for details.

  2. In Memfault, enable Developer Mode for the device.

  3. Wait for the metrics to be collected (CONFIG_NRF_EDGEAI_OBSV_MEMFAULT_AUTO_COLLECT_INTERVAL_SEC). With the Wi-Fi transport, also wait for the next upload (CONFIG_MEMFAULT_PERIODIC_UPLOAD_INTERVAL_SECS).

    Note

    Without Developer Mode, Memfault silently discards Custom Data Recordings above its limit of one per device per day. The firmware does not report an error.

  4. In a web browser, navigate to Memfault.

  5. In the left-hand menu, go to CDR Payloads.

  6. Select a CDR payload from your test device with “edgeai_observability” as reason. You can filter the list of CDR payloads by device name and time.

  7. Download the payload for metrics analysis.

  8. Decode it on a host PC using scripts/decode_edgeai_obsv_cdr/decode_edgeai_obsv_cdr.py script in binary mode:

    ./scripts/decode_edgeai_obsv_cdr/decode_edgeai_obsv_cdr.py --binary --file <downloaded>.bin
    

    See Decoding CDR payloads for installation and other input modes. Payload format is defined in the lib/nrf_edgeai_obsv/obsv.cddl file.

Application output

The application shows the following output from UART30 (VCOM0) on the nRF54LM20 DK, or from UART21 on the nRF7120 DK:

Waiting for wakeword
Wakeword detected
Waiting for keywords
Keyword spotted: Yes
Keyword spotting window timeout

Dependencies

This application uses the following Edge AI Add-on library:

This application uses the following Zephyr libraries:

API documentation

Wakeword and Keyword Spotting application