@@ -19,8 +19,6 @@ static float window[FFT_N * 2] = {0.0};
1919static float complex root [FFT_N ] = {0.0 };
2020static float complex data [FFT_N ] = {0.0 };
2121
22- static float xscale [BAND_N + 1 ] = {0.0 };
23-
2422static bool generated = false;
2523
2624static int bit_reverse (int x )
@@ -51,14 +49,9 @@ static void compute_fft_tables(void)
5149 for (int i = 0 ; i < FFT_N ; i ++ ) {
5250 root [i ] = cexpf (- I * i * TWO_PI / (FFT_N * 2 ));
5351 }
54-
55- // log xscale
56- for (int i = 0 ; i <= BAND_N ; i ++ ) {
57- xscale [i ] = powf (FFT_N , (float )i / BAND_N ) - 0.5 ;
58- }
5952}
6053
61- static float compute_freq_lin (const float * freq , int step , int idx )
54+ static float compute_freq_lin (const float * freq , uint16_t step , uint16_t idx )
6255{
6356 float n = 0.0 ;
6457
@@ -69,7 +62,7 @@ static float compute_freq_lin(const float *freq, int step, int idx)
6962 return n / step * 2.0 ;
7063}
7164
72- static float compute_freq_band (const float * freq , const float * xscale , int band )
65+ static float compute_freq_band (const float * freq , const float * xscale , uint16_t bands , uint16_t band )
7366{
7467 float n = 0.0 ;
7568
@@ -92,13 +85,13 @@ static float compute_freq_band(const float *freq, const float *xscale, int band)
9285 }
9386 }
9487
95- return 20 * log10f (n * BAND_N / FFT_N / 12.0 );
88+ return 20 * log10f (n * bands / FFT_N / 12.0 );
9689}
9790
98- void fft_compute_lin (uint16_t * data_out , uint16_t max_val , uint16_t min_val )
91+ void fft_compute_lin (uint16_t * data_out , uint16_t num , uint16_t step , uint16_t max_val , uint16_t min_val )
9992{
100- for (int i = 0 ; i < FFT_OUT_N ; i ++ ) {
101- data_out [i ] += compute_freq_lin (freq , FFT_N / FFT_OUT_N , i ) * (max_val / 40.0 );
93+ for (int i = 0 ; i < num ; i ++ ) {
94+ data_out [i ] += compute_freq_lin (freq , step , i ) * (max_val / 40.0 );
10295 data_out [i ] /= 2.0 ;
10396
10497 if (data_out [i ] > max_val ) {
@@ -109,10 +102,10 @@ void fft_compute_lin(uint16_t *data_out, uint16_t max_val, uint16_t min_val)
109102 }
110103}
111104
112- void fft_compute_log (uint16_t * data_out , uint16_t max_val , uint16_t min_val )
105+ void fft_compute_log (uint16_t * data_out , uint16_t num , uint16_t step , uint16_t max_val , uint16_t min_val )
113106{
114- for (int i = 0 ; i < FFT_OUT_N ; i ++ ) {
115- data_out [i ] += 20 * log10f (1 + compute_freq_lin (freq , FFT_N / FFT_OUT_N , i )) * (max_val / 40.0 );
107+ for (int i = 0 ; i < num ; i ++ ) {
108+ data_out [i ] += 20 * log10f (1 + compute_freq_lin (freq , step , i )) * (max_val / 40.0 );
116109 data_out [i ] /= 2.0 ;
117110
118111 if (data_out [i ] > max_val ) {
@@ -123,12 +116,12 @@ void fft_compute_log(uint16_t *data_out, uint16_t max_val, uint16_t min_val)
123116 }
124117}
125118
126- void fft_compute_bands (uint16_t * data_out , uint16_t max_val , uint16_t min_val )
119+ void fft_compute_bands (uint16_t * data_out , const float * xscale , uint16_t bands , uint16_t * delay , uint16_t max_val , uint16_t min_val )
127120{
128- static char delay [ BAND_N ] = { 0 };
121+ enum { BAND_FADE = 2 , BAND_DELAY = 2 };
129122
130- for (int i = 0 ; i < BAND_N ; i ++ ) {
131- float x = (40 + compute_freq_band (freq , xscale , i )) * (max_val / 64.0 );
123+ for (int i = 0 ; i < bands ; i ++ ) {
124+ float x = (40 + compute_freq_band (freq , xscale , bands , i )) * (max_val / 64.0 );
132125
133126 data_out [i ] = MAX (0 , data_out [i ] - (BAND_FADE - delay [i ]));
134127
@@ -137,7 +130,7 @@ void fft_compute_bands(uint16_t *data_out, uint16_t max_val, uint16_t min_val)
137130 }
138131
139132 if (x > data_out [i ]) {
140- data_out [i ]= x ;
133+ data_out [i ] = x ;
141134
142135 delay [i ] = BAND_DELAY ;
143136 }
@@ -150,6 +143,13 @@ void fft_compute_bands(uint16_t *data_out, uint16_t max_val, uint16_t min_val)
150143 }
151144}
152145
146+ void fft_compute_xscale (float * xscale , uint16_t bands )
147+ {
148+ for (int i = 0 ; i <= bands ; i ++ ) {
149+ xscale [i ] = powf (FFT_N , (float )i / bands ) - 0.5 ;
150+ }
151+ }
152+
153153void fft_execute (float scale_factor )
154154{
155155 float complex even = 0 , odd = 0 ;
0 commit comments