arduinofft.h
时间: 2023-09-25 08:09:12 浏览: 168
arduinofft.h 是一个 Arduino 库的头文件。该库提供了一些函数和方法来实现 Arduino 上的快速傅里叶变换(FFT)。快速傅里叶变换是一种用于将信号从时域转换到频域的算法,它可以分析信号的频率成分。通过使用 arduinofft.h 库,你可以在 Arduino 上实现 FFT 算法,从而对信号进行频域分析。这个库可以帮助你在 Arduino 项目中进行音频处理、频谱分析等任务。你可以通过引入这个头文件并调用其中的函数来使用该库。
相关问题
#include "arduinoFFT.h" arduinoFFT FFT = arduinoFFT(); const uint16_t samples = 64; const double signalFrequency = 1000; const double samplingFrequency = 5000; const uint8_t amplitude = 100; double vReal[samples]; double vImag[samples]; #define SCL_INDEX 0x00 #define SCL_TIME 0x01 #define SCL_FREQUENCY 0x02 #define SCL_PLOT 0x03 void setup() { Serial.begin(115200); while(!Serial); Serial.println("Ready"); } void loop() { double cycles = (((samples-1) * signalFrequency) / samplingFrequency); for (uint16_t i = 0; i < samples; i++) { vReal[i] = int8_t((amplitude * (sin((i * (twoPi * cycles)) / samples))) / 2.0); vImag[i] = 0.0; } Serial.println("Data:"); PrintVector(vReal, samples, SCL_TIME); FFT.Windowing(vReal, samples, FFT_WIN_TYP_HAMMING, FFT_FORWARD); /* Weigh data */ Serial.println("Weighed data:"); PrintVector(vReal, samples, SCL_TIME); FFT.Compute(vReal, vImag, samples, FFT_FORWARD); /* Compute FFT */ Serial.println("Computed Real values:"); PrintVector(vReal, samples, SCL_INDEX); Serial.println("Computed Imaginary values:"); PrintVector(vImag, samples, SCL_INDEX); FFT.ComplexToMagnitude(vReal, vImag, samples); /* Compute magnitudes */ Serial.println("Computed magnitudes:"); PrintVector(vReal, (samples >> 1), SCL_FREQUENCY); double x; double v; FFT.MajorPeak(vReal, samples, samplingFrequency, &x, &v); Serial.print(x, 6); Serial.print(", "); Serial.println(v, 6); while(1); } void PrintVector(double *vData, uint16_t bufferSize, uint8_t scaleType) { for (uint16_t i = 0; i < bufferSize; i++) { double abscissa; switch (scaleType) { case SCL_INDEX: abscissa = (i * 1.0); break; case SCL_TIME: abscissa = ((i * 1.0) / samplingFrequency); break; case SCL_FREQUENCY: abscissa = ((i * 1.0 * samplingFrequency) / samples); break; } Serial.print(abscissa, 6); if(scaleType==SCL_FREQUENCY) Serial.print("Hz"); Serial.print(" "); Serial.println(vData[i], 4); } Serial.println(); } 根据此历程中fft转换的方法修改oled代码
根据你提供的代码,你想要修改 OLED 显示代码来显示 FFT 转换的结果。我已经修改了代码,添加了 OLED 显示的相关部分。请注意,这里假设你已经连接了 OLED 显示屏并正确配置了 Arduino 板。
```c++
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "arduinoFFT.h"
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
arduinoFFT FFT = arduinoFFT();
const uint16_t samples = 64;
const double signalFrequency = 1000;
const double samplingFrequency = 5000;
const uint8_t amplitude = 100;
double vReal[samples];
double vImag[samples];
void setup()
{
Serial.begin(115200);
while (!Serial)
;
Serial.println("Ready");
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
}
void loop()
{
double cycles = (((samples - 1) * signalFrequency) / samplingFrequency);
for (uint16_t i = 0; i < samples; i++)
{
vReal[i] = int8_t((amplitude * (sin((i * (twoPi * cycles)) / samples))) / 2.0);
vImag[i] = 0.0;
}
display.clearDisplay();
Serial.println("Data:");
PrintVector(vReal, samples, SCL_TIME);
FFT.Windowing(vReal, samples, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
Serial.println("Weighed data:");
PrintVector(vReal, samples, SCL_TIME);
FFT.Compute(vReal, vImag, samples, FFT_FORWARD);
Serial.println("Computed Real values:");
PrintVector(vReal, samples, SCL_INDEX);
Serial.println("Computed Imaginary values:");
PrintVector(vImag, samples, SCL_INDEX);
FFT.ComplexToMagnitude(vReal, vImag, samples);
Serial.println("Computed magnitudes:");
PrintVector(vReal, (samples >> 1), SCL_FREQUENCY);
double x;
double v;
FFT.MajorPeak(vReal, samples, samplingFrequency, &x, &v);
Serial.print(x, 6);
Serial.print(", ");
Serial.println(v, 6);
// 显示 FFT 转换的结果
display.setCursor(0, 0);
display.print("Frequency: ");
display.println(x, 6);
display.print("Magnitude: ");
display.println(v, 6);
display.display();
while (1)
;
}
void PrintVector(double *vData, uint16_t bufferSize, uint8_t scaleType)
{
for (uint16_t i = 0; i < bufferSize; i++)
{
double abscissa;
switch (scaleType)
{
case SCL_INDEX:
abscissa = (i * 1.0);
break;
case SCL_TIME:
abscissa = ((i * 1.0) / samplingFrequency);
break;
case SCL_FREQUENCY:
abscissa = ((i * 1.0 * samplingFrequency) / samples);
break;
}
Serial.print(abscissa, 6);
if (scaleType == SCL_FREQUENCY)
Serial.print("Hz");
Serial.print(" ");
Serial.println(vData[i], 4);
}
Serial.println();
}
```
在这个修改后的代码中,我添加了 OLED 显示的相关部分。在 `loop` 函数中,我们在每次进行 FFT 转换后,通过 `display` 对象显示频率和幅值的结果。
请确保你已经连接了 OLED 显示屏,并将其引脚与 Arduino 板正确连接。如果需要,你可以根据 OLED 显示屏的具体型号和引脚配置进行相应的修改。
希望这个修改后的代码能够满足你的需求!如果你还有其他问题,请随时提问。
阅读全文