ESP-IDF Firmware
Firmware architecture and call graph
Loading...
Searching...
No Matches
dsps_resampler.h File Reference
#include "dsp_err.h"
#include "dsps_fir.h"
Include dependency graph for dsps_resampler.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  dsps_resample_mr_s
 Data struct of f32 multi-rate resampler. More...
struct  dsps_resample_ph_s
 Data struct of f32 poly-phase resampler. More...

Typedefs

typedef struct dsps_resample_mr_s dsps_resample_mr_t
 Data struct of f32 multi-rate resampler.
typedef struct dsps_resample_ph_s dsps_resample_ph_t
 Data struct of f32 poly-phase resampler.

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.
esp_err_t dsps_resampler_ph_init (dsps_resample_ph_t *resampler, float samplerate_factor)
 Initialize the poly-phase resampler.
int32_t dsps_resampler_ph_exec (dsps_resample_ph_t *resampler, float *input, float *output, int32_t length)
 Execute the poly-phase resampler.

Typedef Documentation

◆ dsps_resample_mr_t

Data struct of f32 multi-rate resampler.

This structure is used by a resampler internally. A user should access this structure only in case of extensions for the DSP Library. To initialize the resampler, use the dsps_resampler_mr_init() function. To execute the resampler, use the dsps_resampler_mr_exec() function. To free the resampler, use the dsps_resampler_mr_free() function.

◆ dsps_resample_ph_t

Data struct of f32 poly-phase resampler.

This structure is used by a poly-phase resampler internally. A user should access this structure only in case of extensions for the DSP Library. To initialize the resampler, use the dsps_resampler_ph_init() function. To execute the resampler, use the dsps_resampler_ph_exec() function.

It is possible to correct the sample rate by adjust the phase parameter "on the fly".

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:

◆ dsps_resampler_ph_exec()

int32_t dsps_resampler_ph_exec ( dsps_resample_ph_t * resampler,
float * input,
float * output,
int32_t length )

Execute the poly-phase resampler.

Parameters
resamplerPointer to the resampler structure
inputPointer to the input buffer
outputPointer to the output buffer
lengthLength of the input buffer
Returns
Length of the output buffer

Definition at line 28 of file dsps_resampler_ph.c.

29{
30 int32_t result = 0;
31 int input_pos = 0;
32 float in_x[4];
33
34 int in_pos = 0;
35 for (int pos = resampler->delay_pos; pos < 4; pos++) {
36 in_x[in_pos++] = resampler->delay[pos];
37 }
38 for (int pos = 0; pos < resampler->delay_pos; pos++) {
39 in_x[in_pos++] = resampler->delay[pos];
40 }
41
42 while (input_pos < length) {
43
44 float c0 = in_x[1];
45 float c1 = 0.5 * (in_x[2] - in_x[0]);
46 float c2 = in_x[0] - 2.5 * in_x[1] + 2 * in_x[2] - 0.5 * in_x[3];
47 float c3 = 0.5 * (in_x[3] - in_x[0]) + 1.5 * (in_x[1] - in_x[2]);
48
49 output[result] = c0 + resampler->phase * (c1 + resampler->phase * (c2 + resampler->phase * c3));
50 result++;
51 resampler->phase += resampler->step;
52
53 while (resampler->phase >= 1) {
54 resampler->phase -= 1;
55 resampler->delay[resampler->delay_pos] = input[input_pos];
56 resampler->delay_pos++;
57 if (resampler->delay_pos >= 4) {
58 resampler->delay_pos = 0;
59 }
60 input_pos++;
61 in_pos = 0;
62 for (int pos = resampler->delay_pos; pos < 4; pos++) {
63 in_x[in_pos++] = resampler->delay[pos];
64 }
65 for (int pos = 0; pos < resampler->delay_pos; pos++) {
66 in_x[in_pos++] = resampler->delay[pos];
67 }
68 }
69 }
70
71 return result;
72}

References dsps_resample_ph_s::delay, dsps_resample_ph_s::delay_pos, dsps_resample_ph_s::phase, and dsps_resample_ph_s::step.

◆ dsps_resampler_ph_init()

esp_err_t dsps_resampler_ph_init ( dsps_resample_ph_t * resampler,
float samplerate_factor )

Initialize the poly-phase resampler.

The poly-phase resampler is a implementation of the poly-phase Farrow filter that use cubic interpolation with 4 coefficients.

Parameters
resamplerPointer to the resampler structure
samplerate_factorSample rate factor
Returns
ESP_OK on success, ESP_ERR_INVALID_ARG if the parameters are invalid

Definition at line 10 of file dsps_resampler_ph.c.

11{
12 esp_err_t ret = ESP_OK;
13
14 if (samplerate_factor < 1) {
16 }
17
18 for (int i = 0; i < 4; i++) {
19 resampler->delay[i] = 0;
20 }
21 resampler->delay_pos = 0;
22
23 resampler->step = 1.0f / samplerate_factor;
24 resampler->phase = 0;
25 return ret;
26}

References dsps_resample_ph_s::delay, dsps_resample_ph_s::delay_pos, ESP_ERR_DSP_INVALID_PARAM, ESP_OK, dsps_resample_ph_s::phase, and dsps_resample_ph_s::step.