Skip to content

Commit 04fbc35

Browse files
committed
Version 2.0.2. Patch for issue 92/93
1 parent 1936a46 commit 04fbc35

File tree

5 files changed

+62
-50
lines changed

5 files changed

+62
-50
lines changed

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"email": "bim.overbohm@googlemail.com"
2626
}
2727
],
28-
"version": "2.0.1",
28+
"version": "2.0.2",
2929
"frameworks": ["arduino","mbed","espidf"],
3030
"platforms": "*",
3131
"headers": "arduinoFFT.h"

library.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=arduinoFFT
2-
version=2.0.1
2+
version=2.0.2
33
author=Enrique Condes <enrique@shapeoko.com>
44
maintainer=Enrique Condes <enrique@shapeoko.com>
5-
sentence=A library for implementing floating point Fast Fourier Transform calculations on Arduino.
6-
paragraph=With this library you can calculate the frequency of a sampled signal.
5+
sentence=A library for implementing floating point Fast Fourier Transform calculations on the Arduino framework.
6+
paragraph=With this library you can calculate the dominant frequency of a sampled signal.
77
category=Data Processing
88
url=https://github.com/kosme/arduinoFFT
99
architectures=*

src/arduinoFFT.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ ArduinoFFT<T>::ArduinoFFT(T *vReal, T *vImag, uint_fast16_t samples,
4040
template <typename T> ArduinoFFT<T>::~ArduinoFFT(void) {
4141
// Destructor
4242
if (_precompiledWindowingFactors) {
43-
delete [] _precompiledWindowingFactors;
43+
delete[] _precompiledWindowingFactors;
4444
}
4545
}
4646

@@ -142,6 +142,10 @@ void ArduinoFFT<T>::compute(T *vReal, T *vImag, uint_fast16_t samples,
142142
#endif
143143
}
144144
}
145+
// The computation result at position 0 should be as close to 0 as possible.
146+
// The DC offset on the signal produces a spike on position 0 that should be
147+
// eliminated to avoid issues.
148+
vReal[0] = 0;
145149
}
146150

