ESP-IDF Firmware
Firmware architecture and call graph
Loading...
Searching...
No Matches
dsps_resampler_mr.c
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#include "dsps_resampler.h"
8#include <math.h>
9
10static const float decim_avg_coeff = 0.9999;
11
12esp_err_t dsps_resampler_mr_init(dsps_resample_mr_t *resampler, void *coeffs, int16_t length, int16_t interp, float samplerate_factor, int32_t fixed_point, int16_t shift)
13{
14 esp_err_t ret = ESP_OK;
15
16 if (samplerate_factor < 1) {
18 }
19
20 resampler->fixed_point = fixed_point;
21 resampler->samplerate_factor = samplerate_factor;
22 float decimation = (float)interp / samplerate_factor;
23 resampler->decim_f = floor(decimation);
24 resampler->decim_c = ceil(decimation);
25 resampler->active_decim = resampler->decim_c;
26 resampler->decim_avg_in = 1;
27 resampler->decim_avg_out = resampler->samplerate_factor;
28
29 if (fixed_point == 0) {
30 resampler->dsps_firmr = (int32_t (*)(void *, void *, void *, int32_t))dsps_firmr_f32;
31 resampler->filter = (void *)malloc(sizeof(fir_f32_t));
32
33 ret = dsps_firmr_init_f32((fir_f32_t *)resampler->filter, coeffs, NULL, length, interp, resampler->decim_c, 0);
34 } else {
35 resampler->dsps_firmr = (int32_t (*)(void *, void *, void *, int32_t))dsps_firmr_s16;
36 resampler->filter = (void *)malloc(sizeof(fir_s16_t));
37 ret = dsps_firmr_init_s16((fir_s16_t *)resampler->filter, coeffs, NULL, length, interp, resampler->decim_c, 0, shift);
38 }
39
40 if (ret != ESP_OK) {
41 return ret;
42 }
43
44 return ret;
45}
46
47int32_t dsps_resampler_mr_exec(dsps_resample_mr_t *resampler, void *input, void *output, int32_t length, int32_t length_correction)
48{
49 int32_t result = 0;
50
51 if (resampler->fixed_point == 0) {
52 ((fir_f32_t *)resampler->filter)->decim = resampler->active_decim;
53 } else {
54 ((fir_s16_t *)resampler->filter)->decim = resampler->active_decim;
55 }
56
57 result = resampler->dsps_firmr(resampler->filter, input, output, length);
58
59 resampler->decim_avg_in = resampler->decim_avg_in * decim_avg_coeff + (float)length * (1 - decim_avg_coeff);
60 resampler->decim_avg_out = resampler->decim_avg_out * decim_avg_coeff + (float)(result - length_correction) * (1 - decim_avg_coeff);
61
62 if ((resampler->decim_avg_in * resampler->samplerate_factor) > (resampler->decim_avg_out)) {
63 resampler->active_decim = resampler->decim_f;
64 } else {
65 resampler->active_decim = resampler->decim_c;
66 }
67 return result;
68}
69
71{
72 if (resampler->fixed_point == 0) {
73 dsps_fir_f32_free((fir_f32_t *)(resampler->filter));
74 } else {
76 }
77 free(resampler->filter);
78}
#define ESP_ERR_DSP_INVALID_PARAM
#define dsps_firmr_f32
Definition dsps_fir.h:380
struct fir_f32_s fir_f32_t
Data struct of f32 fir filter.
#define dsps_firmr_s16
Definition dsps_fir.h:382
struct fir_s16_s fir_s16_t
Data struct of s16 fir filter.
esp_err_t dsps_fir_f32_free(fir_f32_t *fir)
support arrays freeing function
esp_err_t dsps_fird_s16_aexx_free(fir_s16_t *fir)
support arrays freeing function
esp_err_t dsps_firmr_init_f32(fir_f32_t *fir, float *coeffs, float *delay, int length, int interp, int decim, int start_pos)
initialize structure for multi-rate FIR filter Function initialize structure for 32 bit floating poin...
esp_err_t dsps_firmr_init_s16(fir_s16_t *fir, int16_t *coeffs, int16_t *delay, int16_t coeffs_len, int16_t interp, int16_t decim, int16_t start_pos, int16_t shift)
initialize structure for multi-rate FIR filter Function initialize structure for 16 bit signed fixed ...
struct dsps_resample_mr_s dsps_resample_mr_t
Data struct of f32 multi-rate resampler.
static const float decim_avg_coeff
void dsps_resampler_mr_free(dsps_resample_mr_t *resampler)
Free the multi-rate resampler.
esp_err_t dsps_resampler_mr_init(dsps_resample_mr_t *resampler, void *coeffs, int16_t length, int16_t interp, float samplerate_factor, int32_t fixed_point, int16_t shift)
Initialize the multi-rate resampler.
int32_t dsps_resampler_mr_exec(dsps_resample_mr_t *resampler, void *input, void *output, int32_t length, int32_t length_correction)
Execute the multi-rate resampler.
int esp_err_t
Definition esp_err.h:21
#define ESP_OK
Definition esp_err.h:23
int32_t(* dsps_firmr)(void *fir, void *input, void *output, int32_t length)
float coeffs[256]
Definition test_fir.c:14