Skip to content
Open
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
3 changes: 3 additions & 0 deletions scripts/host-testbench.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
# stop on most errors
set -e

# Prevent snap or desktop environment from breaking octave
unset GTK_PATH

function filesize() {
du -b "$1" | awk '{print $1}'
}
Expand Down
4 changes: 4 additions & 0 deletions src/include/sof/math/auditory.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,8 @@ void psy_apply_mel_filterbank_16(struct psy_mel_filterbank *mel_fb, struct icomp
void psy_apply_mel_filterbank_32(struct psy_mel_filterbank *mel_fb, struct icomplex32 *fft_out,
int32_t *power_spectra, int32_t *mel_log, int bitshift);

void psy_apply_mel_filterbank_with_linear_32(struct psy_mel_filterbank *mel_fb, struct icomplex32 *fft_out,
int32_t *power_spectra, int32_t *mel_log,
uint32_t *mel_linear, int bitshift);

#endif /* __SOF_MATH_AUDITORY_H__ */
166 changes: 166 additions & 0 deletions src/include/sof/math/pcan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright(c) 2026 Intel Corporation. All rights reserved.
*
* Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
*/

/**
* \file include/sof/math/pcan.h
* \brief Per-Channel AGC Normalization (PCAN) library interface
*
* Wraps and integrates Google's upstream TFLite Micro microfrontend
* library (under Apache-2.0 with patent grant protection).
*/

#ifndef __SOF_MATH_PCAN_H__
#define __SOF_MATH_PCAN_H__

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#include "tensorflow/lite/experimental/microfrontend/lib/bits.h"
#include "tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control.h"
#include "tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control_util.h"
#include "tensorflow/lite/experimental/microfrontend/lib/noise_reduction.h"
#include "tensorflow/lite/experimental/microfrontend/lib/noise_reduction_util.h"
#include "tensorflow/lite/experimental/microfrontend/lib/log_scale.h"

#define PCAN_SNR_BITS kPcanSnrBits /* 12 */
#define PCAN_OUTPUT_BITS kPcanOutputBits /* 6 */
#define PCAN_SMOOTHING_COEF_BITS kNoiseReductionBits /* 14 */
#define PCAN_WIDE_DYNAMIC_BITS kWideDynamicFunctionBits /* 32 */
#define PCAN_LUT_SIZE kWideDynamicFunctionLUTSize /* 125 */

/* Google derives this as log2(fft_size) - 1 - kFilterbankBits / 2 because its
* fixed-point FFT keeps a 1/N scale. SOF compensates the FFT length in the Mel
* filter bank and scales mel_linear to Google's 512-point range, so it is 3 for
* every FFT size.
*/
#define PCAN_INPUT_CORRECTION_BITS 3

/**
* \brief SOF PCAN configuration structure.
*/
struct pcan_config {
int32_t strength; /**< Exponent alpha in Q1.15 (e.g. 31130 for 0.95) */
int32_t offset; /**< Additive offset delta in Q8.7 (e.g. 10240 for 80.0) */
int32_t gain_bits; /**< Gain bit shift (e.g. 21) */
uint16_t even_smoothing; /**< Even-channel IIR smoothing in Q14 (e.g. 410 for 0.025) */
uint16_t odd_smoothing; /**< Odd-channel IIR smoothing in Q14 (e.g. 983 for 0.06) */
uint16_t min_signal_remaining; /**< Minimum signal floor in Q14 (e.g. 819 for 0.05) */
uint16_t smoothing_bits; /**< Bit shift for noise estimate (e.g. 10) */
int32_t input_correction_bits; /**< Input correction bits shift (e.g. 3) */
bool enable_pcan; /**< Enable flag */
};

/**
* \brief SOF PCAN runtime state wrapper.
*/
struct pcan_state {
struct PcanGainControlState g_pcan; /**< Google upstream PCAN state */
struct NoiseReductionState g_noise; /**< Google upstream Noise Reduction state */
struct LogScaleState g_log_scale; /**< Google upstream LogScale state */
uint32_t *noise_estimate; /**< Pointer to noise estimate buffer */
int16_t *gain_lut; /**< Pointer to gain LUT */
int num_channels; /**< Number of channels */
int32_t snr_shift; /**< SNR bit shift */
int32_t input_correction_bits; /**< Input correction bits for log scale */
uint16_t smoothing_bits; /**< Smoothing bits */
bool enable_pcan; /**< PCAN enabled */
bool allocated_noise; /**< Internally allocated noise buffer */
bool allocated_lut; /**< Internally allocated gain LUT buffer */
};

/**
* \brief Evaluate Google's WideDynamicFunction.
*/
static inline int16_t pcan_wide_dynamic_function(uint32_t x, const int16_t *lut)
{
return WideDynamicFunction(x, lut);
}

/**
* \brief Evaluate Google's PcanShrink.
*/
static inline uint32_t pcan_shrink(uint32_t x)
{
return PcanShrink(x);
}

/**
* \brief Compute single point in continuous PCAN gain curve.
*
* \param strength Exponent alpha in Q1.15.
* \param offset Additive offset delta in Q8.7.
* \param gain_bits Fractional bit count for the gain output.
* \param input_bits Bit shift applied to input energy.
* \param x Input noise estimate value.
*/
int16_t pcan_gain_lookup_function(int32_t strength, int32_t offset, int32_t gain_bits,
int32_t input_bits, uint32_t x);

/**
* \brief Fill 125-entry gain lookup table for WideDynamicFunction.
*
* \param strength Exponent alpha in Q1.15.
* \param offset Additive offset delta in Q8.7.
* \param gain_bits Fractional bit count for the gain output.
* \param input_bits Bit shift applied to input energy.
* \param gain_lut Output lookup table buffer of size PCAN_LUT_SIZE.
*/
int pcan_compute_lut(int32_t strength, int32_t offset, int32_t gain_bits,
int32_t input_bits, int16_t *gain_lut);

/**
* \brief Initialize PCAN state with caller-supplied buffers.
*
* On host/unit-test builds (`CONFIG_LIBRARY` or `UNIT_TEST`), `NULL` buffer
* arguments fall back to internal heap allocation freed by pcan_free_state().
* On firmware builds, caller-supplied buffers are required.
*
* \param config PCAN and noise reduction configuration.
* \param state PCAN runtime state to initialize.
* \param noise_estimate_buffer Buffer of size num_channels uint32_t elements.
* \param gain_lut_buffer Buffer of size PCAN_LUT_SIZE int16_t elements.
* \param num_channels Number of filterbank channels.
*/
int pcan_populate_state(const struct pcan_config *config, struct pcan_state *state,
uint32_t *noise_estimate_buffer, int16_t *gain_lut_buffer,
int num_channels);

/**
* \brief Free allocated resources in PCAN state.
*/
void pcan_free_state(struct pcan_state *state);

/**
* \brief Reset PCAN temporal state.
*/
void pcan_reset(struct pcan_state *state);

/**
* \brief Apply noise reduction and update noise estimate using Google's NoiseReductionApply.
*/
static inline void pcan_noise_reduction(struct pcan_state *state, uint32_t *signal)
{
if (state && state->enable_pcan)
NoiseReductionApply(&state->g_noise, signal);
}

/**
* \brief Apply PCAN gain control and compression using Google's PcanGainControlApply.
*/
static inline void pcan_apply(struct pcan_state *state, uint32_t *signal)
{
if (state && state->enable_pcan)
PcanGainControlApply(&state->g_pcan, signal);
}

/**
* \brief Apply Google's fixed-point logarithm and scale to signal in-place.
*/
void pcan_log_scale(struct pcan_state *state, uint32_t *signal);

#endif /* __SOF_MATH_PCAN_H__ */
4 changes: 4 additions & 0 deletions src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ if(CONFIG_MATH_AUDITORY)
add_subdirectory(auditory)
endif()

if(CONFIG_MATH_PCAN)
add_subdirectory(pcan)
endif()

if(CONFIG_MATH_DCT)
list(APPEND base_files dct.c)
endif()
Expand Down
12 changes: 12 additions & 0 deletions src/math/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ config MATH_16BIT_MEL_FILTERBANK
config MATH_32BIT_MEL_FILTERBANK
bool "icomplex32 data support"
default n
select SQRT_FIXED
help
This option enables function psy_apply_mel_filterbank_32().
It takes as input an array of icomplex32
Expand All @@ -304,6 +305,17 @@ config MATH_32BIT_MEL_FILTERBANK

endmenu

config MATH_PCAN
bool "PCAN (Per-Channel AGC Normalization) library"
default n
select BINARY_LOGARITHM_FIXED
select MATH_EXP
select NATURAL_LOGARITHM_FIXED
help
Select this to build PCAN (Per-Channel Automatic Gain Control Normalization)
library. PCAN performs dynamic per-channel gain control and piecewise polynomial
root compression across time to suppress stationary noise and enhance acoustic transients.

config MATH_DCT
bool "DCT transform library"
default n
Expand Down
81 changes: 67 additions & 14 deletions src/math/auditory/mel_filterbank_32.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,23 @@
#include <sof/math/icomplex32.h>
#include <sof/math/log.h>
#include <sof/math/numbers.h>
#include <sof/math/sqrt.h>
#include <stdint.h>

void psy_apply_mel_filterbank_32(struct psy_mel_filterbank *fb, struct icomplex32 *fft_out,
int32_t *power_spectra, int32_t *mel_log, int bitshift)
static inline uint32_t mel_sqrt32(uint32_t num)
{
if (num == 0)
return 0;

/* sofm_sqrt_int32 treats input as Q2.30, returning sqrt(n)*2^15.
* Scale down by 2^15 with rounding to obtain integer sqrt(num).
*/
return (uint32_t)((sofm_sqrt_int32((int32_t)num) + (1 << 14)) >> 15);
}
Comment on lines +15 to +24

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking (sof.unit.math.auditory CI link failure + Kconfig dependency):

  1. Adding sofm_sqrt_int32() breaks sof.unit.math.auditory in ZTest CI (undefined reference to 'sofm_sqrt_int32') — add ${SOF_ROOT}/src/math/sqrt_int32.c to test/ztest/unit/math/auditory/CMakeLists.txt.
  2. In src/math/Kconfig, config MATH_32BIT_MEL_FILTERBANK (line 300) should select SQRT_FIXED so firmware configs enabling 32-bit Mel filterbank always build src/math/sqrt_int32.c.


void psy_apply_mel_filterbank_with_linear_32(struct psy_mel_filterbank *fb, struct icomplex32 *fft_out,
int32_t *power_spectra, int32_t *mel_log,
uint32_t *mel_linear, int bitshift)
{
int64_t pmax;
int64_t p;
Expand Down Expand Up @@ -67,20 +80,60 @@ void psy_apply_mel_filterbank_32(struct psy_mel_filterbank *fb, struct icomplex3
*/
log_arg = sat_int32(Q_SHIFT_RND(p, 45, 25));
log_arg = MAX(log_arg, AUDITORY_EPS_Q31);
log = base2_logarithm((uint32_t)log_arg);
log -= AUDITORY_LOG2_2P25_Q16;

/* Compensate Mel triangles scale */
log += fb->scale_log2;
if (mel_linear) {
/* Compensate dynamic lshift and FFT bitshift so mel_linear reflects
* true acoustic magnitude calibrated to Google microfrontend range.
*/
uint32_t s = mel_sqrt32((uint32_t)log_arg);
/* Total power shift applied was: lshift + 2 * bitshift */
int neg_shift = -((int32_t)lshift + 2 * bitshift);
int int_shift = neg_shift >> 1;
int frac_shift = neg_shift & 1;

/* Subtract the applied lshift for power spectra
* log2(x * 2^(-n)) = log2(x) - n. Note that the bitshift need to be subtracted
* as doubled because it was applied in linear domain, from log(x * 2^(-2 * n))
*/
log -= ((int32_t)lshift + 2 * bitshift) << 16;
uint64_t s_comp = s;
if (frac_shift)
s_comp = (s_comp * 46341U) >> 15; /* 46341 / 32768 ~= sqrt(2) */

if (int_shift >= 32)
s_comp = UINT32_MAX;
else if (int_shift > 0)
s_comp <<= int_shift;
else if (int_shift <= -64)
s_comp = 0;
else if (int_shift < 0)
s_comp >>= -int_shift;

/* Scale to Google microfrontend range: 25826 / 32768 ~= 0.788 */
s_comp = (s_comp * 25826U) >> 15;
if (s_comp > UINT32_MAX)
s_comp = UINT32_MAX;

/* Scale for desired log, output as Q9.23 */
log = Q_MULTSR_32X32((int64_t)log, fb->log_mult, 16, 29, 23);
mel_log[i] = log; /* Q9.23 */
mel_linear[i] = (uint32_t)s_comp;
}

if (mel_log) {
log = base2_logarithm((uint32_t)log_arg);
log -= AUDITORY_LOG2_2P25_Q16;

/* Compensate Mel triangles scale */
log += fb->scale_log2;

/* Subtract the applied lshift for power spectra
* log2(x * 2^(-n)) = log2(x) - n. Note that the bitshift need to be subtracted
* as doubled because it was applied in linear domain, from log(x * 2^(-2 * n))
*/
log -= ((int32_t)lshift + 2 * bitshift) << 16;

/* Scale for desired log, output as Q9.23 */
log = Q_MULTSR_32X32((int64_t)log, fb->log_mult, 16, 29, 23);
mel_log[i] = log; /* Q9.23 */
}
}
}

void psy_apply_mel_filterbank_32(struct psy_mel_filterbank *fb, struct icomplex32 *fft_out,
int32_t *power_spectra, int32_t *mel_log, int bitshift)
{
psy_apply_mel_filterbank_with_linear_32(fb, fft_out, power_spectra, mel_log, NULL, bitshift);
}
41 changes: 41 additions & 0 deletions src/math/pcan/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# SPDX-License-Identifier: BSD-3-Clause

if(NOT DEFINED TFLM_PATH)
if(DEFINED WEST_TOPDIR AND EXISTS "${WEST_TOPDIR}/tflite-micro")
set(TFLM_PATH "${WEST_TOPDIR}/tflite-micro")
elseif(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 "${PROJECT_SOURCE_DIR}/../tflite-micro")
set(TFLM_PATH "${PROJECT_SOURCE_DIR}/../tflite-micro")
elseif(EXISTS "${CMAKE_CURRENT_LIST_DIR}/../../../../tflite-micro")
set(TFLM_PATH "${CMAKE_CURRENT_LIST_DIR}/../../../../tflite-micro")
endif()
endif()

set(MICROFRONTEND_DIR ${TFLM_PATH}/tensorflow/lite/experimental/microfrontend/lib)

set(base_files
pcan.c
${MICROFRONTEND_DIR}/pcan_gain_control.c
${MICROFRONTEND_DIR}/noise_reduction.c
${MICROFRONTEND_DIR}/noise_reduction_util.c
${MICROFRONTEND_DIR}/log_scale.c
${MICROFRONTEND_DIR}/log_lut.c
)

is_zephyr(zephyr)
if(zephyr) ### Zephyr ###

zephyr_include_directories(${TFLM_PATH})
zephyr_library_sources(
${base_files}
)

else() ### library, e.g. testbench or plugin ###

target_include_directories(sof PRIVATE ${TFLM_PATH})
add_local_sources(sof ${base_files})

endif()
Loading
Loading