#define FFT_SIZE 256 // Must be power of 2
#define FFT_BINS (FFT_SIZE / 2) // = 128 output bins
int16_t fft_input[FFT_SIZE]; // Time domain
uint16_t fft_output[FFT_BINS]; // Frequency domain (magnitude)
/*=========================================================================
* IMPORTANT: Without pad_column, bars will appear as solid block!
*=========================================================================*/
/* WRONG: Bars merge into solid block */
ex6_fft_chart = lv_chart_create(scr);
lv_chart_set_type(ex6_fft_chart, LV_CHART_TYPE_BAR);
/* Missing pad_column = solid fill */
/* CORRECT: Individual bars with spacing */
ex6_fft_chart = lv_chart_create(scr);
lv_chart_set_type(ex6_fft_chart, LV_CHART_TYPE_BAR);
lv_obj_set_style_pad_column(ex6_fft_chart, 2, 0); /* 2px spacing */
/*=========================================================================
* Use DEDICATED variables for each example
* Avoid sharing static variables between examples
*=========================================================================*/
/* BAD: Shared variable may conflict with other examples */
static lv_obj_t *main_chart = NULL; /* Used by multiple examples */
/* GOOD: Dedicated variable for this example only */
static lv_obj_t *ex6_fft_chart = NULL; /* Only used in Ex6 */
/* FFT Size affects:
* - Frequency resolution: Higher size = better resolution
* - Processing time: Higher size = more computation
* - Latency: Higher size = more samples needed
*
* Common sizes:
* 128: Low resolution, fast (speech)
* 256: Good balance
* 512: Higher resolution
* 1024: High resolution, slow (music)
*/
/*=========================================================================
* Decimate FFT bins for better bar visibility
* FFT_BINS = 128 (too many for display)
* FFT_CHART_BINS = 64 (better visibility)
*=========================================================================*/
#define FFT_CHART_BINS 64
for (int i = 0; i < FFT_CHART_BINS; i++) {
/* Sample every 2nd bin */
int idx = i * (FFT_BINS / FFT_CHART_BINS);
int32_t val = fft_output[idx];
lv_chart_set_value_by_id(chart, series, i, val);
}
/* Gain slider allows user to adjust display sensitivity */
static uint8_t fft_gain = 50; /* Default 50% */
/* Apply gain in timer callback */
int32_t val = (fft_output[idx] * 100 * fft_gain) / (fft_max * 50);
if (val > 100) val = 100; /* Clamp to max */