This tutorial adapts the IMU inference example for audio keyword spotting. The key differences are replacing the IMU sensor driver with Zephyr’s DMIC (Digital Microphone) API and adjusting the inference pipeline for continuous audio classification.
Reference code: https://github.com/edgeimpulse/ei-zephyr-mic-kws-inference
Reference code: https://github.com/edgeimpulse/ei-zephyr-mic-kws-inference
What You’ll Build
A Zephyr application that:- Captures real-time audio from PDM microphone
- Runs continuous keyword spotting inference
- Displays classification results via serial
- Works on any Zephyr board with PDM microphone support
Prerequisites
- Edge Impulse Zephyr Module workspace: See Edge Impulse Zephyr Module Deployment Guide
- Trained audio model: e.g., the Keyword Spotting tutorial
- Zephyr-supported board with PDM microphone (Nordic Thingy:53, nRF5340 Audio DK, etc.)
- Development tools:
- Zephyr SDK 0.17.4+
- West 1.5.0+
Supported Microphones
PDM microphones accessible through Zephyr’s DMIC (Digital Microphone) driver are compatible:| Board | Microphone | Driver |
|---|---|---|
| Nordic Thingy:53 | PDM | CONFIG_AUDIO_DMIC=y |
| nRF5340 Audio DK | PDM | CONFIG_AUDIO_DMIC=y |
| STM32 boards with DFSDM | PDM | CONFIG_AUDIO_DMIC=y |
| Custom boards | I2S/PDM | See Zephyr Audio Docs |
1. Initialize the Repository
- Zephyr RTOS
- Edge Impulse Zephyr SDK module
- All dependencies
2. Deploy Your Audio Model
In Edge Impulse Studio:- Go to Deployment
- Select Zephyr library
- Click Build
- Download the model
.zip
model/:
model/ contains:
CMakeLists.txtedge-impulse-sdk/model-parameters/tflite-model/
3. Build
Select your board:.west/config:
4. Flash
5. Monitor Output
How It Works
Adapting from IMU to Audio
This example follows the same architecture as the IMU inference tutorial, with these key changes:| Component | IMU Example | Audio Example |
|---|---|---|
| Sensor API | Zephyr Sensor API (sensor.h) | Zephyr DMIC API (audio/dmic.h) |
| Data Source | I²C/SPI accelerometer/gyro | PDM microphone |
| Sample Rate | 100 Hz (typical) | 16 kHz |
| Data Type | 3-axis or 6-axis float | Mono audio int16 |
| Buffer Size | 200-300 samples (2-3s) | 16000 samples (1s) |
| Driver Config | CONFIG_SENSOR=y | CONFIG_AUDIO_DMIC=y |
Code Flow
- Initialize - Set up microphone via Zephyr DMIC API
- Sample - Continuous audio data collection at model frequency
- Buffer - Circular buffer stores audio samples
- Infer - Run classifier when buffer is full
- Output - Print classification results
- Loop - Repeat
Project Structure
Customizing the Example
Adjust Audio Sampling
Inprj.conf:
Change Inference Frequency
Insrc/main.cpp:
Increase Memory for Larger Models
Inprj.conf:
Add Logging
Understanding the Code
Microphone Initialization
Audio Capture
Inference Integration
Main Loop
Device Tree Configuration
For boards without built-in DMIC, add to your.overlay file:
Troubleshooting
Module not found
Module not found
Cause: Edge Impulse SDK not fetchedSolution:
Insufficient memory
Insufficient memory
Cause: Model too large for available RAMSolution: Increase stack size in Or enable EON Compiler when deploying from Studio.
prj.conf:Microphone not detected
Microphone not detected
Cause: DMIC device not configured or pins incorrectSolution: Enable debug logging:Check device tree configuration matches your board’s microphone pins.
Audio quality issues
Audio quality issues
Causes & Solutions:
- Wrong sample rate: Verify
CONFIG_AUDIO_SAMPLE_RATE_16000=ymatches your model - Buffer underrun: Increase buffer size:
- Clock configuration: Check PDM clock frequency in device tree
Poor classification accuracy
Poor classification accuracy
Causes & Solutions:
- Background noise: Train model with noise samples
- Microphone gain: Adjust in device tree:
- Sample rate mismatch: Ensure DMIC sample rate matches training data
Using in Your Own Project
Option 1: Add to Existing Zephyr Project
Update yourwest.yml:
CMakeLists.txt:
Option 2: Clone This Repository
Advanced Features
Voice Activity Detection (VAD)
Only run inference when speech is detected:Continuous Sliding Window
Wake Word Detection
Performance Optimization
Reduce Power Consumption
Use DMA for Audio Transfer
Optimize for Size
Next Steps
Additional Resources
- GitHub: ei-zephyr-mic-kws-inference
- Zephyr Audio Documentation
- Keyword Spotting Tutorial
- Edge Impulse Forum
Summary
You now have microphone-based keyword spotting running on Zephyr! The Edge Impulse Zephyr Module handles:- Audio capture from PDM microphones
- Continuous inference pipeline
- Model integration
- Memory management