Repository navigation
Expand file tree
/
Copy pathpcan.h
More file actions
166 lines (147 loc) · 5.91 KB
/
Copy pathpcan.h
File metadata and controls
166 lines (147 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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__ */