Repository navigation
Expand file tree
/
Copy pathfir_float.c
More file actions
184 lines (151 loc) · 3.55 KB
/
Copy pathfir_float.c
File metadata and controls
184 lines (151 loc) · 3.55 KB
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
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2026 Sound Open Firmware Authors. All rights reserved.
#include <sof/common.h>
#if CONFIG_FORMAT_FLOAT
#include <sof/math/fir_config.h>
#include <sof/math/fir_float.h>
#include <sof/math/numbers.h>
#include <rtos/symbol.h>
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
void fir_reset_float(struct fir_state_float *fir)
{
fir->rwi = 0;
fir->taps = 0;
fir->length = 0;
fir->out_shift = 0;
fir->coef = NULL;
}
EXPORT_SYMBOL(fir_reset_float);
int fir_delay_size_float(struct sof_fir_coef_data *config)
{
if (config->length > SOF_FIR_MAX_LENGTH || config->length < 4)
return -EINVAL;
if (config->length & 0x3)
return -EINVAL;
return (config->length + 4) * sizeof(float);
}
EXPORT_SYMBOL(fir_delay_size_float);
int fir_coef_size_float(struct sof_fir_coef_data *config)
{
if (config->length > SOF_FIR_MAX_LENGTH || config->length < 4)
return -EINVAL;
return config->length * sizeof(float);
}
EXPORT_SYMBOL(fir_coef_size_float);
int fir_init_coef_float(struct fir_state_float *fir,
struct sof_fir_coef_data *config,
float **coef_storage)
{
fir->rwi = 0;
fir->taps = (int)config->length;
fir->length = (int)fir->taps + 2;
fir->out_shift = 0;
if (coef_storage && *coef_storage) {
float scale;
int shift = config->out_shift;
int i;
fir->coef = *coef_storage;
*coef_storage += fir->taps;
if (shift >= 0 && shift <= 30)
scale = (1.0f / 32768.0f) / (float)(1 << shift);
else if (shift < 0 && shift >= -30)
scale = (1.0f / 32768.0f) * (float)(1 << (-shift));
else
scale = 1.0f / 32768.0f;
for (i = 0; i < fir->taps; i++)
fir->coef[i] = (float)config->coef[i] * scale;
}
return 0;
}
EXPORT_SYMBOL(fir_init_coef_float);
void fir_init_delay_float(struct fir_state_float *fir, float **data)
{
fir->delay = *data;
*data += fir->length;
}
EXPORT_SYMBOL(fir_init_delay_float);
float fir_float(struct fir_state_float *fir, float x)
{
float *data = &fir->delay[fir->rwi];
const float *coef = &fir->coef[0];
float y = 0.0f;
int n1, n2, n;
const int length = fir->length;
const int taps = fir->taps;
if (!fir->length)
return x;
*data = x;
n1 = ++fir->rwi;
if (fir->rwi == length)
fir->rwi = 0;
n1 = MIN(n1, taps);
for (n = 0; n < n1; n++) {
y += (*coef) * (*data);
coef++;
data--;
}
n2 = taps - n1;
data = &fir->delay[length - 1];
for (n = 0; n < n2; n++) {
y += (*coef) * (*data);
coef++;
data--;
}
return y;
}
EXPORT_SYMBOL(fir_float);
void fir_float_2x(struct fir_state_float *fir, float x0, float x1, float *y0, float *y1)
{
if (!fir->taps) {
*y0 = x0;
*y1 = x1;
return;
}
/* When write index is at the last slot, wrap the second sample to slot 0 */
if (fir->rwi == fir->length - 1) {
*y0 = fir_float(fir, x0);
*y1 = fir_float(fir, x1);
return;
}
float a0 = 0.0f;
float a1 = 0.0f;
float sample0;
float sample1;
float tap;
float *data = &fir->delay[fir->rwi];
const float *coef = &fir->coef[0];
int n1, n2, i;
const int length = fir->length;
const int taps = fir->taps;
*data = x0;
*(data + 1) = x1;
n1 = fir->rwi + 1;
fir->rwi += 2;
if (fir->rwi >= length)
fir->rwi -= length;
sample1 = x1;
n1 = MIN(n1, taps);
for (i = 0; i < n1; i++) {
tap = *coef++;
sample0 = *data--;
a1 += tap * sample1;
a0 += tap * sample0;
sample1 = sample0;
}
n2 = taps - n1;
data = &fir->delay[length - 1];
for (i = 0; i < n2; i++) {
tap = *coef++;
sample0 = *data--;
a1 += tap * sample1;
a0 += tap * sample0;
sample1 = sample0;
}
*y0 = a0;
*y1 = a1;
}
EXPORT_SYMBOL(fir_float_2x);
#endif /* CONFIG_FORMAT_FLOAT */