ESP-IDF Firmware
Firmware architecture and call graph
Loading...
Searching...
No Matches
dsps_resampler_mr.c File Reference
#include "dsps_resampler.h"
#include <math.h>
Include dependency graph for dsps_resampler_mr.c:

Go to the source code of this file.

Functions

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.
void dsps_resampler_mr_free (dsps_resample_mr_t *resampler)
 Free the multi-rate resampler.

Variables

static const float decim_avg_coeff = 0.9999

Function Documentation

◆ dsps_resampler_mr_exec()

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.

This function executes the multi-rate resampler. The input and output buffers can be the same. The function based on multi-rate FIR filter. The decimation factor is updated for each execution. The current decimation factor calculated as division of average input and average output sample rate. To correct the output sample rate, the length_correction parameter is used. To increase the output sample rate, the length_correction parameter should be positive. To decrease the output sample rate, the length_correction parameter should be negative. This parameter is used when the input and output sample rates are comes from different sources.

Parameters
resamplerPointer to the resampler structure
inputPointer to the input buffer
outputPointer to the output buffer
lengthLength of the input buffers
length_correctionLength correction for the current execution. Positive value increases the output sample rate, negative value decreases the output sample rate.
Returns
Length of the output buffer

Definition at line 47 of file dsps_resampler_mr.c.

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}
struct fir_f32_s fir_f32_t
Data struct of f32 fir filter.
struct fir_s16_s fir_s16_t
Data struct of s16 fir filter.
static const float decim_avg_coeff
int32_t(* dsps_firmr)(void *fir, void *input, void *output, int32_t length)

References dsps_resample_mr_s::active_decim, decim_avg_coeff, dsps_resample_mr_s::decim_avg_in, dsps_resample_mr_s::decim_avg_out, dsps_resample_mr_s::decim_c, dsps_resample_mr_s::decim_f, dsps_resample_mr_s::dsps_firmr, dsps_resample_mr_s::filter, dsps_resample_mr_s::fixed_point, and dsps_resample_mr_s::samplerate_factor.

◆ dsps_resampler_mr_free()

void dsps_resampler_mr_free ( dsps_resample_mr_t * resampler)

Free the multi-rate resampler.

Parameters
resamplerPointer to the resampler structure

Definition at line 70 of file dsps_resampler_mr.c.

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}
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

References dsps_fir_f32_free(), dsps_fird_s16_aexx_free(), dsps_resample_mr_s::filter, and dsps_resample_mr_s::fixed_point.

Here is the call graph for this function:

◆ dsps_resampler_mr_init()

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.

Parameters
resamplerPointer to the resampler structure
coeffsPointer to the filter coefficients (float or int16_t for fixed point)
lengthLength of the filter coefficients
interpInterpolation factor for the filter
samplerate_factorSample rate factor
fixed_pointFixed point flag, 0 for float, 1 for fixed point
shiftShift value for fixed point
Returns
ESP_OK on success, ESP_ERR_INVALID_ARG if the parameters are invalid

Definition at line 12 of file dsps_resampler_mr.c.

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}
#define ESP_ERR_DSP_INVALID_PARAM
#define dsps_firmr_f32
Definition dsps_fir.h:380
#define dsps_firmr_s16
Definition dsps_fir.h:382
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 ...
int esp_err_t
Definition esp_err.h:21
#define ESP_OK
Definition esp_err.h:23
float coeffs[256]
Definition test_fir.c:14

References dsps_resample_mr_s::active_decim, coeffs, dsps_resample_mr_s::decim_avg_in, dsps_resample_mr_s::decim_avg_out, dsps_resample_mr_s::decim_c, dsps_resample_mr_s::decim_f, dsps_resample_mr_s::dsps_firmr, dsps_firmr_f32, dsps_firmr_init_f32(), dsps_firmr_init_s16(), dsps_firmr_s16, ESP_ERR_DSP_INVALID_PARAM, ESP_OK, dsps_resample_mr_s::filter, dsps_resample_mr_s::fixed_point, and dsps_resample_mr_s::samplerate_factor.

Here is the call graph for this function:

Variable Documentation

◆ decim_avg_coeff

const float decim_avg_coeff = 0.9999
static

Definition at line 10 of file dsps_resampler_mr.c.

Referenced by dsps_resampler_mr_exec().