esp32使用fft算法显示音乐频谱

演示参考下方视频

  • 源码链接在视频末尾获取
  • 点击查看视频

介绍

  • 本项目使用esp32s3,ifd4.2,声音传感器以及tft显示屏构成
  • 使用快速傅里叶变换算法处理采集到的音频数据
  • fft处理后的数据使用lvgl显示在tft屏幕上

电路图

esp32使用fft算法显示音乐频谱_第1张图片

源码

void FFT_Task(void *arg)
{
	static int8_t i2s_readraw_buff[1024];
	size_t bytesread;
	int16_t *buffptr;
	int16_t *samples_sc16 = (int16_t *)i2s_readraw_buff;
	float *samples_fc32 = (float *)calloc(SAMPLES_NUM, sizeof(float));
	double data = 0;
	i2s_init();

	while (1)
	{
		if (fft_en == 1)
		{

			i2s_read(I2S_NUM_0, (char *)i2s_readraw_buff, SAMPLES_NUM * 2, &bytesread, (100 / portTICK_RATE_MS));
			
			fft_config_t *real_fft_plan = fft_init(512, FFT_REAL, FFT_FORWARD, NULL, NULL);
			buffptr = (int16_t *)i2s_readraw_buff;
			for (uint16_t count_n = 0; count_n < real_fft_plan->size; count_n++)
			{
				real_fft_plan->input[count_n] = (float)map(buffptr[count_n], INT16_MIN, INT16_MAX, -1000, 1000);
			}
			fft_execute(real_fft_plan);
			for (uint16_t count_n = 1; count_n < CANVAS_HEIGHT; count_n++)
			{
				data = sqrt(real_fft_plan->output[2 * count_n] * real_fft_plan->output[2 * count_n] + real_fft_plan->output[2 * count_n + 1] * real_fft_plan->output[2 * count_n + 1]);
				fft_dis_buff[CANVAS_HEIGHT - count_n] = map(data, 0, 2000, 0, CANVAS_HEIGHT);
			}
			fft_destroy(real_fft_plan);

			for (uint16_t count_y = 0; count_y < CANVAS_HEIGHT;)
			{
				lv_chart_set_next(chart_fft, series_fft, fft_dis_buff[count_y]);
				count_y += 5;
			}
		}
		else
		{
			i2s_driver_uninstall(I2S_NUM_0);
			vTaskDelete(NULL);
		}
	}
}

你可能感兴趣的:(esp32,算法,esp32,fft,tft)