Skip to content

Commit bda1416

Browse files
committed
Merge branch 'bugfix/dl_fft' into 'master'
Bugfix/dl fft See merge request ai/esp-dl!183
2 parents 991bb2d + e371b31 commit bda1416

File tree

5 files changed

+58
-26
lines changed

5 files changed

+58
-26
lines changed

test_apps/dl_fft/main/test_dl_fft.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ TEST_CASE("7. test dl fft hp s16", "[dl_fft]")
244244
fft_input_s16_128, fft_input_s16_256, fft_input_s16_512, fft_input_s16_1024, fft_input_s16_2048};
245245
const float *output[5] = {fft_output_128, fft_output_256, fft_output_512, fft_output_1024, fft_output_2048};
246246
int test_nfft[5] = {128, 256, 512, 1024, 2048};
247-
float target_db = 65; // high precision int16 fft
247+
float target_db = 60; // high precision int16 fft
248248
int ram_size_before = heap_caps_get_free_size(MALLOC_CAP_8BIT);
249249
uint32_t start = 0, end = 0;
250250
int out_exponent;
@@ -261,7 +261,7 @@ TEST_CASE("7. test dl fft hp s16", "[dl_fft]")
261261

262262
// check snr
263263
dl_short_to_float(x, nfft * 2, out_exponent, y);
264-
TEST_ASSERT_EQUAL(true, check_fft_results(y, output[i], nfft, target_db, 5e-3));
264+
TEST_ASSERT_EQUAL(true, check_fft_results(y, output[i], nfft, target_db, 2e-2));
265265

266266
start = esp_timer_get_time();
267267
for (int k = 0; k < LOOP; k++) {
@@ -285,7 +285,7 @@ TEST_CASE("8. test dl ifft hp s16", "[dl_fft]")
285285
fft_input_s16_128, fft_input_s16_256, fft_input_s16_512, fft_input_s16_1024, fft_input_s16_2048};
286286
// const float *output[5] = {fft_input_128, fft_input_256, fft_input_512, fft_input_1024, fft_input_2048};
287287
int test_nfft[5] = {128, 256, 512, 1024, 2048};
288-
float target_db = 65; // high precision int16 fft
288+
float target_db = 60; // high precision int16 fft
289289
int ram_size_before = heap_caps_get_free_size(MALLOC_CAP_8BIT);
290290
uint32_t start = 0, end = 0;
291291
int exponent;
@@ -306,7 +306,7 @@ TEST_CASE("8. test dl ifft hp s16", "[dl_fft]")
306306
// check snr
307307
dl_short_to_float(x, nfft * 2, out_exponent, y);
308308
dl_short_to_float(input[i], nfft * 2, -15, z);
309-
TEST_ASSERT_EQUAL(true, check_fft_results(y, z, nfft, target_db, 5e-3));
309+
TEST_ASSERT_EQUAL(true, check_fft_results(y, z, nfft, target_db, 1e-2));
310310

