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

Go to the source code of this file.

Data Structures

struct  fir_f32_s
 Data struct of f32 fir filter. More...
struct  fir_s16_s
 Data struct of s16 fir filter. More...

Macros

#define dsps_fir_f32   dsps_fir_f32_ansi
#define dsps_fird_f32   dsps_fird_f32_ansi
#define dsps_firmr_f32   dsps_firmr_f32_ansi
#define dsps_fird_s16   dsps_fird_s16_ansi
#define dsps_firmr_s16   dsps_firmr_s16_ansi

Typedefs

typedef struct fir_f32_s fir_f32_t
 Data struct of f32 fir filter.
typedef struct fir_s16_s fir_s16_t
 Data struct of s16 fir filter.

Functions

esp_err_t dsps_fir_init_f32 (fir_f32_t *fir, float *coeffs, float *delay, int coeffs_len)
 initialize structure for 32 bit FIR filter
esp_err_t dsps_fird_init_f32 (fir_f32_t *fir, float *coeffs, float *delay, int N, int decim)
 initialize structure for 32 bit Decimation FIR filter Function initialize structure for 32 bit floating point FIR filter with decimation The implementation use ANSI C and could be compiled and run on any platform
esp_err_t dsps_fird_init_s16 (fir_s16_t *fir, int16_t *coeffs, int16_t *delay, int16_t coeffs_len, int16_t decim, int16_t start_pos, int16_t shift)
 initialize structure for 16 bit Decimation FIR filter Function initialize structure for 16 bit signed fixed point FIR filter with decimation The implementation use ANSI C and could be compiled and run on any platform
esp_err_t dsps_fir_f32_ansi (fir_f32_t *fir, const float *input, float *output, int len)
 32 bit floating point FIR filter
esp_err_t dsps_fir_f32_ae32 (fir_f32_t *fir, const float *input, float *output, int len)
esp_err_t dsps_fir_f32_aes3 (fir_f32_t *fir, const float *input, float *output, int len)
int dsps_fird_f32_ansi (fir_f32_t *fir, const float *input, float *output, int len)
 32 bit floating point Decimation FIR filter
int dsps_fird_f32_ae32 (fir_f32_t *fir, const float *input, float *output, int len)
int dsps_fird_f32_aes3 (fir_f32_t *fir, const float *input, float *output, int len)
int dsps_fird_f32_arp4 (fir_f32_t *fir, const float *input, float *output, int len)
int32_t dsps_fird_s16_ansi (fir_s16_t *fir, const int16_t *input, int16_t *output, int32_t len)
 16 bit signed fixed point Decimation FIR filter
