Skip to content
Draft
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
2 changes: 2 additions & 0 deletions app/ultrasound.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_COMP_ULTRASOUND_TX=m
CONFIG_COMP_ULTRASOUND_RX=m
4 changes: 4 additions & 0 deletions src/audio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,7 @@ endif()
if(CONFIG_COMP_BT_CODEC)
add_subdirectory(bt_codec)
endif()

if(CONFIG_COMP_ULTRASOUND_TX OR CONFIG_COMP_ULTRASOUND_RX)
add_subdirectory(ultrasound)
endif()
1 change: 1 addition & 0 deletions src/audio/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -453,5 +453,6 @@ config DAI_VERBOSE_GLITCH_WARNINGS
DAI DMA operation and issues with low-latency scheduling.

rsource "bt_codec/Kconfig"
rsource "ultrasound/Kconfig"

endmenu
15 changes: 15 additions & 0 deletions src/audio/ultrasound/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SPDX-License-Identifier: BSD-3-Clause

if(CONFIG_COMP_ULTRASOUND_TX STREQUAL "m" AND DEFINED CONFIG_LLEXT)
add_subdirectory(llext_tx ${PROJECT_BINARY_DIR}/ultrasound_tx_llext)
add_dependencies(app ultrasound_tx)
elseif(CONFIG_COMP_ULTRASOUND_TX)
add_local_sources(sof ultrasound_tx.c)
endif()

if(CONFIG_COMP_ULTRASOUND_RX STREQUAL "m" AND DEFINED CONFIG_LLEXT)
add_subdirectory(llext_rx ${PROJECT_BINARY_DIR}/ultrasound_rx_llext)
add_dependencies(app ultrasound_rx)
elseif(CONFIG_COMP_ULTRASOUND_RX)
add_local_sources(sof ultrasound_rx.c)
endif()
19 changes: 19 additions & 0 deletions src/audio/ultrasound/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-License-Identifier: BSD-3-Clause

config COMP_ULTRASOUND_TX
tristate "Ultrasonic carrier generator (TX) component"
default m if LIBRARY_DEFAULT_MODULAR
default y
help
Select for ultrasonic carrier generator component. Injects an inaudible
ultrasonic tone (>= 19 kHz) with smooth ramping onto an audio stream
for acoustic gesture and presence sensing.

config COMP_ULTRASOUND_RX
tristate "Ultrasonic Doppler gesture and presence detector (RX) component"
default m if LIBRARY_DEFAULT_MODULAR
default y
help
Select for ultrasonic Doppler gesture and presence detector component.
Demodulates ultrasonic carrier sidebands, classifies push/pull/tap/swipe
gestures and user presence, and emits IPC4 module notifications.
6 changes: 6 additions & 0 deletions src/audio/ultrasound/llext_rx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) 2026 Intel Corporation.
# SPDX-License-Identifier: Apache-2.0

sof_llext_build("ultrasound_rx"
SOURCES ../ultrasound_rx.c
)
6 changes: 6 additions & 0 deletions src/audio/ultrasound/llext_rx/llext.toml.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <tools/rimage/config/platform.toml>
#define LOAD_TYPE "2"
#include "../ultrasound_rx.toml"

[module]
count = __COUNTER__
6 changes: 6 additions & 0 deletions src/audio/ultrasound/llext_tx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) 2026 Intel Corporation.
# SPDX-License-Identifier: Apache-2.0

sof_llext_build("ultrasound_tx"
SOURCES ../ultrasound_tx.c
)
6 changes: 6 additions & 0 deletions src/audio/ultrasound/llext_tx/llext.toml.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <tools/rimage/config/platform.toml>
#define LOAD_TYPE "2"
#include "../ultrasound_tx.toml"

[module]
count = __COUNTER__
201 changes: 201 additions & 0 deletions src/audio/ultrasound/ultrasound_dsp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright(c) 2026 Intel Corporation.
*
* Fixed-point DSP primitives for SOF ultrasonic gesture/presence sensing.
*/

