Skip to content

Commit 96701da

Browse files
authored
Merge pull request #106 from kosme/develop
Allow enabling bit reversal on the imaginary part of the input
2 parents 886ec1e + 7f7bf6e commit 96701da

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

library.json

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

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=arduinoFFT
2-
version=2.0.3
2+
version=2.0.4
33
author=Enrique Condes <[email protected]>
44
maintainer=Enrique Condes <[email protected]>
55
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.
6+
paragraph=With this library you can calculate the frequencies present on a sampled signal.
77
category=Data Processing
88
url=https://github.com/kosme/arduinoFFT
99
architectures=*

src/arduinoFFT.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ template <typename T>
5252
void ArduinoFFT<T>::complexToMagnitude(T *vReal, T *vImag,
5353
uint_fast16_t samples) const {
5454
// vM is half the size of vReal and vImag
55-
for (uint_fast16_t i = 0; i < samples; i++) {
55+
for (uint_fast16_t i = 0; i < (samples >> 1) + 1; i++) {
5656
vReal[i] = sqrt_internal(sq(vReal[i]) + sq(vImag[i]));
5757
}
5858
}
@@ -82,6 +82,9 @@ void ArduinoFFT<T>::compute(T *vReal, T *vImag, uint_fast16_t samples,
8282
for (uint_fast16_t i = 0; i < (samples - 1); i++) {
8383
if (i < j) {
8484
swap(&vReal[i], &vReal[j]);
85+
#ifdef COMPLEX_INPUT
86+
swap(&vImag[i], &vImag[j]);
87+
#endif
8588
if (dir == FFTDirection::Reverse)
8689
swap(&vImag[i], &vImag[j]);
8790
}
@@ -189,11 +192,12 @@ void ArduinoFFT<T>::majorPeak(T *vData, uint_fast16_t samples,
189192
T delta = 0.5 * ((vData[IndexOfMaxY - 1] - vData[IndexOfMaxY + 1]) /
190193
(vData[IndexOfMaxY - 1] - (2.0 * vData[IndexOfMaxY]) +
191194
vData[IndexOfMaxY + 1]));
192-
T interpolatedX = ((IndexOfMaxY + delta) * samplingFrequency) / (samples - 1);
193-
if (IndexOfMaxY == (samples >> 1)) // To improve calculation on edge values
194-
interpolatedX = ((IndexOfMaxY + delta) * samplingFrequency) / (samples);
195+
if (IndexOfMaxY == (samples >> 1)) { // To improve calculation on edge values
196+
*frequency = ((IndexOfMaxY + delta) * samplingFrequency) / (samples);
197+
} else {
198+
*frequency = ((IndexOfMaxY + delta) * samplingFrequency) / (samples - 1);
199+
}
195200
// returned value: interpolated frequency peak apex
196-
*frequency = interpolatedX;
197201
if (magnitude != nullptr) {
198202
#if defined(ESP8266) || defined(ESP32)
199203
*magnitude = fabs(vData[IndexOfMaxY - 1] - (2.0 * vData[IndexOfMaxY]) +
@@ -504,16 +508,16 @@ template <typename T> double ArduinoFFT<T>::sqrt_internal(double x) const {
504508

505509
template <typename T>
506510
const T ArduinoFFT<T>::_WindowCompensationFactors[11] = {
507-
1.0000000000 * 2.0, // rectangle (Box car)
508-
1.8549343278 * 2.0, // hamming
509-
1.8554726898 * 2.0, // hann
510-
2.0039186079 * 2.0, // triangle (Bartlett)
511-
2.8163172034 * 2.0, // nuttall
512-
2.3673474360 * 2.0, // blackman
513-
2.7557840395 * 2.0, // blackman nuttall
514-
2.7929062517 * 2.0, // blackman harris
515-
3.5659039231 * 2.0, // flat top
516-
1.5029392863 * 2.0, // welch
511+
2.0, // 1.0000000000 * 2.0, // rectangle (Box car)
512+
3.7098686556, // 1.8549343278 * 2.0, // hamming
513+
3.7109453796, // 1.8554726898 * 2.0, // hann
514+
4.0078372158, // 2.0039186079 * 2.0, // triangle (Bartlett)
515+
5.6326344068, // 2.8163172034 * 2.0, // nuttall
516+
4.734694872, // 2.3673474360 * 2.0, // blackman
517+
5.511568079, // 2.7557840395 * 2.0, // blackman nuttall
518+
5.5858125034, // 2.7929062517 * 2.0, // blackman harris
519+
7.1318078462, // 3.5659039231 * 2.0, // flat top
520+
3.0058785726, // 1.5029392863 * 2.0, // welch
517521
// This is added as a precaution, since this index should never be
518522
// accessed under normal conditions
519523
1.0 // Custom, precompiled value.

0 commit comments

Comments
 (0)