int32_t dsps_fird_s16_ae32 (fir_s16_t *fir, const int16_t *input, int16_t *output, int32_t len)
int32_t dsps_fird_s16_aes3 (fir_s16_t *fir, const int16_t *input, int16_t *output, int32_t len)
int32_t dsps_fird_s16_arp4 (fir_s16_t *fir, const int16_t *input, int16_t *output, int32_t len)
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 point multi-rate FIR filter The implementation use ANSI C and could be compiled and run on any platform
esp_err_t dsps_firmr_init_s16 (fir_s16_t *fir, int16_t *coeffs, int16_t *delay, int16_t length, 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 point multi-rate FIR filter The implementation use ANSI C and could be compiled and run on any platform
int dsps_firmr_f32_ansi (fir_f32_t *fir, const float *input, float *output, int input_len)
 32 bit floating point multi-rate FIR filter
int32_t dsps_firmr_s16_ansi (fir_s16_t *fir, const int16_t *input, int16_t *output, int32_t input_len)
 16 bit signed fixed point multi-rate FIR filter
esp_err_t dsps_fird_s16_aexx_free (fir_s16_t *fir)
 support arrays freeing function
esp_err_t dsps_fir_f32_free (fir_f32_t *fir)
 support arrays freeing function
esp_err_t dsps_16_array_rev (int16_t *arr, int16_t len)
 Array reversal.

Macro Definition Documentation

◆ dsps_fir_f32

#define dsps_fir_f32   dsps_fir_f32_ansi

Definition at line 378 of file dsps_fir.h.

◆ dsps_fird_f32

#define dsps_fird_f32   dsps_fird_f32_ansi

Definition at line 379 of file dsps_fir.h.

◆ dsps_fird_s16

#define dsps_fird_s16   dsps_fird_s16_ansi

Definition at line 381 of file dsps_fir.h.

◆ dsps_firmr_f32

#define dsps_firmr_f32   dsps_firmr_f32_ansi

Definition at line 380 of file dsps_fir.h.

Referenced by dsps_resampler_mr_init().

◆ dsps_firmr_s16

#define dsps_firmr_s16   dsps_firmr_s16_ansi

Definition at line 382 of file dsps_fir.h.

Referenced by dsps_resampler_mr_init().

Typedef Documentation

◆ fir_f32_t

typedef struct fir_f32_s fir_f32_t

Data struct of f32 fir filter.

This structure is used by a filter internally. A user should access this structure only in case of extensions for the DSP Library. All fields of this structure are initialized by the dsps_fir_init_f32(...) function.

◆ fir_s16_t

typedef struct fir_s16_s fir_s16_t

Data struct of s16 fir filter.

This structure is used by a filter internally. A user should access this structure only in case of extensions for the DSP Library. All fields of this structure are initialized by the dsps_fir_init_s16(...) function.

Function Documentation

◆ dsps_16_array_rev()

esp_err_t dsps_16_array_rev ( int16_t * arr,
int16_t len )

Array reversal.

Function reverses 16-bit long array members for the purpose of the dsps_fird_s16_aes3 implementation The function has to be called either during the fir struct initialization or every time the coefficients change

Parameters
arrpointer to the array to be reversed
lenlength of the array to be reversed
Returns
  • ESP_OK on success

Definition at line 146 of file dsps_fird_init_s16.c.

147{
148
149 int16_t temp;
150
151 for (int i = 0; i < (int)(len / 2); i++) {
152 temp = arr[i];
153 arr[i] = arr[len - 1 - i];
154 arr[len - 1 - i] = temp;
155 }
156 return ESP_OK;
157}
#define ESP_OK
Definition esp_err.h:23

References ESP_OK.

◆ dsps_fir_f32_ae32()

esp_err_t dsps_fir_f32_ae32 ( fir_f32_t * fir,
const float * input,
float * output,
int len )

◆ dsps_fir_f32_aes3()

esp_err_t dsps_fir_f32_aes3 ( fir_f32_t * fir,
const float * input,
float * output,
int len )

Referenced by test_fir().

Here is the caller graph for this function:

◆ dsps_fir_f32_ansi()

esp_err_t dsps_fir_f32_ansi ( fir_f32_t * fir,
const float * input,
float * output,
int len )

32 bit floating point FIR filter

Function implements FIR filter The extension (_ansi) uses ANSI C and could be compiled and run on any platform. The extension (_ae32) is optimized for ESP32 chip.

Parameters
firpointer to fir filter structure, that must be initialized before
[in]inputinput array
[out]outputarray with the result of FIR filter
[in]lenlength of input and result arrays
Returns
  • ESP_OK on success
  • One of the error codes from DSP library

Definition at line 17 of file dsps_fir_f32_ansi.c.

18{
19 for (int i = 0 ; i < len ; i++) {
20 float acc = 0;
21 int coeff_pos = 0;
22 fir->delay[fir->pos] = input[i];
23 fir->pos++;
24 if (fir->pos >= fir->N) {
25 fir->pos = 0;
26 }
27 for (int n = fir->pos; n < fir->N ; n++) {
28 acc += fir->coeffs[coeff_pos++] * fir->delay[n];
29 }
30 for (int n = 0; n < fir->pos ; n++) {
31 acc += fir->coeffs[coeff_pos++] * fir->delay[n];
32 }
33 output[i] = acc;
34 }
35 return ESP_OK;
36}
float * coeffs
Definition dsps_fir.h:30
int pos
Definition dsps_fir.h:33
float * delay
Definition dsps_fir.h:31
#define N
Definition test_mmult.c:13
const int n
Definition test_mmult.c:17

References fir_f32_s::coeffs, fir_f32_s::delay, ESP_OK, fir_f32_s::N, N, n, and fir_f32_s::pos.

Referenced by test_fir().

Here is the caller graph for this function:

◆ dsps_fir_f32_free()

esp_err_t dsps_fir_f32_free ( fir_f32_t * fir)

support arrays freeing function

Function frees the delay line arrays, if it was allocated by the init functions.

Parameters
firpointer to fir filter structure, that must be initialized before
Returns
  • ESP_OK on success

Definition at line 60 of file dsps_fir_init_f32.c.

61{
62 if (fir->use_delay != 0) {
63 fir->use_delay = 0;
64 free(fir->delay);
65 }
66 return ESP_OK;
67}
int16_t use_delay
Definition dsps_fir.h:35

References fir_f32_s::delay, ESP_OK, and fir_f32_s::use_delay.

Referenced by dsps_resampler_mr_free().

Here is the caller graph for this function:

◆ dsps_fir_init_f32()

esp_err_t dsps_fir_init_f32 ( fir_f32_t * fir,
float * coeffs,
float * delay,
int coeffs_len )

initialize structure for 32 bit FIR filter

Function initialize structure for 32 bit floating point FIR filter The implementation use ANSI C and could be compiled and run on any platform

Parameters
firpointer to fir filter structure, that must be preallocated
coeffsarray with FIR filter coefficients. Must be length N
delayarray for FIR filter delay line. Must have a length = coeffs_len + 4
coeffs_lenFIR filter length. Length of coeffs array. For esp32s3 length should be divided by 4 and aligned to 16.
Returns
  • ESP_OK on success
  • One of the error codes from DSP library

Definition at line 19 of file dsps_fir_init_f32.c.

20{
21 // Allocate delay line in case if it's NULL
22 if (delay == NULL) {
23#ifdef CONFIG_IDF_TARGET_ESP32S3
24 delay = (float *)memalign(16, (coeffs_len + 4) * sizeof(float));
25#else
26 delay = (float *)malloc((coeffs_len + 4) * sizeof(float));
27#endif // CONFIG_IDF_TARGET_ESP32S3
28 fir->use_delay = 1;
29 } else {
30 fir->use_delay = 0;
31 }
32 for (int i = 0; i < (coeffs_len + 4); i++) {
33 delay[i] = 0;
34 }
35 fir->coeffs = coeffs;
36 fir->delay = delay;
37 fir->N = coeffs_len;
38 fir->pos = 0;
39
40#ifdef CONFIG_IDF_TARGET_ESP32S3
41 if (fir->N % 4 != 0) {
43 }
44 // The coeffs array should be aligned to 16
45 if (((uint32_t)coeffs) & 0x0f) {
47 }
48 // The delay array should be aligned to 16
49 if (((uint32_t)delay) & 0x0f) {
51 }
52#endif // CONFIG_IDF_TARGET_ESP32S3
53
54 for (int i = 0 ; i < coeffs_len; i++) {
55 fir->delay[i] = 0;
56 }
57 return ESP_OK;
58}
#define ESP_ERR_DSP_INVALID_LENGTH
#define ESP_ERR_DSP_ARRAY_NOT_ALIGNED
#define memalign(align_, size_)
Definition dsp_tests.h:35
float delay[256]
Definition test_fir.c:15
float coeffs[256]
Definition test_fir.c:14

References coeffs, fir_f32_s::coeffs, delay, fir_f32_s::delay, ESP_ERR_DSP_ARRAY_NOT_ALIGNED, ESP_ERR_DSP_INVALID_LENGTH, ESP_OK, memalign, fir_f32_s::N, fir_f32_s::pos, and fir_f32_s::use_delay.

Referenced by test_fir().

Here is the caller graph for this function:

◆ dsps_fird_f32_ae32()

int dsps_fird_f32_ae32 ( fir_f32_t * fir,
const float * input,
float * output,
int len )

◆ dsps_fird_f32_aes3()

int dsps_fird_f32_aes3 ( fir_f32_t * fir,
const float * input,
float * output,
int len )

◆ dsps_fird_f32_ansi()

int dsps_fird_f32_ansi ( fir_f32_t * fir,
const float * input,
float * output,
int len )

32 bit floating point Decimation FIR filter

Function implements FIR filter with decimation The extension (_ansi) uses ANSI C and could be compiled and run on any platform. The extension (_ae32) is optimized for ESP32 chip.

Parameters
firpointer to fir filter structure, that must be initialized before
inputinput array
outputarray with the result of FIR filter
lenlength of result array
Returns
: function returns the number of samples stored in the output array depends on the previous state value could be [0..len/decimation]

Definition at line 17 of file dsps_fird_f32_ansi.c.

18{
19 int result = 0;
20 for (int i = 0; i < len ; i++) {
21 for (int k = 0 ; k < fir->decim ; k++) {
22 fir->delay[fir->pos++] = *input++;
23 if (fir->pos >= fir->N) {
24 fir->pos = 0;
25 }
26 }
27 float acc = 0;
28 int coeff_pos = 0;
29 for (int n = fir->pos; n < fir->N ; n++) {
30 acc += fir->coeffs[coeff_pos++] * fir->delay[n];
31 }
32 for (int n = 0; n < fir->pos ; n++) {
33 acc += fir->coeffs[coeff_pos++] * fir->delay[n];
34 }
35 output[result++] = acc;
36 }
37 return result;
38}
int decim
Definition dsps_fir.h:34
const int k
Definition test_mmult.c:18

References fir_f32_s::coeffs, fir_f32_s::decim, fir_f32_s::delay, k, fir_f32_s::N, N, n, and fir_f32_s::pos.

◆ dsps_fird_f32_arp4()

int dsps_fird_f32_arp4 ( fir_f32_t * fir,
const float * input,
float * output,
int len )

◆ dsps_fird_init_f32()

esp_err_t dsps_fird_init_f32 ( fir_f32_t * fir,
float * coeffs,
float * delay,
int N,
int decim )

initialize structure for 32 bit Decimation FIR filter Function initialize structure for 32 bit floating point FIR filter with decimation The implementation use ANSI C and could be compiled and run on any platform

Parameters
firpointer to fir filter structure, that must be preallocated
coeffsarray with FIR filter coefficients. Must be length N
delayarray for FIR filter delay line. Must be length N
NFIR filter length. Length of coeffs and delay arrays.
decimdecimation factor.
Returns
  • ESP_OK on success
  • One of the error codes from DSP library

Definition at line 18 of file dsps_fird_init_f32.c.

19{
20 fir->coeffs = coeffs;
21 fir->delay = delay;
22 fir->N = N;
23 fir->pos = 0;
24 fir->decim = decim;
25
26#ifdef CONFIG_IDF_TARGET_ESP32S3
27 // The amount of coefficients should be divided to 4,
28 // if not, add zero coefficients to round length to 0
29 if (fir->N % 4 != 0) {
31 }
32 // The coeffs array should be aligned to 16
33 if (((uint32_t)coeffs) & 0x0f) {
35 }
36 // The delay array should be aligned to 16
37 if (((uint32_t)delay) & 0x0f) {
39 }
40#endif // CONFIG_IDF_TARGET_ESP32S3
41
42 for (int i = 0 ; i < N; i++) {
43 fir->delay[i] = 0;
44 }
45 return ESP_OK;
46}

References coeffs, fir_f32_s::coeffs, fir_f32_s::decim, delay, fir_f32_s::delay, ESP_ERR_DSP_ARRAY_NOT_ALIGNED, ESP_ERR_DSP_INVALID_LENGTH, ESP_OK, fir_f32_s::N, N, and fir_f32_s::pos.

◆ dsps_fird_init_s16()

esp_err_t dsps_fird_init_s16 ( fir_s16_t * fir,
int16_t * coeffs,
int16_t * delay,
int16_t coeffs_len,
int16_t decim,
int16_t start_pos,
int16_t shift )

initialize structure for 16 bit Decimation FIR filter Function initialize structure for 16 bit signed fixed point FIR filter with decimation The implementation use ANSI C and could be compiled and run on any platform

Parameters
firpointer to fir filter structure, that must be preallocated
coeffsarray with FIR filter coefficients. Must be length N
delayarray for FIR filter delay line. Must be length N
coeffs_lenFIR filter length. Length of coeffs and delay arrays.
decimdecimation factor.
start_posinitial value of decimation counter. Must be [0..d)
shiftshift position of the result
Returns
  • ESP_OK on success
  • One of the error codes from DSP library

Definition at line 14 of file dsps_fird_init_s16.c.

15{
16 fir->coeffs = coeffs;
17 fir->delay = delay;
18 fir->coeffs_len = coeffs_len;
19 fir->pos = 0;
20 fir->decim = decim;
21 fir->d_pos = start_pos;
22 fir->shift = shift;
23 fir->rounding_val = (int16_t)(ROUNDING_VALUE);
24 fir->free_status = 0;
25
26 if (fir->coeffs_len < 2) { // number of coeffcients must be higer than 1
28 }
29
30 if ((fir->shift > 40) || (fir->shift < -40)) { // shift amount must be within a range from -40 to 40
32 }
33
34 if (fir->d_pos >= fir->decim) { // start position must be lower than decimation
36 }
37
38#if CONFIG_DSP_OPTIMIZED
39
40 // Rounding value buffer primary for a purpose of ee.ld.accx.ip, but used for both the esp32 and esp32s3
41 // dsps_fird_s16_aexx_free() must be called to free the memory after the FIR function is finished
42 int32_t *aexx_rounding_buff = (int32_t *)memalign(16, 2 * sizeof(int32_t));
43
44 long long rounding = (long long)(fir->rounding_val);
45
46 if (fir->shift >= 0) {
47 rounding = (rounding >> fir->shift);
48 } else {
49 rounding = (rounding << (-fir->shift));
50 }
51#if dsps_fird_s16_arp4_enabled
52 fir->pos = start_pos;
53
54 int16_t *new_delay_buff = (int16_t *)memalign(16, (coeffs_len + 8 * 2) * sizeof(int16_t));
55 for (int i = 0 ; i < (coeffs_len + 8 * 2) ; i++) {
56 new_delay_buff[i] = 0;
57 }
58 fir->delay = &new_delay_buff[8];
59 fir->free_status |= 0x0001;
60
61#endif // dsps_fird_s16_arp4_enabled
62
63
64 aexx_rounding_buff[0] = (int32_t)(rounding); // 32 lower bits (acclo) type reassignment to 32-bit
65 aexx_rounding_buff[1] = (int32_t)((rounding >> 32) & 0xFF); // 8 higher bits (acchi) shift by 32 and apply the mask
66 fir->rounding_buff = aexx_rounding_buff;
67 fir->free_status |= 0x0004;
68
69#if dsps_fird_s16_aes3_enabled
70
71 if (fir->delay == NULL) { // New delay buffer is allocated if the current delay line is NULL
72 int16_t *new_delay_buff = (int16_t *)memalign(16, coeffs_len * sizeof(int16_t));
73 fir->delay = new_delay_buff;
74 fir->free_status |= 0x0001;
75 } else {
76 if ((int)fir->delay & 0xf) { // Delay line array must be aligned
78 }
79 }
80
81 if ((int)fir->coeffs & 0xf) { // Coefficients array must be aligned
83 }
84
85 // If the number of coefficients is not divisible by 8, a new delay line a new coefficients arrays are allocated
86 // the newly allocated arrays are divisible by 8. Coefficients are copied from the original fir structure to
87 // the new coeffs array and the remaining space is filled with zeroes
88 // dsps_fird_s16_free_coeffs_delay must be called to free the memory after the FIR function is finished
89 if (fir->coeffs_len % 8) { // Number of coefficients must be devisible by 8
90 int16_t zero_coeffs = (8 - (fir->coeffs_len % 8));
91 int16_t new_coeffs_len = fir->coeffs_len + zero_coeffs;
92 int16_t *aes3_delay_buff = (int16_t *)memalign(16, new_coeffs_len * sizeof(int16_t));
93 int16_t *aes3_coeffs_buff = (int16_t *)memalign(16, new_coeffs_len * sizeof(int16_t));
94
95 for (int i = 0; i < fir->coeffs_len; i++) { // copy fir->coeffs to aes3_coeffs_buff
96 aes3_coeffs_buff[i] = fir->coeffs[i];
97 }
98
99 for (int i = fir->coeffs_len; i < new_coeffs_len; i++) { // add zeroes to the end
100 aes3_coeffs_buff[i] = 0;
101 }
102
103 fir->delay = aes3_delay_buff;
104 fir->coeffs = aes3_coeffs_buff;
105 fir->coeffs_len = new_coeffs_len;
106 fir->free_status |= 0x0002;
107 }
108
109#endif // dsps_fird_s16_aes3_enabled
110#endif // CONFIG_DSP_OPTIMIZED
111
112 for (int i = 0; i < fir->coeffs_len; i++) { // Initialize the delay line to zero
113 fir->delay[i] = 0;
114 }
115
116 return ESP_OK;
117}
#define ESP_ERR_DSP_PARAM_OUTOFRANGE
#define ROUNDING_VALUE
int16_t shift
Definition dsps_fir.h:61
int16_t * coeffs
Definition dsps_fir.h:55
int32_t rounding_val
Definition dsps_fir.h:63
int16_t * delay
Definition dsps_fir.h:56
int16_t decim
Definition dsps_fir.h:59
int16_t pos
Definition dsps_fir.h:58
int32_t * rounding_buff
Definition dsps_fir.h:62
int16_t d_pos
Definition dsps_fir.h:60
int16_t coeffs_len
Definition dsps_fir.h:57
int16_t free_status
Definition dsps_fir.h:64

References coeffs, fir_s16_s::coeffs, fir_s16_s::coeffs_len, fir_s16_s::d_pos, fir_s16_s::decim, delay, fir_s16_s::delay, ESP_ERR_DSP_ARRAY_NOT_ALIGNED, ESP_ERR_DSP_INVALID_LENGTH, ESP_ERR_DSP_PARAM_OUTOFRANGE, ESP_OK, fir_s16_s::free_status, memalign, fir_s16_s::pos, fir_s16_s::rounding_buff, fir_s16_s::rounding_val, ROUNDING_VALUE, and fir_s16_s::shift.

◆ dsps_fird_s16_ae32()

int32_t dsps_fird_s16_ae32 ( fir_s16_t * fir,
const int16_t * input,
int16_t * output,
int32_t len )

◆ dsps_fird_s16_aes3()

int32_t dsps_fird_s16_aes3 ( fir_s16_t * fir,
const int16_t * input,
int16_t * output,
int32_t len )

◆ dsps_fird_s16_aexx_free()

esp_err_t dsps_fird_s16_aexx_free ( fir_s16_t * fir)

support arrays freeing function

Function frees all the arrays, which were created during the initialization of the fir_s16_t structure

  1. frees allocated memory for rounding buffer, for the purposes of esp32s3 ee.ld.accx.ip assembly instruction
  2. frees allocated memory in case the delay line is NULL
  3. frees allocated memory in case the length of the filter (and the delay line) is not divisible by 8 and new delay line and filter coefficients arrays are created for the purpose of the esp32s3 assembly
Parameters
firpointer to fir filter structure, that must be initialized before
Returns
  • ESP_OK on success

Definition at line 119 of file dsps_fird_init_s16.c.

120{
121
122 if (fir->free_status == 0) {
123 return ESP_OK;
124 }
125
126 if (fir->free_status & 0x0003) {
127
128 if (fir->free_status & 0x0002) {
129 free(fir->coeffs);
130 }
131#if dsps_fird_s16_arp4_enabled
132 fir->delay = &fir->delay[-8];
133#endif
134 free(fir->delay);
135 }
136
137 if (fir->free_status & 0x0004) {
138 free(fir->rounding_buff);
139 }
140 fir->free_status = 0;
141
142 return ESP_OK;
143}

References fir_s16_s::coeffs, fir_s16_s::delay, ESP_OK, fir_s16_s::free_status, and fir_s16_s::rounding_buff.

Referenced by dsps_resampler_mr_free().

Here is the caller graph for this function:

◆ dsps_fird_s16_ansi()

int32_t dsps_fird_s16_ansi ( fir_s16_t * fir,
const int16_t * input,
int16_t * output,
int32_t len )

16 bit signed fixed point Decimation FIR filter

Function implements FIR filter with decimation The extension (_ansi) uses ANSI C and could be compiled and run on any platform. The extension (_ae32) is optimized for ESP32 chip.

Parameters
firpointer to fir filter structure, that must be initialized before
inputinput array
outputarray with the result of the FIR filter
lenlength of the result array
Returns
: function returns the number of samples stored in the output array depends on the previous state value could be [0..len/decimation]

Definition at line 9 of file dsps_fird_s16_ansi.c.

10{
11 int32_t result = 0;
12 int32_t input_pos = 0;
13 long long rounding = 0;
14 const int32_t final_shift = fir->shift - 15;
15
16 rounding = (long long)(fir->rounding_val);
17
18 if (fir->shift >= 0) {
19 rounding = (rounding >> fir->shift) & 0xFFFFFFFFFF; // 40-bit mask
20 } else {
21 rounding = (rounding << (-fir->shift)) & 0xFFFFFFFFFF; // 40-bit mask
22 }
23
24 // len is already a length of the *output array, calculated as (length of the input array / decimation)
25 for (int i = 0; i < len; i++) {
26
27 for (int j = 0; j < fir->decim - fir->d_pos; j++) {
28
29 if (fir->pos >= fir->coeffs_len) {
30 fir->pos = 0;
31 }
32 fir->delay[fir->pos++] = input[input_pos++];
33 }
34 fir->d_pos = 0;
35
36 long long acc = rounding;
37 int16_t coeff_pos = fir->coeffs_len - 1;
38
39 for (int n = fir->pos; n < fir->coeffs_len ; n++) {
40 acc += (int32_t)fir->coeffs[coeff_pos--] * (int32_t)fir->delay[n];
41 }
42 for (int n = 0; n < fir->pos ; n++) {
43 acc += (int32_t)fir->coeffs[coeff_pos--] * (int32_t)fir->delay[n];
44 }
45
46 if (final_shift > 0) {
47 output[result++] = (int16_t)(acc << final_shift);
48 } else {
49 output[result++] = (int16_t)(acc >> (-final_shift));
50 }
51
52 }
53 return result;
54}

References fir_s16_s::coeffs, fir_s16_s::coeffs_len, fir_s16_s::d_pos, fir_s16_s::decim, fir_s16_s::delay, n, fir_s16_s::pos, fir_s16_s::rounding_val, and fir_s16_s::shift.

◆ dsps_fird_s16_arp4()

int32_t dsps_fird_s16_arp4 ( fir_s16_t * fir,
const int16_t * input,
int16_t * output,
int32_t len )

References coeffs, and delay.

◆ dsps_firmr_f32_ansi()

int dsps_firmr_f32_ansi ( fir_f32_t * fir,
const float * input,
float * output,
int input_len )

32 bit floating point multi-rate FIR filter

Function implements FIR filter with decimation The extension (_ansi) uses ANSI C and could be compiled and run on any platform. The extension (_ae32) is optimized for ESP32 chip.

Parameters
firpointer to fir filter structure, that must be initialized before
inputinput array
outputarray with the result of the FIR filter
input_lenlength of the input array
Returns
  • amount of samples stored in the output array
  • depends on the previous state value
  • could be [0..len*intepr/decimation]

Definition at line 14 of file dsps_firmr_f32_ansi.c.

15{
16 int m = fir->start_pos;
17 int result = 0;
18
19 for (int i = 0; i < input_len; i++) {
20 fir->delay[fir->pos] = input[i];
21
22 for (m = fir->start_pos; m < fir->interp; m += fir->decim) {
23 float fir_sum = 0;
24 int coeff_pos = 0;
25 for (int n = fir->pos; n < fir->delay_size; n++) {
26 fir_sum += fir->delay[n] * fir->coeffs[coeff_pos++ * fir->interp + m];
27 }
28 for (int n = 0; n < fir->pos; n++) {
29 fir_sum += fir->delay[n] * fir->coeffs[coeff_pos++ * fir->interp + m];
30 }
31 output[result++] = fir_sum;
32 }
33 fir->start_pos = m - fir->interp;
34
35 fir->pos--;
36 if (fir->pos < 0) {
37 fir->pos = fir->delay_size - 1;
38 }
39 }
40 return result;
41}
int interp
Definition dsps_fir.h:42
int start_pos
Definition dsps_fir.h:44
int delay_size
Interpolation parameters.
Definition dsps_fir.h:41
const int m
Definition test_mmult.c:16

References fir_f32_s::coeffs, fir_f32_s::decim, fir_f32_s::delay, fir_f32_s::delay_size, fir_f32_s::interp, m, n, fir_f32_s::pos, and fir_f32_s::start_pos.

◆ dsps_firmr_init_f32()

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 point multi-rate FIR filter The implementation use ANSI C and could be compiled and run on any platform

Parameters
firpointer to fir filter structure, that must be preallocated
coeffsarray with FIR filter coefficients. Must be length N
delayarray for FIR filter delay line. Must be length N
lengthFIR filter length. Length of coeffs and delay arrays.
interpinterpolation factor.
decimdecimation factor.
start_posinitial value of decimation counter. Must be [0..decim)
Returns
  • ESP_OK on success
  • One of the error codes from DSP library

Definition at line 12 of file dsps_firmr_init_f32.c.

13{
14 fir->coeffs = coeffs;
15 fir->delay = delay;
16 fir->N = length;
17 fir->pos = 0;
18 fir->decim = decim;
19 fir->use_delay = 0;
20 fir->interp = interp;
21 fir->interp_pos = 0;
22 fir->start_pos = start_pos;
23 fir->delay_size = length / interp;
24
25 if (delay == NULL) {
26#ifdef CONFIG_IDF_TARGET_ESP32S3
27 fir->delay = (float *)memalign(16, (fir->delay_size + 4) * sizeof(float));
28#else
29 fir->delay = (float *)malloc((fir->delay_size + 4) * sizeof(float));
30#endif // CONFIG_IDF_TARGET_ESP32S3
31 fir->use_delay = 1;
32 } else {
33 fir->use_delay = 0;
34 }
35
36 if (decim == 0) {
38 }
39 if (interp == 0) {
41 }
42 if (length % interp != 0) {
44 }
45 if (start_pos < 0 || start_pos >= decim) {
47 }
48
49#ifdef CONFIG_IDF_TARGET_ESP32S3
50 // The delay array should be aligned to 16
51 if (((uint32_t)delay) & 0x0f) {
53 }
54#endif // CONFIG_IDF_TARGET_ESP32S3
55
56 for (int i = 0 ; i < fir->delay_size; i++) {
57 fir->delay[i] = 0;
58 }
59 return ESP_OK;
60}
#define ESP_ERR_DSP_INVALID_PARAM
int interp_pos
Definition dsps_fir.h:43

References coeffs, fir_f32_s::coeffs, fir_f32_s::decim, delay, fir_f32_s::delay, fir_f32_s::delay_size, ESP_ERR_DSP_ARRAY_NOT_ALIGNED, ESP_ERR_DSP_INVALID_LENGTH, ESP_ERR_DSP_INVALID_PARAM, ESP_OK, fir_f32_s::interp, fir_f32_s::interp_pos, memalign, fir_f32_s::N, fir_f32_s::pos, fir_f32_s::start_pos, and fir_f32_s::use_delay.

Referenced by dsps_resampler_mr_init().

Here is the caller graph for this function:

◆ dsps_firmr_init_s16()

esp_err_t dsps_firmr_init_s16 ( fir_s16_t * fir,
int16_t * coeffs,
int16_t * delay,
int16_t length,
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 point multi-rate FIR filter The implementation use ANSI C and could be compiled and run on any platform

Parameters
firpointer to fir filter structure, that must be preallocated
coeffsarray with FIR filter coefficients. Must be length N
delayarray for FIR filter delay line. Must be length N
lengthFIR filter length. Length of coeffs and delay arrays.
interpinterpolation factor.
decimdecimation factor.
start_posinitial value of decimation counter. Must be [0..decim)
shiftshift of accumulator value to store in the output array.
Returns
  • ESP_OK on success
  • One of the error codes from DSP library

Definition at line 15 of file dsps_firmr_init_s16.c.

16{
17 fir->coeffs = coeffs;
18 fir->delay = delay;
19
20
21 fir->coeffs_len = coeffs_len;
22 fir->pos = 0;
23 fir->decim = decim;
24 fir->d_pos = start_pos;
25 fir->shift = shift;
26 fir->rounding_val = (int16_t)(ROUNDING_VALUE);
27 fir->free_status = 0;
28
29 if (delay == NULL) {
30#ifdef CONFIG_IDF_TARGET_ESP32S3
31 fir->delay = (int16_t *)memalign(16, (fir->delay_size + 4) * sizeof(int16_t));
32#else
33 fir->delay = (int16_t *)malloc((fir->delay_size + 4) * sizeof(int16_t));
34#endif // CONFIG_IDF_TARGET_ESP32S3
35 fir->free_status = 1;
36 } else {
37 fir->free_status = 0;
38 }
39
40
41 fir->interp = interp;
42 fir->interp_pos = 0;
43 fir->start_pos = start_pos;
44 fir->delay_size = coeffs_len / interp;
45
46
47 if (fir->coeffs_len < 2) { // number of coeffcients must be higer than 1
49 }
50
51 if ((fir->shift > 40) || (fir->shift < -40)) { // shift amount must be within a range from -40 to 40
53 }
54
55 if (fir->d_pos >= fir->decim) { // start position must be lower than decimation
57 }
58
59 for (int i = 0; i < fir->delay_size; i++) { // Initialize the delay line to zero
60 fir->delay[i] = 0;
61 }
62
63 return ESP_OK;
64}
int16_t delay_size
Interpolation parameters.
Definition dsps_fir.h:71
int16_t start_pos
Definition dsps_fir.h:74
int16_t interp_pos
Definition dsps_fir.h:73
int16_t interp
Definition dsps_fir.h:72

References coeffs, fir_s16_s::coeffs, fir_s16_s::coeffs_len, fir_s16_s::d_pos, fir_s16_s::decim, delay, fir_s16_s::delay, fir_s16_s::delay_size, ESP_ERR_DSP_INVALID_LENGTH, ESP_ERR_DSP_PARAM_OUTOFRANGE, ESP_OK, fir_s16_s::free_status, fir_s16_s::interp, fir_s16_s::interp_pos, memalign, fir_s16_s::pos, fir_s16_s::rounding_val, ROUNDING_VALUE, fir_s16_s::shift, and fir_s16_s::start_pos.

Referenced by dsps_resampler_mr_init().

Here is the caller graph for this function:

◆ dsps_firmr_s16_ansi()

int32_t dsps_firmr_s16_ansi ( fir_s16_t * fir,
const int16_t * input,
int16_t * output,
int32_t input_len )

16 bit signed fixed point multi-rate FIR filter

Function implements FIR filter with decimation The extension (_ansi) uses ANSI C and could be compiled and run on any platform. The extension (_ae32) is optimized for ESP32 chip.

Parameters
firpointer to fir filter structure, that must be initialized before
inputinput array
outputarray with the result of the FIR filter
input_lenlength of the intput array
Returns
: function returns the number of samples stored in the output array depends on the previous state value could be [0..len*intepr/decimation]

Definition at line 11 of file dsps_firmr_s16_ansi.c.

12{
13 int32_t result = 0;
14 long long rounding = 0;
15 const int32_t final_shift = fir->shift - 15;
16 rounding = (long long)(fir->rounding_val);
17
18 if (fir->shift >= 0) {
19 rounding = (rounding >> fir->shift) & 0xFFFFFFFFFF; // 40-bit mask
20 } else {
21 rounding = (rounding << (-fir->shift)) & 0xFFFFFFFFFF; // 40-bit mask
22 }
23
24 int32_t m = fir->start_pos;
25
26 for (int i = 0; i < input_len; i++) {
27 fir->delay[fir->pos] = input[i];
28
29 for (m = fir->start_pos; m < fir->interp; m += fir->decim) {
30 long long acc = rounding;
31 int coeff_pos = 0;
32 for (int n = fir->pos; n < fir->delay_size; n++) {
33 acc += (int32_t)fir->delay[n] * (int32_t)fir->coeffs[coeff_pos++ * fir->interp + m];
34 }
35 for (int n = 0; n < fir->pos; n++) {
36 acc += (int32_t)fir->delay[n] * (int32_t)fir->coeffs[coeff_pos++ * fir->interp + m];
37 }
38
39 if (final_shift > 0) {
40 output[result++] = (int16_t)(acc << final_shift);
41 } else {
42 output[result++] = (int16_t)(acc >> (-final_shift));
43 }
44 }
45 fir->start_pos = m - fir->interp;
46
47 fir->pos--;
48 if (fir->pos < 0) {
49 fir->pos = fir->delay_size - 1;
50 }
51 }
52
53 return result;
54}

References fir_s16_s::coeffs, fir_s16_s::decim, fir_s16_s::delay, fir_s16_s::delay_size, fir_s16_s::interp, m, n, fir_s16_s::pos, fir_s16_s::rounding_val, fir_s16_s::shift, and fir_s16_s::start_pos.