From 4b2c3c12e0c257c6520d45a014b8cafead0172e8 Mon Sep 17 00:00:00 2001 From: Enrique Condes Date: Thu, 11 May 2017 13:29:01 -0500 Subject: [PATCH] New example. Obtain frequency of a signal sampled through the ADC. --- Examples/FFT_03/FFT_03.ino | 108 +++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 Examples/FFT_03/FFT_03.ino diff --git a/Examples/FFT_03/FFT_03.ino b/Examples/FFT_03/FFT_03.ino new file mode 100644 index 0000000..4917b9b --- /dev/null +++ b/Examples/FFT_03/FFT_03.ino @@ -0,0 +1,108 @@ +/* + + Example of use of the FFT libray to compute FFT for a signal sampled through the ADC. + Copyright (C) 2017 Enrique Condes + + 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 . + +*/ + +#include "arduinoFFT.h" + +arduinoFFT FFT = arduinoFFT(); /* Create FFT object */ +/* +These values can be changed in order to evaluate the functions +*/ +#define CHANNEL A0 +const uint16_t samples = 64; //This value MUST ALWAYS be a power of 2 +double samplingFrequency = 200; + +unsigned int delayTime = 0; + +/* +These are the input and output vectors +Input vectors receive computed results from FFT +*/ +double vReal[samples]; +double vImag[samples]; + +#define SCL_INDEX 0x00 +#define SCL_TIME 0x01 +#define SCL_FREQUENCY 0x02 + +void setup() +{ + if(samplingFrequency<=1000) + delayTime = 1000/samplingFrequency; + else + delayTime = 1000000/samplingFrequency; + Serial.begin(115200); + Serial.println("Ready"); +} + +void loop() +{ + for(uint16_t i =0;i> 1), SCL_FREQUENCY); + double x = FFT.MajorPeak(vReal, samples, samplingFrequency); + Serial.println(x, 6); + while(1); /* Run Once */ + +} + +void PrintVector(double *vData, uint8_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; + } + Serial.print(abscissa, 6); + Serial.print(" "); + Serial.print(vData[i], 4); + Serial.println(); + } + Serial.println(); +}