arduino - 应用 FFT 后计算信号的幅度
问题描述
我正在使用 Arduino Due 和传感器来检测噪声,我应用 FFT 库来提取频率,它工作得很好。但我不知道如何计算幅度并在控制台上打印它们?
这是代码:
#include "arduinoFFT.h"
#define SAMPLES 32
#define SAMPLING_FREQUENCY 1000
arduinoFFT FFT = arduinoFFT();
unsigned int sampling_period_us;
unsigned long microseconds;
double vReal[SAMPLES];
double vImag[SAMPLES];
void setup() {
Serial.begin(9600);
sampling_period_us = round(1000000*(1.0/SAMPLING_FREQUENCY));
}
void loop() {
/*SAMPLING*/
for(int i=0; i<SAMPLES; i++)
{
microseconds = micros();
vReal[i] = analogRead(0);
vImag[i] = 0;
while(micros() < (microseconds + sampling_period_us)){
}
}
/*FFT*/
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
Serial.println(peak);
for(int i=0; i<(SAMPLES/2); i++)
{
Serial.print((i * 1.0 * SAMPLING_FREQUENCY) / SAMPLES, 1);
Serial.print(" ");
//Serial.println(vReal[i], 1); //View only this line in serial plotter to visualize the bins
}
delay(1000);
while(1);
}
解决方案
串行绘图仪没有大量的样品来绘图功能。
如果要正确查看每个窗口的幅度,则需要使用以下代码
for(int i=0; i<(SAMPLES/2); i++)
{
// Serial.print((i * 1.0 * SAMPLING_FREQUENCY) / SAMPLES, 1);
for(int i=0; i<20; i++)//Adjust the 20 to make space between each samples.
Serial.print(0);
Serial.print(vReal[i]);
}
然后调整串行绘图仪的大小以适合您的窗口。
我希望这有帮助。
推荐阅读
- powershell - 使用 OpenSLL 错误对文件夹和/或内容进行签名
- aws-sdk - 如何从 Java 对象生成 Recordio
- git - 有没有办法查看更改何时进入 Git 中的分支?
- java - Apache Beam / Google 数据流 - 错误处理
- java - 如何让 JUnit5 与 JDK10 (jigsaw) 和 Maven3 一起工作?
- python - 二进制搜索以在 Python 中找到可能的最高值
- reactjs - 如何将反应元素转换为 HTML
- json - 使用 gson 解析器将 Json 文件解析为 java 对象时出错
- sql - SQL根据状态和计数选择具有特定条件的行
- java - 如何保证所有 Spark 工作人员都已停止?