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
1 change: 1 addition & 0 deletions src/include/sof/math/auditory.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <sof/math/log.h>
#include <stdint.h>

#define AUDITORY_MAX_MEL_BANDS 256
#define AUDITORY_EPS_Q31 1 /* Smallest nonzero Q1.31 value */
#define AUDITORY_LOG2_2P25_Q16 Q_CONVERT_FLOAT(25.0, 16) /* log2(2^25) */

Expand Down
29 changes: 29 additions & 0 deletions src/math/auditory/auditory.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
#include <sof/math/fft.h>
#include <sof/math/log.h>
#include <sof/math/numbers.h>
#include <sof/trace/trace.h>
#include <ipc/topology.h>
#include <errno.h>
#include <stdint.h>

LOG_MODULE_REGISTER(math_auditory, CONFIG_SOF_LOG_LEVEL);

#define ONE_Q16 Q_CONVERT_FLOAT(1, 16)
#define ONE_Q20 Q_CONVERT_FLOAT(1, 20)
#define ONE_Q30 Q_CONVERT_FLOAT(1, 30)
Expand Down Expand Up @@ -117,6 +120,16 @@ int mod_psy_get_mel_filterbank(struct processing_module *mod, struct psy_mel_fil
if (!fb->scratch_data1 || !fb->scratch_data2)
return -ENOMEM;

/* fb->mel_bins is used both as the loop bound and as part of the
* (mel_bins + 1) divisor below. Reject non-positive values up front to
* avoid a divide by zero (mel_bins == -1) and to keep mel_step
* meaningful.
*/
if (fb->mel_bins <= 0 || fb->mel_bins > AUDITORY_MAX_MEL_BANDS) {
comp_cl_err(mod->dev, "Invalid mel_bins %d", fb->mel_bins);
return -EINVAL;
}

/* Log power can be log, or log10 or dB, get multiply coef to convert
* log to desired format.
*/
Expand Down Expand Up @@ -149,6 +162,17 @@ int mod_psy_get_mel_filterbank(struct processing_module *mod, struct psy_mel_fil
mel_start = psy_hz_to_mel(fb->start_freq);
mel_end = psy_hz_to_mel(fb->end_freq);
mel_step = (mel_end - mel_start) / (fb->mel_bins + 1);
Comment thread
singalsu marked this conversation as resolved.
/* delta_cl / delta_rc below are both equal to mel_step; guard against
* a non-positive step (start_freq >= end_freq, or so many bins that
* the integer division truncates to zero) before using them as
* divisors.
*/
Comment thread
singalsu marked this conversation as resolved.
if (mel_step <= 0) {
comp_cl_err(mod->dev, "Invalid mel_step %d (start_freq=%d end_freq=%d mel_bins=%d)",
mel_step, fb->start_freq, fb->end_freq, fb->mel_bins);
return -EINVAL;
}
Comment thread
singalsu marked this conversation as resolved.

for (i = 0; i < fb->mel_bins; i++) {
left_mel = mel_start + i * mel_step;
center_mel = mel_start + (i + 1) * mel_step;
Comment on lines 176 to 178

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Won't do, not relevant.

Expand All @@ -160,6 +184,11 @@ int mod_psy_get_mel_filterbank(struct processing_module *mod, struct psy_mel_fil
if (fb->slaney_normalize) {
left_hz = psy_mel_to_hz(left_mel);
right_hz = psy_mel_to_hz(right_mel);
if (right_hz <= left_hz) {
comp_cl_err(mod->dev, "Invalid Hz range left=%d right=%d at mel bin %d",
left_hz, right_hz, i);
return -EINVAL;
}
Comment thread
singalsu marked this conversation as resolved.
scale = Q_SHIFT_RND(TWO_Q29 / (right_hz - left_hz), 29, 16); /* Q16.16*/
if (i == 0) {
scale_inv = Q_SHIFT_LEFT(ONE_Q30 / scale, 14, 16);
Expand Down
Loading