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

演示参考下方视频

介绍

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

电路图

源码

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);
		}
	}
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌入式基地

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值