Merge pull request #95 from kosme/develop

Version 2.0.2. Patch for issues 92/93
master v2.0.2
Enrique Condes 2024-04-18 17:13:59 +08:00 zatwierdzone przez GitHub
commit 0da88512f9
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
5 zmienionych plików z 62 dodań i 50 usunięć

Wyświetl plik

@ -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"

Wyświetl plik

@ -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=*

Wyświetl plik

@ -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;
}
}
@ -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 {
@ -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
@ -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];
}
@ -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
@ -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>;

Wyświetl plik

@ -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
@ -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:
@ -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;

43
src/enumsFFT.h 100644
Wyświetl plik

@ -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