311311
start = esp_timer_get_time();
312312
for (int k = 0; k < LOOP; k++) {

tools/dl_fft/base/dl_fft2r_sc16_ansi.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ static inline int16_t dl_xtfixed_bf_1(
88
result -= (int32_t)a1 * (int32_t)a2 + (int32_t)a3 * (int32_t)a4;
99
result += add_rount_mult;
1010
result = result >> result_shift;
11+
1112
return (int16_t)result;
1213
}
1314

@@ -19,6 +20,7 @@ static inline int16_t dl_xtfixed_bf_2(
1920
result -= ((int32_t)a1 * (int32_t)a2 - (int32_t)a3 * (int32_t)a4);
2021
result += add_rount_mult;
2122
result = result >> result_shift;
23+
2224
return (int16_t)result;
2325
}
2426

@@ -30,6 +32,7 @@ static inline int16_t dl_xtfixed_bf_3(
3032
result += (int32_t)a1 * (int32_t)a2 + (int32_t)a3 * (int32_t)a4;
3133
result += add_rount_mult;
3234
result = result >> result_shift;
35+
3336
return (int16_t)result;
3437
}
3538

@@ -41,6 +44,7 @@ static inline int16_t dl_xtfixed_bf_4(
4144
result += (int32_t)a1 * (int32_t)a2 - (int32_t)a3 * (int32_t)a4;
4245
result += add_rount_mult;
4346
result = result >> result_shift;
47+
4448
return (int16_t)result;
4549
}
4650

@@ -192,23 +196,25 @@ esp_err_t dl_fft2r_sc16_hp_ansi(int16_t *data, int N, int16_t *table, int *shift
192196
uint32_t *w = (uint32_t *)table;
193197
uint32_t *in_data = (uint32_t *)data;
194198

195-
int ie, ia, m;
199+
int ie, ia, m, loop_num = 2;
196200
dl_sc16_t cs; // c - re, s - im
197201
dl_sc16_t m_data;
198202
dl_sc16_t a_data;
199203
int add_rount_mult = 1 << 15;
200-
bool flag = true;
201204

202205
ie = 1;
203206
shift[0] = 0;
204207
for (int N2 = N / 2; N2 > 0; N2 >>= 1) {
205208
ia = 0;
206209
int loop_shift = 16;
207-
if (flag || N2 == 1) {
208-
loop_shift = dl_array_max_q_s16(data, N);
209-
flag = false;
210+
if (loop_num == 2) {
211+
loop_shift = dl_array_max_q_s16(data, N * 2);
212+
if (loop_shift < 16) {
213+
loop_shift += 1;
214+
}
215+
loop_num = 0;
210216
} else {
211-
flag = true;
217+
loop_num += 1;
212218
}
213219
shift[0] += loop_shift - 15;
214220
add_rount_mult = 1 << (loop_shift - 1);
@@ -273,23 +279,25 @@ esp_err_t dl_ifft2r_sc16_hp_ansi(int16_t *data, int N, int16_t *table, int *shif
273279
uint32_t *w = (uint32_t *)table;
274280
uint32_t *in_data = (uint32_t *)data;
275281

276-
int ie, ia, m;
282+
int ie, ia, m, loop_num = 2;
277283
dl_sc16_t cs; // c - re, s - im
278284
dl_sc16_t m_data;
279285
dl_sc16_t a_data;
280286
int add_rount_mult = 1 << 15;
281-
bool flag = true;
282287

283288
ie = 1;
284289
shift[0] = 0;
285290
for (int N2 = N / 2; N2 > 0; N2 >>= 1) {
286291
ia = 0;
287292
int loop_shift = 16;
288-
if (flag || N2 == 1) {
289-
loop_shift = dl_array_max_q_s16(data, N);
290-
flag = false;
293+
if (loop_num == 2) {
294+
loop_shift = dl_array_max_q_s16(data, N * 2);
295+
if (loop_shift < 16) {
296+
loop_shift += 1;
297+
}
298+
loop_num = 0;
291299
} else {
292-
flag = true;
300+
loop_num += 1;
293301
}
294302
shift[0] += loop_shift - 15;
295303
add_rount_mult = 1 << (loop_shift - 1);

tools/dl_fft/base/dl_fft_base.c

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,8 @@ int dl_power_of_two(uint32_t n)
3232

3333
float *dl_short_to_float(const int16_t *x, int len, int exponent, float *y)
3434
{
35-
float scale = 1.0;
36-
if (exponent > 0) {
37-
scale = (1 << exponent);
38-
} else if (exponent < 0) {
39-
scale = 1.0 / (1 << (-exponent));
40-
}
35+
float scale = powf(2, exponent);
36+
// printf("scale: %f\n", scale);
4137
for (int i = 0; i < len; i++) {
4238
y[i] = scale * x[i];
4339
}
@@ -46,7 +42,7 @@ float *dl_short_to_float(const int16_t *x, int len, int exponent, float *y)
4642

4743
int16_t dl_array_max_q_s16(const int16_t *x, int size)
4844
{
49-
int32_t max = x[0];
45+
int16_t max = 0;
5046
for (int i = 1; i < size; i++) {
5147
if (x[i] > max) {
5248
max = x[i];
@@ -59,11 +55,38 @@ int16_t dl_array_max_q_s16(const int16_t *x, int size)
5955
return 1;
6056
}
6157

62-
uint16_t k = 2;
58+
int16_t k = 2;
6359
while (max > 1) {
6460
k++;
6561
max = max >> 1;
6662
}
6763

6864
return k;
6965
}
66+
67+
int dl_array_max_q_f32(const float *x, int size, float eps)
68+
{
69+
float max = 0;
70+
for (int i = 1; i < size; i++) {
71+
if (x[i] > max) {
72+
max = x[i];
73+
} else if (-x[i] > max) {
74+
max = -x[i];
75+
}
76+
}
77+
int max_int = ceilf(max + eps);
78+
79+
return dl_power_of_two(max_int);
80+
}
81+
82+
int dl_float_to_short(const float *x, int len, int16_t *y, int out_exponent)
83+
{
84+
int exponent = out_exponent - dl_array_max_q_f32(x, len, 1e-8);
85+
float scale = powf(2, exponent);
86+
87+
for (int i = 0; i < len; i++) {
88+
y[i] = (int16_t)roundf(x[i] * scale);
89+
}
90+
91+
return -exponent;
92+
}

tools/dl_fft/base/dl_fft_base.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ bool dl_is_power_of_two(int x);
1919
int dl_power_of_two(uint32_t n);
2020
float *dl_short_to_float(const int16_t *x, int len, int exponent, float *y);
2121
int16_t dl_array_max_q_s16(const int16_t *x, int size);
22+
int dl_float_to_short(const float *x, int len, int16_t *y, int out_exponent);
2223

2324
// float fftr2
2425
float *dl_gen_fftr2_table_f32(int fft_point, uint32_t caps);

tools/dl_fft/idf_component.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
version: "0.1.0"
1+
version: "0.2.0"
22
license: "MIT"
33
description: dl_fft is a lightweight and efficient fft library for all espressif chips.
44
url: https://github.com/espressif/esp-dl/tree/master/esp-dl/tools/dl_fft
55
dependencies:
6-
idf: ">=5.0"
6+
idf: ">=5.0"

0 commit comments

Comments
 (0)