Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 2.0.2. Patch for issues 92/93 #95

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=arduinoFFT
version=2.0.1
version=2.0.2
author=Enrique Condes <enrique@shapeoko.com>
maintainer=Enrique Condes <enrique@shapeoko.com>
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=*
Expand Down
17 changes: 12 additions & 5 deletions src/arduinoFFT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ArduinoFFT<T>::ArduinoFFT(T *vReal, T *vImag, uint_fast16_t samples,
template <typename T> ArduinoFFT<T>::~ArduinoFFT(void) {
// Destructor
if (_precompiledWindowingFactors) {
delete [] _precompiledWindowingFactors;
delete[] _precompiledWindowingFactors;
}
}

Expand Down Expand Up @@ -142,6 +142,10 @@ void ArduinoFFT<T>::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 <typename T> void ArduinoFFT<T>::dcRemoval(void) const {
Expand Down Expand Up @@ -247,7 +251,7 @@ void ArduinoFFT<T>::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
Expand All @@ -270,7 +274,7 @@ void ArduinoFFT<T>::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];
}
Expand Down Expand Up @@ -501,7 +505,7 @@ template <typename T> double ArduinoFFT<T>::sqrt_internal(double x) const {
#endif

template <typename T>
const T ArduinoFFT<T>::_WindowCompensationFactors[10] = {
const T ArduinoFFT<T>::_WindowCompensationFactors[11] = {
1.0000000000 * 2.0, // rectangle (Box car)
1.8549343278 * 2.0, // hamming
1.8554726898 * 2.0, // hann
Expand All @@ -511,7 +515,10 @@ const T ArduinoFFT<T>::_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<double>;
Expand Down
44 changes: 3 additions & 41 deletions src/arduinoFFT.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <math.h>
#include <stdint.h>
#endif
#include "enumsFFT.h"

// This definition uses a low-precision square root approximation instead of the
// regular sqrt() call
Expand All @@ -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 <typename T> class ArduinoFFT {
public:
Expand Down Expand Up @@ -138,14 +100,14 @@ template <typename T> class ArduinoFFT {

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;
Expand Down
43 changes: 43 additions & 0 deletions src/enumsFFT.h
Original file line number Diff line number Diff line change
@@ -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