Define constants on the headers file

pull/8/head
Enrique Condes 2017-04-25 13:01:06 -05:00
rodzic c69b76d2a9
commit 1b4d271f94
2 zmienionych plików z 24 dodań i 24 usunięć

Wyświetl plik

@ -16,26 +16,23 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "arduinoFFT.h" #include "arduinoFFT.h"
#define twoPi 6.28318531 arduinoFFT::arduinoFFT(void)
#define fourPi 12.56637061
arduinoFFT::arduinoFFT(void)
{ {
/* Constructor */ /* Constructor */
} }
arduinoFFT::~arduinoFFT(void) arduinoFFT::~arduinoFFT(void)
{ {
/* Destructor */ /* Destructor */
} }
uint8_t arduinoFFT::Revision(void) uint8_t arduinoFFT::Revision(void)
{ {
return(FFT_LIB_REV); return(FFT_LIB_REV);
} }
@ -44,7 +41,7 @@ void arduinoFFT::Compute(double *vReal, double *vImag, uint16_t samples, uint8_t
Compute(vReal, vImag, samples, Exponent(samples), dir); Compute(vReal, vImag, samples, Exponent(samples), dir);
} }
void arduinoFFT::Compute(double *vReal, double *vImag, uint16_t samples, uint8_t power, uint8_t dir) void arduinoFFT::Compute(double *vReal, double *vImag, uint16_t samples, uint8_t power, uint8_t dir)
{ {
/* Computes in-place complex-to-complex FFT */ /* Computes in-place complex-to-complex FFT */
/* Reverse bits */ /* Reverse bits */
@ -62,20 +59,20 @@ void arduinoFFT::Compute(double *vReal, double *vImag, uint16_t samples, uint8_t
j += k; j += k;
} }
/* Compute the FFT */ /* Compute the FFT */
double c1 = -1.0; double c1 = -1.0;
double c2 = 0.0; double c2 = 0.0;
uint8_t l2 = 1; uint8_t l2 = 1;
for (uint8_t l = 0; (l < power); l++) { for (uint8_t l = 0; (l < power); l++) {
uint8_t l1 = l2; uint8_t l1 = l2;
l2 <<= 1; l2 <<= 1;
double u1 = 1.0; double u1 = 1.0;
double u2 = 0.0; double u2 = 0.0;
for (j = 0; j < l1; j++) { for (j = 0; j < l1; j++) {
for (uint16_t i = j; i < samples; i += l2) { for (uint16_t i = j; i < samples; i += l2) {
uint16_t i1 = i + l1; uint16_t i1 = i + l1;
double t1 = u1 * vReal[i1] - u2 * vImag[i1]; double t1 = u1 * vReal[i1] - u2 * vImag[i1];
double t2 = u1 * vImag[i1] + u2 * vReal[i1]; double t2 = u1 * vImag[i1] + u2 * vReal[i1];
vReal[i1] = vReal[i] - t1; vReal[i1] = vReal[i] - t1;
vImag[i1] = vImag[i] - t2; vImag[i1] = vImag[i] - t2;
vReal[i] += t1; vReal[i] += t1;
vImag[i] += t2; vImag[i] += t2;
@ -84,7 +81,7 @@ void arduinoFFT::Compute(double *vReal, double *vImag, uint16_t samples, uint8_t
u2 = ((u1 * c2) + (u2 * c1)); u2 = ((u1 * c2) + (u2 * c1));
u1 = z; u1 = z;
} }
c2 = sqrt((1.0 - c1) / 2.0); c2 = sqrt((1.0 - c1) / 2.0);
if (dir == FFT_FORWARD) { if (dir == FFT_FORWARD) {
c2 = -c2; c2 = -c2;
} }
@ -99,7 +96,7 @@ void arduinoFFT::Compute(double *vReal, double *vImag, uint16_t samples, uint8_t
} }
} }
void arduinoFFT::ComplexToMagnitude(double *vReal, double *vImag, uint16_t samples) void arduinoFFT::ComplexToMagnitude(double *vReal, double *vImag, uint16_t samples)
{ {
/* vM is half the size of vReal and vImag */ /* vM is half the size of vReal and vImag */
for (uint8_t i = 0; i < samples; i++) { for (uint8_t i = 0; i < samples; i++) {
@ -107,7 +104,7 @@ void arduinoFFT::ComplexToMagnitude(double *vReal, double *vImag, uint16_t sampl
} }
} }
void arduinoFFT::Windowing(double *vData, uint16_t samples, uint8_t windowType, uint8_t dir) void arduinoFFT::Windowing(double *vData, uint16_t samples, uint8_t windowType, uint8_t dir)
{ {
/* Weighing factors are computed once before multiple use of FFT */ /* Weighing factors are computed once before multiple use of FFT */
/* The weighing function is symetric; half the weighs are recorded */ /* The weighing function is symetric; half the weighs are recorded */
@ -145,13 +142,13 @@ void arduinoFFT::Windowing(double *vData, uint16_t samples, uint8_t windowType,
vData[samples - (i + 1)] *= weighingFactor; vData[samples - (i + 1)] *= weighingFactor;
} }
else { else {
vData[i] /= weighingFactor; vData[i] /= weighingFactor;
vData[samples - (i + 1)] /= weighingFactor; vData[samples - (i + 1)] /= weighingFactor;
} }
} }
} }
double arduinoFFT::MajorPeak(double *vD, uint16_t samples, double samplingFrequency) double arduinoFFT::MajorPeak(double *vD, uint16_t samples, double samplingFrequency)
{ {
double maxY = 0; double maxY = 0;
uint16_t IndexOfMaxY = 0; uint16_t IndexOfMaxY = 0;
@ -171,17 +168,17 @@ double arduinoFFT::MajorPeak(double *vD, uint16_t samples, double samplingFreque
/* Private functions */ /* Private functions */
void arduinoFFT::Swap(double *x, double *y) void arduinoFFT::Swap(double *x, double *y)
{ {
double temp = *x; double temp = *x;
*x = *y; *x = *y;
*y = temp; *y = temp;
} }
uint8_t arduinoFFT::Exponent(uint16_t value) uint8_t arduinoFFT::Exponent(uint16_t value)
{ {
/* Computes the Exponent of a powered 2 value */ /* Computes the Exponent of a powered 2 value */
uint8_t result = 0; uint8_t result = 0;
while (((value >> result) & 1) != 1) result++; while (((value >> result) & 1) != 1) result++;
return(result); return(result);
} }

Wyświetl plik

@ -16,7 +16,7 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef arduinoFFT_h /* Prevent loading library twice */ #ifndef arduinoFFT_h /* Prevent loading library twice */
@ -48,7 +48,10 @@
#define FFT_WIN_TYP_BLACKMAN 0x04 /* blackmann */ #define FFT_WIN_TYP_BLACKMAN 0x04 /* blackmann */
#define FFT_WIN_TYP_FLT_TOP 0x05 /* flat top */ #define FFT_WIN_TYP_FLT_TOP 0x05 /* flat top */
#define FFT_WIN_TYP_WELCH 0x06 /* welch */ #define FFT_WIN_TYP_WELCH 0x06 /* welch */
/*Mathematial constants*/
#define twoPi 6.28318531
#define fourPi 12.56637061
class arduinoFFT { class arduinoFFT {
public: public:
/* Constructor */ /* Constructor */
@ -61,13 +64,13 @@ public:
void Compute(double *vReal, double *vImag, uint16_t samples, uint8_t power, uint8_t dir); void Compute(double *vReal, double *vImag, uint16_t samples, uint8_t power, uint8_t dir);
double MajorPeak(double *vD, uint16_t samples, double samplingFrequency); double MajorPeak(double *vD, uint16_t samples, double samplingFrequency);
uint8_t Revision(void); uint8_t Revision(void);
void Windowing(double *vData, uint16_t samples, uint8_t windowType, uint8_t dir); void Windowing(double *vData, uint16_t samples, uint8_t windowType, uint8_t dir);
uint8_t Exponent(uint16_t value); uint8_t Exponent(uint16_t value);
private: private:
/* Functions */ /* Functions */
void Swap(double *x, double *y); void Swap(double *x, double *y);
}; };
#endif #endif