#ifndef __SOF_AUDIO_ULTRASOUND_DSP_H__
#define __SOF_AUDIO_ULTRASOUND_DSP_H__

#include <stdint.h>

#define US_FFT_SIZE 64
#define US_FFT_LOG2 6
#define US_BASEBAND_RATE_HZ 1000
#define US_SPEED_OF_SOUND_M_S 343

/* 257-entry full-wave sine LUT in Q1.15 */
static const int16_t us_sin_lut[257] = {
0, 804, 1608, 2410, 3212, 4011, 4808, 5602,
6393, 7179, 7962, 8739, 9512, 10278, 11039, 11793,
12539, 13279, 14010, 14732, 15446, 16151, 16846, 17530,
18204, 18868, 19519, 20159, 20787, 21403, 22005, 22594,
23170, 23731, 24279, 24811, 25329, 25832, 26319, 26790,
27245, 27683, 28105, 28510, 28898, 29268, 29621, 29956,
30273, 30571, 30852, 31113, 31356, 31580, 31785, 31971,
32137, 32285, 32412, 32521, 32609, 32678, 32728, 32757,
32767, 32757, 32728, 32678, 32609, 32521, 32412, 32285,
32137, 31971, 31785, 31580, 31356, 31113, 30852, 30571,
30273, 29956, 29621, 29268, 28898, 28510, 28105, 27683,
27245, 26790, 26319, 25832, 25329, 24811, 24279, 23731,
23170, 22594, 22005, 21403, 20787, 20159, 19519, 18868,
18204, 17530, 16846, 16151, 15446, 14732, 14010, 13279,
12539, 11793, 11039, 10278, 9512, 8739, 7962, 7179,
6393, 5602, 4808, 4011, 3212, 2410, 1608, 804,
0, -804, -1608, -2410, -3212, -4011, -4808, -5602,
-6393, -7179, -7962, -8739, -9512, -10278, -11039, -11793,
-12539, -13279, -14010, -14732, -15446, -16151, -16846, -17530,
-18204, -18868, -19519, -20159, -20787, -21403, -22005, -22594,
-23170, -23731, -24279, -24811, -25329, -25832, -26319, -26790,
-27245, -27683, -28105, -28510, -28898, -29268, -29621, -29956,
-30273, -30571, -30852, -31113, -31356, -31580, -31785, -31971,
-32137, -32285, -32412, -32521, -32609, -32678, -32728, -32757,
-32767, -32757, -32728, -32678, -32609, -32521, -32412, -32285,
-32137, -31971, -31785, -31580, -31356, -31113, -30852, -30571,
-30273, -29956, -29621, -29268, -28898, -28510, -28105, -27683,
-27245, -26790, -26319, -25832, -25329, -24811, -24279, -23731,
-23170, -22594, -22005, -21403, -20787, -20159, -19519, -18868,
-18204, -17530, -16846, -16151, -15446, -14732, -14010, -13279,
-12539, -11793, -11039, -10278, -9512, -8739, -7962, -7179,
-6393, -5602, -4808, -4011, -3212, -2410, -1608, -804,
0,
};

/* 64-point Hann window in Q1.15 */
static const int16_t us_hann64[US_FFT_SIZE] = {
0, 81, 325, 728, 1286, 1995, 2847, 3833,
4944, 6169, 7495, 8909, 10398, 11946, 13539, 15159,
16792, 18421, 20029, 21601, 23122, 24575, 25947, 27224,
28393, 29443, 30363, 31145, 31779, 32260, 32584, 32747,
32747, 32584, 32260, 31779, 31145, 30363, 29443, 28393,
27224, 25947, 24575, 23122, 21601, 20029, 18421, 16792,
15159, 13539, 11946, 10398, 8909, 7495, 6169, 4944,
3833, 2847, 1995, 1286, 728, 325, 81, 0,
};

