Skip to content

Commit 7af7812

Browse files
committed
fft: update compute_freq_lin()
1 parent 86ad955 commit 7af7812

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

main/src/user/fft.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
static int bitrev[FFT_N] = {0.0};
1616
static float window[FFT_N * 2] = {0.0};
17+
static float xscale[BAND_N + 1] = {0.0};
1718
static float complex data[FFT_N] = {0.0};
1819
static float complex root[FFT_N / 2] = {0.0};
1920

20-
static bool generated = false;
21-
static float xscale[BAND_N + 1] = {0.0};
21+
static bool generated = false;
2222

2323
static int bit_reverse(int x)
2424
{
@@ -44,17 +44,26 @@ static void compute_fft_tables(void)
4444
window[i] = 0.53836 - 0.46164 * cosf(i * TWO_PI / (FFT_N * 2 - 1));
4545
}
4646

47+
// log xscale
48+
for (int i = 0; i <= BAND_N; i++) {
49+
xscale[i] = powf(FFT_N, (float)i / BAND_N) - 0.5;
50+
}
51+
4752
// twiddle factor
4853
for (int i = 0; i < FFT_N / 2; i++) {
4954
root[i] = cexpf(-i * TWO_PI / (FFT_N - 1) * I);
5055
}
5156
}
5257

53-
static void compute_log_xscale(float *xscale, int bands)
58+
static float compute_freq_lin(const float *freq, int step, int idx)
5459
{
55-
for (int i = 0; i <= bands; i++) {
56-
xscale[i] = powf(FFT_N, (float)i / bands) - 0.5;
60+
float n = 0.0;
61+
62+
for (int i = 0; i < step; i++) {
63+
n += freq[step * idx + i];
5764
}
65+
66+
return n / step * 2.0;
5867
}
5968

6069
static float compute_freq_band(const float *freq, const float *xscale, int band)
@@ -92,7 +101,7 @@ void fft_compute_lin(uint16_t *data_out, uint16_t scale_factor, uint16_t max_val
92101
freq[0] /= 2.0;
93102

94103
for (int i = 0; i < FFT_OUT_N; i++) {
95-
data_out[i] += freq[FFT_N / FFT_OUT_N * i] * (max_val / 40.0);
104+
data_out[i] += compute_freq_lin(freq, FFT_N / FFT_OUT_N, i) * (max_val / 40.0);
96105
data_out[i] /= 2.0;
97106

98107
if (data_out[i] > max_val) {
@@ -115,7 +124,7 @@ void fft_compute_log(uint16_t *data_out, uint16_t scale_factor, uint16_t max_val
115124
freq[0] /= 2.0;
116125

117126
for (int i = 0; i < FFT_OUT_N; i++) {
118-
data_out[i] += 20 * log10f(1 + freq[FFT_N / FFT_OUT_N * i]) * (max_val / 40.0);
127+
data_out[i] += 20 * log10f(1 + compute_freq_lin(freq, FFT_N / FFT_OUT_N, i)) * (max_val / 40.0);
119128
data_out[i] /= 2.0;
120129

121130
if (data_out[i] > max_val) {
@@ -227,7 +236,6 @@ void fft_init(void)
227236

228237
if (!generated) {
229238
compute_fft_tables();
230-
compute_log_xscale(xscale, BAND_N);
231239

232240
generated = true;
233241
}

0 commit comments

Comments
 (0)