147151
template <typename T> void ArduinoFFT<T>::dcRemoval(void) const {
@@ -247,7 +251,7 @@ void ArduinoFFT<T>::majorPeakParabola(T *vData, uint_fast16_t samples,
247251

248252
// And magnitude is at the extrema of the parabola if you want It...
249253
if (magnitude != nullptr) {
250-
*magnitude = a * x * x + b * x + c;
254+
*magnitude = (a * x * x) + (b * x) + c;
251255
}
252256

253257
// Convert to frequency
@@ -270,7 +274,7 @@ void ArduinoFFT<T>::setArrays(T *vReal, T *vImag, uint_fast16_t samples) {
270274
_oneOverSamples = 1.0 / samples;
271275
#endif
272276
if (_precompiledWindowingFactors) {
273-
delete [] _precompiledWindowingFactors;
277+
delete[] _precompiledWindowingFactors;
274278
}
275279
_precompiledWindowingFactors = new T[samples / 2];
276280
}
@@ -501,7 +505,7 @@ template <typename T> double ArduinoFFT<T>::sqrt_internal(double x) const {
501505
#endif
502506

503507
template <typename T>
504-
const T ArduinoFFT<T>::_WindowCompensationFactors[10] = {
508+
const T ArduinoFFT<T>::_WindowCompensationFactors[11] = {
505509
1.0000000000 * 2.0, // rectangle (Box car)
506510
1.8549343278 * 2.0, // hamming
507511
1.8554726898 * 2.0, // hann
@@ -511,7 +515,10 @@ const T ArduinoFFT<T>::_WindowCompensationFactors[10] = {
511515
2.7557840395 * 2.0, // blackman nuttall
512516
2.7929062517 * 2.0, // blackman harris
513517
3.5659039231 * 2.0, // flat top
514-
1.5029392863 * 2.0 // welch
518+
1.5029392863 * 2.0, // welch
519+
// This is added as a precaution, since this index should never be
520+
// accessed under normal conditions
521+
1.0 // Custom, precompiled value.
515522
};
516523

517524
template class ArduinoFFT<double>;

src/arduinoFFT.h

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <math.h>
4242
#include <stdint.h>
4343
#endif
44+
#include "enumsFFT.h"
4445

4546
// This definition uses a low-precision square root approximation instead of the
4647
// regular sqrt() call
@@ -52,46 +53,7 @@
5253
#endif
5354
#endif
5455

55-
enum class FFTDirection { Forward, Reverse };
56-
57-
enum class FFTWindow {
58-
Rectangle, // rectangle (Box car)
59-
Hamming, // hamming
60-
Hann, // hann
61-
Triangle, // triangle (Bartlett)
62-
Nuttall, // nuttall
63-
Blackman, // blackman
64-
Blackman_Nuttall, // blackman nuttall
65-
Blackman_Harris, // blackman harris
66-
Flat_top, // flat top
67-
Welch, // welch
68-
Precompiled // Placeholder for using custom or precompiled window values
69-
};
7056
#define FFT_LIB_REV 0x20
71-
/* Custom constants */
72-
/* These defines keep compatibility with pre 2.0 code */
73-
#define FFT_FORWARD FFTDirection::Forward
74-
#define FFT_REVERSE FFTDirection::Reverse
75-
76-
/* Windowing type */
77-
#define FFT_WIN_TYP_RECTANGLE FFTWindow::Rectangle /* rectangle (Box car) */
78-
#define FFT_WIN_TYP_HAMMING FFTWindow::Hamming /* hamming */
79-
#define FFT_WIN_TYP_HANN FFTWindow::Hann /* hann */
80-
#define FFT_WIN_TYP_TRIANGLE FFTWindow::Triangle /* triangle (Bartlett) */
81-
#define FFT_WIN_TYP_NUTTALL FFTWindow::Nuttall /* nuttall */
82-
#define FFT_WIN_TYP_BLACKMAN FFTWindow::Blackman /* blackman */
83-
#define FFT_WIN_TYP_BLACKMAN_NUTTALL \
84-
FFTWindow::Blackman_Nuttall /* blackman nuttall */
85-
#define FFT_WIN_TYP_BLACKMAN_HARRIS \
86-
FFTWindow::Blackman_Harris /* blackman harris*/
87-
#define FFT_WIN_TYP_FLT_TOP FFTWindow::Flat_top /* flat top */
88-
#define FFT_WIN_TYP_WELCH FFTWindow::Welch /* welch */
89-
/* End of compatibility defines */
90-
91-
/* Mathematial constants */
92-
#define twoPi 6.28318531
93-
#define fourPi 12.56637061
94-
#define sixPi 18.84955593
9557

9658
template <typename T> class ArduinoFFT {
9759
public:
@@ -138,14 +100,14 @@ template <typename T> class ArduinoFFT {
138100

139101
private:
140102
/* Variables */
141-
static const T _WindowCompensationFactors[10];
103+
static const T _WindowCompensationFactors[11];
142104
#ifdef FFT_SPEED_OVER_PRECISION
143105
T _oneOverSamples = 0.0;
144106
#endif
145107
bool _isPrecompiled = false;
146108
bool _precompiledWithCompensation = false;
147109
uint_fast8_t _power = 0;
148-
T *_precompiledWindowingFactors;
110+
T *_precompiledWindowingFactors = nullptr;
149111
uint_fast16_t _samples;
150112
T _samplingFrequency;
151113
T *_vImag;

src/enumsFFT.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef enumsFFT_h
2+
#define enumsFFT_h
3+
/* Custom constants */
4+
/* These defines keep compatibility with pre 2.0 code */
5+
#define FFT_FORWARD FFTDirection::Forward
6+
#define FFT_REVERSE FFTDirection::Reverse
7+
8+
/* Windowing type */
9+
#define FFT_WIN_TYP_RECTANGLE FFTWindow::Rectangle /* rectangle (Box car) */
10+
#define FFT_WIN_TYP_HAMMING FFTWindow::Hamming /* hamming */
11+
#define FFT_WIN_TYP_HANN FFTWindow::Hann /* hann */
12+
#define FFT_WIN_TYP_TRIANGLE FFTWindow::Triangle /* triangle (Bartlett) */
13+
#define FFT_WIN_TYP_NUTTALL FFTWindow::Nuttall /* nuttall */
14+
#define FFT_WIN_TYP_BLACKMAN FFTWindow::Blackman /* blackman */
15+
#define FFT_WIN_TYP_BLACKMAN_NUTTALL \
16+
FFTWindow::Blackman_Nuttall /* blackman nuttall */
17+
#define FFT_WIN_TYP_BLACKMAN_HARRIS \
18+
FFTWindow::Blackman_Harris /* blackman harris*/
19+
#define FFT_WIN_TYP_FLT_TOP FFTWindow::Flat_top /* flat top */
20+
#define FFT_WIN_TYP_WELCH FFTWindow::Welch /* welch */
21+
/* End of compatibility defines */
22+
23+
/* Mathematial constants */
24+
#define twoPi 6.28318531
25+
#define fourPi 12.56637061
26+
#define sixPi 18.84955593
27+
28+
enum class FFTWindow {
29+
Rectangle, // rectangle (Box car)
30+
Hamming, // hamming
31+
Hann, // hann
32+
Triangle, // triangle (Bartlett)
33+
Nuttall, // nuttall
34+
Blackman, // blackman
35+
Blackman_Nuttall, // blackman nuttall
36+
Blackman_Harris, // blackman harris
37+
Flat_top, // flat top
38+
Welch, // welch
39+
Precompiled // Placeholder for using custom or precompiled window values
40+
};
41+
42+
enum class FFTDirection { Forward, Reverse };
43+
#endif

0 commit comments

Comments
 (0)