/* Linearly interpolated Q1.15 sine from 32-bit phase [0, 2*pi) */
static inline int32_t us_sin_q15(uint32_t phase)
{
uint32_t idx = phase >> 24;
int32_t frac = (int32_t)((phase >> 9) & 0x7FFFU);
int32_t y0 = us_sin_lut[idx];
int32_t y1 = us_sin_lut[idx + 1];

return y0 + (((y1 - y0) * frac) >> 15);
}

static inline int32_t us_cos_q15(uint32_t phase)
{
return us_sin_q15(phase + 0x40000000U);
}

static inline uint32_t us_phase_inc(uint32_t freq_hz, uint32_t sample_rate)
{
if (!sample_rate)
return 0;

return (uint32_t)((((uint64_t)freq_hz << 32) + (sample_rate >> 1)) / sample_rate);
}

/* Exact 64-bit integer square root */
static inline uint32_t us_isqrt64(uint64_t n)
{
uint64_t x = n;
uint64_t y;

if (n == 0)
return 0;

y = (x + 1) >> 1;
while (y < x) {
x = y;
y = (x + n / x) >> 1;
}

return (uint32_t)x;
}

/* Q24.8 approximation of log2(x) for x >= 1 */
static inline int32_t us_log2_q8(uint32_t x)
{
int32_t msb;
uint32_t frac;

if (x <= 1)
return 0;

msb = 31 - __builtin_clz(x);
if (msb >= 8)
frac = (x >> (msb - 8)) & 0xFFU;
else
frac = (x << (8 - msb)) & 0xFFU;

/* Small quadratic concavity correction for log2(1+f): +f*(1-f)*0.34 */
frac += ((frac * (256U - frac)) * 87U) >> 16;

return (msb << 8) + (int32_t)frac;
}

/* 20 * log10(peak / floor) in Q24.8 dB: 20/ln(10)*ln(2) = 6.0206 = 1541 / 256 */
static inline uint32_t us_snr_db_q8(uint32_t peak, uint32_t noise_floor)
{
int32_t diff_log2;

if (noise_floor < 1)
noise_floor = 1;
if (peak <= noise_floor)
return 0;

diff_log2 = us_log2_q8(peak) - us_log2_q8(noise_floor);
if (diff_log2 <= 0)
return 0;

return (uint32_t)(((int64_t)diff_log2 * 1541LL) >> 8);
}

/* In-place 64-point radix-2 forward complex FFT (unscaled, int32_t safe up to |x|<=2^22) */
static inline void us_fft64(int32_t *re, int32_t *im)
{
int i, j, len, half;

/* Bit-reversal permutation */
for (i = 1, j = 0; i < US_FFT_SIZE; i++) {
int bit = US_FFT_SIZE >> 1;

for (; j & bit; bit >>= 1)
j ^= bit;
j ^= bit;

if (i < j) {
int32_t tr = re[i];
int32_t ti = im[i];

re[i] = re[j];
im[i] = im[j];
re[j] = tr;
im[j] = ti;
}
}

/* Cooley-Tukey stages: W_len^k = exp(-j * 2*pi * k / len) */
for (len = 2; len <= US_FFT_SIZE; len <<= 1) {
uint32_t step = 0x100000000ULL / (uint32_t)len;

half = len >> 1;
for (i = 0; i < US_FFT_SIZE; i += len) {
uint32_t angle = 0;

for (j = 0; j < half; j++) {
int32_t wr = us_cos_q15(angle);
int32_t wi = -us_sin_q15(angle);
int idx1 = i + j;
int idx2 = idx1 + half;
int32_t tr = (int32_t)(((int64_t)re[idx2] * wr -
(int64_t)im[idx2] * wi) >> 15);
int32_t ti = (int32_t)(((int64_t)re[idx2] * wi +
(int64_t)im[idx2] * wr) >> 15);

re[idx2] = re[idx1] - tr;
im[idx2] = im[idx1] - ti;
re[idx1] = re[idx1] + tr;
im[idx1] = im[idx1] + ti;

angle += step;
}
}
}
}

#endif /* __SOF_AUDIO_ULTRASOUND_DSP_H__ */

Loading
Loading