From 04fbc35bda908d83dfc6bdd3cba808d62f8f532b Mon Sep 17 00:00:00 2001 From: Enrique Condes Date: Thu, 18 Apr 2024 17:12:43 +0800 Subject: [PATCH] Version 2.0.2. Patch for issue 92/93 --- library.json | 2 +- library.properties | 6 +++--- src/arduinoFFT.cpp | 17 ++++++++++++----- src/arduinoFFT.h | 44 +++----------------------------------------- src/enumsFFT.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 50 deletions(-) create mode 100644 src/enumsFFT.h diff --git a/library.json b/library.json index cca7541..92d9748 100644 --- a/library.json +++ b/library.json @@ -25,7 +25,7 @@ "email": "bim.overbohm@googlemail.com" } ], - "version": "2.0.1", + "version": "2.0.2", "frameworks": ["arduino","mbed","espidf"], "platforms": "*", "headers": "arduinoFFT.h" diff --git a/library.properties b/library.properties index 176092a..c31b794 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,9 @@ name=arduinoFFT -version=2.0.1 +version=2.0.2 author=Enrique Condes maintainer=Enrique Condes -sentence=A library for implementing floating point Fast Fourier Transform calculations on Arduino. -paragraph=With this library you can calculate the frequency of a sampled signal. +sentence=A library for implementing floating point Fast Fourier Transform calculations on the Arduino framework. +paragraph=With this library you can calculate the dominant frequency of a sampled signal. category=Data Processing url=https://github.com/kosme/arduinoFFT architectures=* diff --git a/src/arduinoFFT.cpp b/src/arduinoFFT.cpp index 1bb5b22..c49fb76 100644 --- a/src/arduinoFFT.cpp +++ b/src/arduinoFFT.cpp @@ -40,7 +40,7 @@ ArduinoFFT::ArduinoFFT(T *vReal, T *vImag, uint_fast16_t samples, template ArduinoFFT::~ArduinoFFT(void) { // Destructor if (_precompiledWindowingFactors) { - delete [] _precompiledWindowingFactors; + delete[] _precompiledWindowingFactors; } } @@ -142,6 +142,10 @@ void ArduinoFFT::compute(T *vReal, T *vImag, uint_fast16_t samples, #endif } } + // The computation result at position 0 should be as close to 0 as possible. + // The DC offset on the signal produces a spike on position 0 that should be + // eliminated to avoid issues. + vReal[0] = 0; } template void ArduinoFFT::dcRemoval(void) const { @@ -247,7 +251,7 @@ void ArduinoFFT::majorPeakParabola(T *vData, uint_fast16_t samples, // And magnitude is at the extrema of the parabola if you want It... if (magnitude != nullptr) { - *magnitude = a * x * x + b * x + c; + *magnitude = (a * x * x) + (b * x) + c; } // Convert to frequency @@ -270,7 +274,7 @@ void ArduinoFFT::setArrays(T *vReal, T *vImag, uint_fast16_t samples) { _oneOverSamples = 1.0 / samples; #endif if (_precompiledWindowingFactors) { - delete [] _precompiledWindowingFactors; + delete[] _precompiledWindowingFactors; } _precompiledWindowingFactors = new T[samples / 2]; } @@ -501,7 +505,7 @@ template double ArduinoFFT::sqrt_internal(double x) const { #endif template -const T ArduinoFFT::_WindowCompensationFactors[10] = { +const T ArduinoFFT::_WindowCompensationFactors[11] = { 1.0000000000 * 2.0, // rectangle (Box car) 1.8549343278 * 2.0, // hamming 1.8554726898 * 2.0, // hann @@ -511,7 +515,10 @@ const T ArduinoFFT::_WindowCompensationFactors[10] = { 2.7557840395 * 2.0, // blackman nuttall 2.7929062517 * 2.0, // blackman harris 3.5659039231 * 2.0, // flat top - 1.5029392863 * 2.0 // welch + 1.5029392863 * 2.0, // welch + // This is added as a precaution, since this index should never be + // accessed under normal conditions + 1.0 // Custom, precompiled value. }; template class ArduinoFFT; diff --git a/src/arduinoFFT.h b/src/arduinoFFT.h index e8e1069..2099291 100644 --- a/src/arduinoFFT.h +++ b/src/arduinoFFT.h @@ -41,6 +41,7 @@ #include #include #endif +#include "enumsFFT.h" // This definition uses a low-precision square root approximation instead of the // regular sqrt() call @@ -52,46 +53,7 @@ #endif #endif -enum class FFTDirection { Forward, Reverse }; - -enum class FFTWindow { - Rectangle, // rectangle (Box car) - Hamming, // hamming - Hann, // hann - Triangle, // triangle (Bartlett) - Nuttall, // nuttall - Blackman, // blackman - Blackman_Nuttall, // blackman nuttall - Blackman_Harris, // blackman harris - Flat_top, // flat top - Welch, // welch - Precompiled // Placeholder for using custom or precompiled window values -}; #define FFT_LIB_REV 0x20 -/* Custom constants */ -/* These defines keep compatibility with pre 2.0 code */ -#define FFT_FORWARD FFTDirection::Forward -#define FFT_REVERSE FFTDirection::Reverse - -/* Windowing type */ -#define FFT_WIN_TYP_RECTANGLE FFTWindow::Rectangle /* rectangle (Box car) */ -#define FFT_WIN_TYP_HAMMING FFTWindow::Hamming /* hamming */ -#define FFT_WIN_TYP_HANN FFTWindow::Hann /* hann */ -#define FFT_WIN_TYP_TRIANGLE FFTWindow::Triangle /* triangle (Bartlett) */ -#define FFT_WIN_TYP_NUTTALL FFTWindow::Nuttall /* nuttall */ -#define FFT_WIN_TYP_BLACKMAN FFTWindow::Blackman /* blackman */ -#define FFT_WIN_TYP_BLACKMAN_NUTTALL \ - FFTWindow::Blackman_Nuttall /* blackman nuttall */ -#define FFT_WIN_TYP_BLACKMAN_HARRIS \ - FFTWindow::Blackman_Harris /* blackman harris*/ -#define FFT_WIN_TYP_FLT_TOP FFTWindow::Flat_top /* flat top */ -#define FFT_WIN_TYP_WELCH FFTWindow::Welch /* welch */ -/* End of compatibility defines */ - -/* Mathematial constants */ -#define twoPi 6.28318531 -#define fourPi 12.56637061 -#define sixPi 18.84955593 template class ArduinoFFT { public: @@ -138,14 +100,14 @@ public: private: /* Variables */ - static const T _WindowCompensationFactors[10]; + static const T _WindowCompensationFactors[11]; #ifdef FFT_SPEED_OVER_PRECISION T _oneOverSamples = 0.0; #endif bool _isPrecompiled = false; bool _precompiledWithCompensation = false; uint_fast8_t _power = 0; - T *_precompiledWindowingFactors; + T *_precompiledWindowingFactors = nullptr; uint_fast16_t _samples; T _samplingFrequency; T *_vImag; diff --git a/src/enumsFFT.h b/src/enumsFFT.h new file mode 100644 index 0000000..d064639 --- /dev/null +++ b/src/enumsFFT.h @@ -0,0 +1,43 @@ +#ifndef enumsFFT_h +#define enumsFFT_h +/* Custom constants */ +/* These defines keep compatibility with pre 2.0 code */ +#define FFT_FORWARD FFTDirection::Forward +#define FFT_REVERSE FFTDirection::Reverse + +/* Windowing type */ +#define FFT_WIN_TYP_RECTANGLE FFTWindow::Rectangle /* rectangle (Box car) */ +#define FFT_WIN_TYP_HAMMING FFTWindow::Hamming /* hamming */ +#define FFT_WIN_TYP_HANN FFTWindow::Hann /* hann */ +#define FFT_WIN_TYP_TRIANGLE FFTWindow::Triangle /* triangle (Bartlett) */ +#define FFT_WIN_TYP_NUTTALL FFTWindow::Nuttall /* nuttall */ +#define FFT_WIN_TYP_BLACKMAN FFTWindow::Blackman /* blackman */ +#define FFT_WIN_TYP_BLACKMAN_NUTTALL \ + FFTWindow::Blackman_Nuttall /* blackman nuttall */ +#define FFT_WIN_TYP_BLACKMAN_HARRIS \ + FFTWindow::Blackman_Harris /* blackman harris*/ +#define FFT_WIN_TYP_FLT_TOP FFTWindow::Flat_top /* flat top */ +#define FFT_WIN_TYP_WELCH FFTWindow::Welch /* welch */ +/* End of compatibility defines */ + +/* Mathematial constants */ +#define twoPi 6.28318531 +#define fourPi 12.56637061 +#define sixPi 18.84955593 + +enum class FFTWindow { + Rectangle, // rectangle (Box car) + Hamming, // hamming + Hann, // hann + Triangle, // triangle (Bartlett) + Nuttall, // nuttall + Blackman, // blackman + Blackman_Nuttall, // blackman nuttall + Blackman_Harris, // blackman harris + Flat_top, // flat top + Welch, // welch + Precompiled // Placeholder for using custom or precompiled window values +}; + +enum class FFTDirection { Forward, Reverse }; +#endif \ No newline at end of file