ESP-IDF Firmware
Firmware architecture and call graph
Loading...
Searching...
No Matches
dsps_fird_s16_ansi.c
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#include "dsps_fir.h"
8
9int32_t dsps_fird_s16_ansi(fir_s16_t *fir, const int16_t *input, int16_t *output, int32_t len)
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}
struct fir_s16_s fir_s16_t
Data struct of s16 fir filter.
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
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
int16_t d_pos
Definition dsps_fir.h:60
int16_t coeffs_len
Definition dsps_fir.h:57
const int n
Definition test_mmult.c:17