-
Notifications
You must be signed in to change notification settings - Fork 85
/
dsps_fir.h
275 lines (243 loc) · 9.83 KB
/
dsps_fir.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2018-2022 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _dsps_fir_H_
#define _dsps_fir_H_
#include "dsp_err.h"
#include "dsps_fir_platform.h"
#include "dsp_common.h"
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief 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.
*/
typedef struct fir_f32_s {
float *coeffs; /*!< Pointer to the coefficient buffer.*/
float *delay; /*!< Pointer to the delay line buffer.*/
int N; /*!< FIR filter coefficients amount.*/
int pos; /*!< Position in delay line.*/
int decim; /*!< Decimation factor.*/
int16_t use_delay; /*!< The delay line was allocated by init function.*/
} fir_f32_t;
/**
* @brief 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.
*/
typedef struct fir_s16_s {
int16_t *coeffs; /*!< Pointer to the coefficient buffer.*/
int16_t *delay; /*!< Pointer to the delay line buffer.*/
int16_t coeffs_len; /*!< FIR filter coefficients amount.*/
int16_t pos; /*!< Position in delay line.*/
int16_t decim; /*!< Decimation factor.*/
int16_t d_pos; /*!< Actual decimation counter.*/
int16_t shift; /*!< Shift value of the result.*/
int32_t *rounding_buff; /*!< Rounding buffer for the purposes of esp32s3 ee.ld.accx.ip assembly instruction */
int32_t rounding_val; /*!< Rounding value*/
int16_t free_status; /*!< Indicator for dsps_fird_s16_aes3_free() function*/
} fir_s16_t;
/**
* @brief 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
*
* @param fir: pointer to fir filter structure, that must be preallocated
* @param coeffs: array with FIR filter coefficients. Must be length N
* @param delay: array for FIR filter delay line. Must have a length = coeffs_len + 4
* @param coeffs_len: FIR filter length. Length of coeffs array. For esp32s3 length should be divided by 4 and aligned to 16.
*
* @return
* - ESP_OK on success
* - One of the error codes from DSP library
*/
esp_err_t dsps_fir_init_f32(fir_f32_t *fir, float *coeffs, float *delay, int coeffs_len);
/**
* @brief 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
*
* @param fir: pointer to fir filter structure, that must be preallocated
* @param coeffs: array with FIR filter coefficients. Must be length N
* @param delay: array for FIR filter delay line. Must be length N
* @param N: FIR filter length. Length of coeffs and delay arrays.
* @param decim: decimation factor.
*
* @return
* - ESP_OK on success
* - One of the error codes from DSP library
*/
esp_err_t dsps_fird_init_f32(fir_f32_t *fir, float *coeffs, float *delay, int N, int decim);
/**
* @brief 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
*
* @param fir: pointer to fir filter structure, that must be preallocated
* @param coeffs: array with FIR filter coefficients. Must be length N
* @param delay: array for FIR filter delay line. Must be length N
* @param coeffs_len: FIR filter length. Length of coeffs and delay arrays.
* @param decim: decimation factor.
* @param start_pos: initial value of decimation counter. Must be [0..d)
* @param shift: shift position of the result
*
* @return
* - ESP_OK on success
* - One of the error codes from DSP library
*/
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);
/**@{*/
/**
* @brief 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.
*
* @param fir: pointer to fir filter structure, that must be initialized before
* @param[in] input: input array
* @param[out] output: array with the result of FIR filter
* @param[in] len: length of input and result arrays
*
* @return
* - ESP_OK on success
* - One of the error codes from DSP library
*/
esp_err_t dsps_fir_f32_ansi(fir_f32_t *fir, const float *input, float *output, int len);
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);
/**@}*/
/**@{*/
/**
* @brief 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.
*
* @param fir: pointer to fir filter structure, that must be initialized before
* @param input: input array
* @param output: array with the result of FIR filter
* @param len: length of result array
*
* @return: function returns the number of samples stored in the output array
* depends on the previous state value could be [0..len/decimation]
*/
int dsps_fird_f32_ansi(fir_f32_t *fir, const float *input, float *output, int len);
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);
/**@}*/
/**@{*/
/**
* @brief 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.
*
* @param fir: pointer to fir filter structure, that must be initialized before
* @param input: input array
* @param output: array with the result of the FIR filter
* @param len: length of the result array
*
* @return: function returns the number of samples stored in the output array
* depends on the previous state value could be [0..len/decimation]
*/
int32_t dsps_fird_s16_ansi(fir_s16_t *fir, const int16_t *input, int16_t *output, int32_t len);
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);
/**@}*/
/**@{*/
/**
* @brief 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
*
* @param fir: pointer to fir filter structure, that must be initialized before
*
* @return
* - ESP_OK on success
*/
esp_err_t dsps_fird_s16_aexx_free(fir_s16_t *fir);
/**@}*/
/**@{*/
/**
* @brief support arrays freeing function
*
* Function frees the delay line arrays, if it was allocated by the init functions.
*
* @param fir: pointer to fir filter structure, that must be initialized before
*
* @return
* - ESP_OK on success
*/
esp_err_t dsps_fir_f32_free(fir_f32_t *fir);
/**@}*/
/**@{*/
/**
* @brief 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
*
* @param arr: pointer to the array to be reversed
* @param len: length of the array to be reversed
*
* @return
* - ESP_OK on success
*/
esp_err_t dsps_16_array_rev(int16_t *arr, int16_t len);
/**@}*/
#ifdef __cplusplus
}
#endif
#if CONFIG_DSP_OPTIMIZED
#if (dsps_fir_f32_ae32_enabled == 1)
#define dsps_fir_f32 dsps_fir_f32_ae32
#elif (dsps_fir_f32_aes3_enabled == 1)
#define dsps_fir_f32 dsps_fir_f32_aes3
#else
#define dsps_fir_f32 dsps_fir_f32_ansi
#endif
#if (dsps_fird_f32_aes3_enabled == 1)
#define dsps_fird_f32 dsps_fird_f32_aes3
#elif (dsps_fird_f32_ae32_enabled == 1)
#define dsps_fird_f32 dsps_fird_f32_ae32
#else
#define dsps_fird_f32 dsps_fird_f32_ansi
#endif
#if (dsps_fird_s16_ae32_enabled == 1)
#define dsps_fird_s16 dsps_fird_s16_ae32
#elif (dsps_fird_s16_aes3_enabled == 1)
#define dsps_fird_s16 dsps_fird_s16_aes3
#else
#define dsps_fird_s16 dsps_fird_s16_ansi
#endif
#else // CONFIG_DSP_OPTIMIZED
#define dsps_fir_f32 dsps_fir_f32_ansi
#define dsps_fird_f32 dsps_fird_f32_ansi
#define dsps_fird_s16 dsps_fird_s16_ansi
#endif // CONFIG_DSP_OPTIMIZED
#endif // _dsps_fir_H_