2018-02-07 08:04:17 +00:00
|
|
|
/*
|
|
|
|
|
2024-04-15 14:21:59 +00:00
|
|
|
Example of use of the FFT library
|
2020-02-19 16:15:49 +00:00
|
|
|
|
|
|
|
Copyright (C) 2018 Enrique Condes
|
2024-03-06 05:56:17 +00:00
|
|
|
Copyright (C) 2020 Bim Overbohm (template, speed improvements)
|
2018-02-07 08:04:17 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
In this example, the Arduino simulates the sampling of a sinusoidal 1000 Hz
|
|
|
|
signal with an amplitude of 100, sampled at 5000 Hz. Samples are stored
|
|
|
|
inside the vReal array. The samples are windowed according to Hamming
|
|
|
|
function. The FFT is computed using the windowed samples. Then the magnitudes
|
|
|
|
of each of the frequencies that compose the signal are calculated. Finally,
|
|
|
|
the frequency spectrum magnitudes are printed. If you use the Arduino IDE
|
|
|
|
serial plotter, you will see a single spike corresponding to the 1000 Hz
|
2021-12-09 11:42:40 +00:00
|
|
|
frequency.
|
2018-02-07 08:04:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "arduinoFFT.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
These values can be changed in order to evaluate the functions
|
|
|
|
*/
|
|
|
|
const uint16_t samples = 64; //This value MUST ALWAYS be a power of 2
|
|
|
|
const double signalFrequency = 1000;
|
|
|
|
const double samplingFrequency = 5000;
|
|
|
|
const uint8_t amplitude = 100;
|
2020-02-19 16:15:49 +00:00
|
|
|
|
2018-02-07 08:04:17 +00:00
|
|
|
/*
|
|
|
|
These are the input and output vectors
|
|
|
|
Input vectors receive computed results from FFT
|
|
|
|
*/
|
|
|
|
double vReal[samples];
|
|
|
|
double vImag[samples];
|
|
|
|
|
2020-02-19 16:15:49 +00:00
|
|
|
ArduinoFFT<double> FFT = ArduinoFFT<double>(vReal, vImag, samples, samplingFrequency);
|
|
|
|
|
2018-02-10 19:21:10 +00:00
|
|
|
#define SCL_INDEX 0x00
|
|
|
|
#define SCL_TIME 0x01
|
|
|
|
#define SCL_FREQUENCY 0x02
|
|
|
|
#define SCL_PLOT 0x03
|
|
|
|
|
2018-02-07 08:04:17 +00:00
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
Serial.begin(115200);
|
2020-10-06 19:47:01 +00:00
|
|
|
while(!Serial);
|
|
|
|
Serial.println("Ready");
|
2018-02-07 08:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
/* Build raw data */
|
2024-03-06 05:56:17 +00:00
|
|
|
double ratio = twoPi * signalFrequency / samplingFrequency; // Fraction of a complete cycle stored at each sample (in radians)
|
2018-02-07 08:04:17 +00:00
|
|
|
for (uint16_t i = 0; i < samples; i++)
|
|
|
|
{
|
2024-03-06 05:56:17 +00:00
|
|
|
vReal[i] = int8_t(amplitude * sin(i * ratio) / 2.0);/* Build data with positive and negative values*/
|
|
|
|
//vReal[i] = uint8_t((amplitude * (sin(i * ratio) + 1.0)) / 2.0);/* Build data displaced on the Y axis to include only positive values*/
|
2018-02-07 08:04:17 +00:00
|
|
|
vImag[i] = 0.0; //Imaginary part must be zeroed in case of looping to avoid wrong calculations and overflows
|
|
|
|
}
|
2020-02-19 16:15:49 +00:00
|
|
|
FFT.windowing(FFTWindow::Hamming, FFTDirection::Forward); /* Weigh data */
|
|
|
|
FFT.compute(FFTDirection::Forward); /* Compute FFT */
|
|
|
|
FFT.complexToMagnitude(); /* Compute magnitudes */
|
2018-02-10 19:21:10 +00:00
|
|
|
PrintVector(vReal, samples>>1, SCL_PLOT);
|
2020-02-19 16:15:49 +00:00
|
|
|
double x = FFT.majorPeak();
|
2018-02-07 08:04:17 +00:00
|
|
|
while(1); /* Run Once */
|
|
|
|
// delay(2000); /* Repeat after delay */
|
|
|
|
}
|
2018-02-10 19:21:10 +00:00
|
|
|
|
|
|
|
void PrintVector(double *vData, uint16_t bufferSize, uint8_t scaleType)
|
|
|
|
{
|
|
|
|
for (uint16_t i = 0; i < bufferSize; i++)
|
|
|
|
{
|
|
|
|
double abscissa;
|
|
|
|
/* Print abscissa value */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if(scaleType!=SCL_PLOT)
|
|
|
|
{
|
|
|
|
Serial.print(abscissa, 6);
|
2018-02-10 19:59:45 +00:00
|
|
|
if(scaleType==SCL_FREQUENCY)
|
|
|
|
Serial.print("Hz");
|
2018-02-10 19:21:10 +00:00
|
|
|
Serial.print(" ");
|
|
|
|
}
|
|
|
|
Serial.println(vData[i], 4);
|
|
|
|
}
|
|
|
|
Serial.println();
|
|
|
|
}
|