Skip to content

Commit c2a7311

Browse files
committed
Merge branch 'feat/fft_cpp_interface' into 'master'
feat: dl_fft add C++ interface See merge request ai/esp-dl!196
2 parents 189c5ec + 099c813 commit c2a7311

File tree

7 files changed

+573
-25
lines changed

7 files changed

+573
-25
lines changed

test_apps/dl_fft/main/test_dl_fft.cpp

Lines changed: 232 additions & 19 deletions
Large diffs are not rendered by default.

test_apps/dl_fft/main/test_fft.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,14 @@ bool check_fft_results(const float *x, const float *gt, int size, float snr_thre
5757
}
5858
return pass;
5959
}
60+
61+
bool check_is_same(const float *x, const float *gt, int size, float threshold)
62+
{
63+
for (int i = 0; i < size; i++) {
64+
if (fabs(x[i] - gt[i]) > threshold) {
65+
return false;
66+
}
67+
}
68+
printf("check is same pass\n");
69+
return true;
70+
}

test_apps/dl_fft/main/test_fft.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ float get_snr(const float *x, const float *gt, int size);
2727
float get_rmse(const float *x, const float *gt, int size);
2828

2929
bool check_fft_results(const float *x, const float *gt, int size, float snr_threshold, float rmse_threshold);
30+
31+
bool check_is_same(const float *x, const float *gt, int size, float threshold);
3032
#ifdef __cplusplus
3133
}
3234
#endif

tools/dl_fft/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(srcs "dl_fft_f32.c"
44
"dl_fft_s16.c"
55
"dl_rfft_f32.c"
66
"dl_rfft_s16.c"
7+
"dl_fft.hpp"
78
"base/dl_fft2r_fc32_ansi.c"
89
"base/dl_fft4r_fc32_ansi.c"
910
"base/dl_fft2r_sc16_ansi.c"

tools/dl_fft/README.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,32 @@ The float FFT implementation is come from esp-dsp. And we further optimized the
66
For int16 FFT, we recommend to use `dl_fft_s16_hp_run` or `dl_rfft_s16_hp_run` interface. `hp` means "high precision".
77

88
## Get Started
9+
10+
### C interface
911
```
1012
1113
#include "dl_fft.h"
1214
#include "dl_rfft.h"
1315
1416
// float fft
1517
float x[nfft*2];
18+
19+
float *x = (float *)heap_caps_aligned_alloc(16, nfft * sizeof(float) *2, MALLOC_CAP_8BIT);
1620
dl_fft_f32_t *fft_handle = dl_fft_f32_init(nfft, MALLOC_CAP_8BIT);
1721
dl_fft_f32_run(fft_handle, x);
1822
dl_ifft_f32_run(fft_handle, x);
1923
dl_fft_f32_deinit(fft_handle);
2024
2125
// float rfft
22-
float x[nfft];
26+
float *x = (float *)heap_caps_aligned_alloc(16, nfft * sizeof(float), MALLOC_CAP_8BIT);
2327
dl_fft_f32_t *fft_handle = dl_rfft_f32_init(nfft, MALLOC_CAP_8BIT);
2428
dl_rfft_f32_run(fft_handle, x);
2529
dl_irfft_f32_run(fft_handle, x);
2630
dl_rfft_f32_deinit(fft_handle);
2731
2832
// int16 fft
29-
int16_t x[nfft*2];
30-
float y[nfft*2];
33+
int16_t *x= (float *)heap_caps_aligned_alloc(16, nfft * sizeof(int16_t) * 2, MALLOC_CAP_8BIT);
34+
float *y = (float *)heap_caps_aligned_alloc(16, nfft * sizeof(float) *2, MALLOC_CAP_8BIT);
3135
int in_exponent = -15; // float y = x * 2^in_exponent;
3236
int fft_exponent;
3337
int ifft_exponent;
@@ -38,8 +42,8 @@ dl_short_to_float(x, nfft, ifft_exponent, y); // convert output from int16_t to
3842
dl_fft_s16_deinit(fft_handle);
3943
4044
// int16 rfft
41-
int16_t x[nfft];
42-
float y[nfft];
45+
int16_t *x= (float *)heap_caps_aligned_alloc(16, nfft * sizeof(int16_t), MALLOC_CAP_8BIT);
46+
float *y = (float *)heap_caps_aligned_alloc(16, nfft * sizeof(float), MALLOC_CAP_8BIT);
4347
int in_exponent = -15; // float y = x * 2^in_exponent;
4448
int fft_exponent;
4549
int ifft_exponent;
@@ -53,6 +57,32 @@ dl_rfft_s16_deinit(fft_handle);
5357
```
5458
Please refer to [dl_fft.h](./dl_fft.h) and [dl_rfft.h](./dl_rfft.h) for more details.
5559

60+
61+
### C++ interface:
62+
```
63+
float *x1 = (float *)heap_caps_aligned_alloc(16, nfft * sizeof(float) *2, MALLOC_CAP_8BIT);
64+
int16_t *x2= (float *)heap_caps_aligned_alloc(16, nfft * sizeof(int16_t)*2, MALLOC_CAP_8BIT);
65+
FFT *fft = FFT::get_instance();
66+
67+
# float
68+
fft->fft(x1, nfft);
69+
fft->ifft(x1, nfft);
70+
fft->rfft(x1, nfft);
71+
fft->irfft(x1, nfft);
72+
73+
#int16_t
74+
int in_exponent=-15;
75+
int out_exponent;
76+
fft->fft_hp(x2, nfft, in_exponent, &out_exponent);
77+
fft->ifft_hp(x2, nfft, in_exponent, &out_exponent);
78+
fft->rfft_hp(x2, nfft, in_exponent, &out_exponent);
79+
fft->irfft_hp(x2, nfft, in_exponent, &out_exponent);
80+
```
81+
Please refer to [dl_fft.hpp](./dl_fft.hpp) for more details.
82+
83+
> Note: The input array x must be allocated with heap_caps_aligned_alloc and aligned to 16 bytes.
84+
85+
5686
## FAQ:
5787

5888
#### 1. Why not just use esp-dsp directly?

0 commit comments

Comments
 (0)