ESP-IDF Firmware
Firmware architecture and call graph
Loading...
Searching...
No Matches
dsps_resampler_ph.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
10esp_err_t dsps_resampler_ph_init(dsps_resample_ph_t *resampler, float samplerate_factor)
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}
27
28int32_t dsps_resampler_ph_exec(dsps_resample_ph_t *resampler, float *input, float *output, int32_t length)
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}
#define ESP_ERR_DSP_INVALID_PARAM
struct dsps_resample_ph_s dsps_resample_ph_t
Data struct of f32 poly-phase 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.
int esp_err_t
Definition esp_err.h:21
#define ESP_OK
Definition esp_err.h:23