2014-07-20 01:08:55 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
FFT libray
|
|
|
|
Copyright (C) 2010 Didier Longueville
|
2014-07-20 03:42:36 +00:00
|
|
|
Copyright (C) 2014 Enrique Condes
|
2014-07-20 01:08:55 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-04-25 18:01:06 +00:00
|
|
|
|
2014-07-20 01:08:55 +00:00
|
|
|
*/
|
|
|
|
|
2014-07-20 03:42:36 +00:00
|
|
|
#include "arduinoFFT.h"
|
2014-07-20 01:08:55 +00:00
|
|
|
|
2017-04-25 18:01:06 +00:00
|
|
|
arduinoFFT::arduinoFFT(void)
|
2018-02-10 20:01:14 +00:00
|
|
|
{ // Constructor
|
2020-01-27 06:27:27 +00:00
|
|
|
#warning("This method is deprecated and may be removed on future revisions.")
|
2018-02-10 20:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
arduinoFFT::arduinoFFT(double *vReal, double *vImag, uint16_t samples, double samplingFrequency)
|
|
|
|
{// Constructor
|
|
|
|
this->_vReal = vReal;
|
|
|
|
this->_vImag = vImag;
|
|
|
|
this->_samples = samples;
|
|
|
|
this->_samplingFrequency = samplingFrequency;
|
|
|
|
this->_power = Exponent(samples);
|
2014-07-20 01:08:55 +00:00
|
|
|
}
|
|
|
|
|
2014-07-20 03:42:36 +00:00
|
|
|
arduinoFFT::~arduinoFFT(void)
|
2017-04-25 18:01:06 +00:00
|
|
|
{
|
2018-02-10 20:01:14 +00:00
|
|
|
// Destructor
|
2014-07-20 01:08:55 +00:00
|
|
|
}
|
|
|
|
|
2014-07-20 03:42:36 +00:00
|
|
|
uint8_t arduinoFFT::Revision(void)
|
2017-04-25 18:01:06 +00:00
|
|
|
{
|
2014-07-20 01:08:55 +00:00
|
|
|
return(FFT_LIB_REV);
|
|
|
|
}
|
|
|
|
|
2014-07-20 03:42:36 +00:00
|
|
|
void arduinoFFT::Compute(double *vReal, double *vImag, uint16_t samples, uint8_t dir)
|
|
|
|
{
|
2020-01-27 06:27:27 +00:00
|
|
|
#warning("This method is deprecated and may be removed on future revisions.")
|
2014-07-20 03:42:36 +00:00
|
|
|
Compute(vReal, vImag, samples, Exponent(samples), dir);
|
|
|
|
}
|
|
|
|
|
2018-02-10 20:01:14 +00:00
|
|
|
void arduinoFFT::Compute(uint8_t dir)
|
|
|
|
{// Computes in-place complex-to-complex FFT /
|
|
|
|
// Reverse bits /
|
|
|
|
uint16_t j = 0;
|
|
|
|
for (uint16_t i = 0; i < (this->_samples - 1); i++) {
|
|
|
|
if (i < j) {
|
|
|
|
Swap(&this->_vReal[i], &this->_vReal[j]);
|
|
|
|
if(dir==FFT_REVERSE)
|
|
|
|
Swap(&this->_vImag[i], &this->_vImag[j]);
|
|
|
|
}
|
|
|
|
uint16_t k = (this->_samples >> 1);
|
|
|
|
while (k <= j) {
|
|
|
|
j -= k;
|
|
|
|
k >>= 1;
|
|
|
|
}
|
|
|
|
j += k;
|
|
|
|
}
|
|
|
|
// Compute the FFT /
|
2020-01-27 06:27:27 +00:00
|
|
|
#ifdef __AVR__
|
|
|
|
uint8_t index = 0;
|
|
|
|
#endif
|
2018-02-10 20:01:14 +00:00
|
|
|
double c1 = -1.0;
|
|
|
|
double c2 = 0.0;
|
|
|
|
uint16_t l2 = 1;
|
|
|
|
for (uint8_t l = 0; (l < this->_power); l++) {
|
|
|
|
uint16_t l1 = l2;
|
|
|
|
l2 <<= 1;
|
|
|
|
double u1 = 1.0;
|
|
|
|
double u2 = 0.0;
|
|
|
|
for (j = 0; j < l1; j++) {
|
|
|
|
for (uint16_t i = j; i < this->_samples; i += l2) {
|
|
|
|
uint16_t i1 = i + l1;
|
|
|
|
double t1 = u1 * this->_vReal[i1] - u2 * this->_vImag[i1];
|
|
|
|
double t2 = u1 * this->_vImag[i1] + u2 * this->_vReal[i1];
|
|
|
|
this->_vReal[i1] = this->_vReal[i] - t1;
|
|
|
|
this->_vImag[i1] = this->_vImag[i] - t2;
|
|
|
|
this->_vReal[i] += t1;
|
|
|
|
this->_vImag[i] += t2;
|
|
|
|
}
|
|
|
|
double z = ((u1 * c1) - (u2 * c2));
|
|
|
|
u2 = ((u1 * c2) + (u2 * c1));
|
|
|
|
u1 = z;
|
|
|
|
}
|
2020-01-27 06:27:27 +00:00
|
|
|
#ifdef __AVR__
|
|
|
|
c2 = pgm_read_float_near(&(_c2[index]));
|
|
|
|
c1 = pgm_read_float_near(&(_c1[index]));
|
|
|
|
index++;
|
|
|
|
#else
|
2018-02-10 20:01:14 +00:00
|
|
|
c2 = sqrt((1.0 - c1) / 2.0);
|
2020-01-27 06:27:27 +00:00
|
|
|
c1 = sqrt((1.0 + c1) / 2.0);
|
|
|
|
#endif
|
2018-02-10 20:01:14 +00:00
|
|
|
if (dir == FFT_FORWARD) {
|
|
|
|
c2 = -c2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Scaling for reverse transform /
|
|
|
|
if (dir != FFT_FORWARD) {
|
|
|
|
for (uint16_t i = 0; i < this->_samples; i++) {
|
|
|
|
this->_vReal[i] /= this->_samples;
|
|
|
|
this->_vImag[i] /= this->_samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 18:01:06 +00:00
|
|
|
void arduinoFFT::Compute(double *vReal, double *vImag, uint16_t samples, uint8_t power, uint8_t dir)
|
2018-02-10 20:01:14 +00:00
|
|
|
{ // Computes in-place complex-to-complex FFT
|
|
|
|
// Reverse bits
|
2020-01-27 06:27:27 +00:00
|
|
|
#warning("This method is deprecated and may be removed on future revisions.")
|
2014-07-20 01:08:55 +00:00
|
|
|
uint16_t j = 0;
|
|
|
|
for (uint16_t i = 0; i < (samples - 1); i++) {
|
|
|
|
if (i < j) {
|
|
|
|
Swap(&vReal[i], &vReal[j]);
|
2018-02-10 20:01:14 +00:00
|
|
|
if(dir==FFT_REVERSE)
|
|
|
|
Swap(&vImag[i], &vImag[j]);
|
2014-07-20 01:08:55 +00:00
|
|
|
}
|
|
|
|
uint16_t k = (samples >> 1);
|
|
|
|
while (k <= j) {
|
|
|
|
j -= k;
|
|
|
|
k >>= 1;
|
|
|
|
}
|
|
|
|
j += k;
|
|
|
|
}
|
2018-02-10 20:01:14 +00:00
|
|
|
// Compute the FFT
|
2020-01-27 06:27:27 +00:00
|
|
|
#ifdef __AVR__
|
|
|
|
uint8_t index = 0;
|
|
|
|
#endif
|
2017-04-25 18:01:06 +00:00
|
|
|
double c1 = -1.0;
|
2014-07-20 01:08:55 +00:00
|
|
|
double c2 = 0.0;
|
2017-08-04 16:18:24 +00:00
|
|
|
uint16_t l2 = 1;
|
2014-07-20 03:42:36 +00:00
|
|
|
for (uint8_t l = 0; (l < power); l++) {
|
2017-08-04 16:18:24 +00:00
|
|
|
uint16_t l1 = l2;
|
2014-07-20 01:08:55 +00:00
|
|
|
l2 <<= 1;
|
2017-04-25 18:01:06 +00:00
|
|
|
double u1 = 1.0;
|
2014-07-20 01:08:55 +00:00
|
|
|
double u2 = 0.0;
|
|
|
|
for (j = 0; j < l1; j++) {
|
|
|
|
for (uint16_t i = j; i < samples; i += l2) {
|
|
|
|
uint16_t i1 = i + l1;
|
|
|
|
double t1 = u1 * vReal[i1] - u2 * vImag[i1];
|
|
|
|
double t2 = u1 * vImag[i1] + u2 * vReal[i1];
|
2017-04-25 18:01:06 +00:00
|
|
|
vReal[i1] = vReal[i] - t1;
|
2014-07-20 01:08:55 +00:00
|
|
|
vImag[i1] = vImag[i] - t2;
|
|
|
|
vReal[i] += t1;
|
|
|
|
vImag[i] += t2;
|
|
|
|
}
|
|
|
|
double z = ((u1 * c1) - (u2 * c2));
|
|
|
|
u2 = ((u1 * c2) + (u2 * c1));
|
|
|
|
u1 = z;
|
|
|
|
}
|
2020-01-27 06:27:27 +00:00
|
|
|
#ifdef __AVR__
|
|
|
|
c2 = pgm_read_float_near(&(_c2[index]));
|
|
|
|
c1 = pgm_read_float_near(&(_c1[index]));
|
|
|
|
index++;
|
|
|
|
#else
|
2017-04-25 18:01:06 +00:00
|
|
|
c2 = sqrt((1.0 - c1) / 2.0);
|
2020-01-27 06:27:27 +00:00
|
|
|
c1 = sqrt((1.0 + c1) / 2.0);
|
|
|
|
#endif
|
2014-07-20 01:08:55 +00:00
|
|
|
if (dir == FFT_FORWARD) {
|
|
|
|
c2 = -c2;
|
|
|
|
}
|
|
|
|
}
|
2018-02-10 20:01:14 +00:00
|
|
|
// Scaling for reverse transform
|
2014-07-20 01:08:55 +00:00
|
|
|
if (dir != FFT_FORWARD) {
|
|
|
|
for (uint16_t i = 0; i < samples; i++) {
|
|
|
|
vReal[i] /= samples;
|
|
|
|
vImag[i] /= samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 20:01:14 +00:00
|
|
|
void arduinoFFT::ComplexToMagnitude()
|
|
|
|
{ // vM is half the size of vReal and vImag
|
|
|
|
for (uint16_t i = 0; i < this->_samples; i++) {
|
|
|
|
this->_vReal[i] = sqrt(sq(this->_vReal[i]) + sq(this->_vImag[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 18:01:06 +00:00
|
|
|
void arduinoFFT::ComplexToMagnitude(double *vReal, double *vImag, uint16_t samples)
|
2018-02-10 20:01:14 +00:00
|
|
|
{ // vM is half the size of vReal and vImag
|
2020-01-27 06:27:27 +00:00
|
|
|
#warning("This method is deprecated and may be removed on future revisions.")
|
2017-08-04 16:18:24 +00:00
|
|
|
for (uint16_t i = 0; i < samples; i++) {
|
2014-07-20 01:08:55 +00:00
|
|
|
vReal[i] = sqrt(sq(vReal[i]) + sq(vImag[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-24 10:42:59 +00:00
|
|
|
void arduinoFFT::DCRemoval()
|
|
|
|
{
|
|
|
|
// calculate the mean of vData
|
|
|
|
double mean = 0;
|
2020-06-07 15:21:03 +00:00
|
|
|
for (uint16_t i = 0; i < this->_samples; i++)
|
2018-11-24 10:42:59 +00:00
|
|
|
{
|
|
|
|
mean += this->_vReal[i];
|
|
|
|
}
|
|
|
|
mean /= this->_samples;
|
|
|
|
// Subtract the mean from vData
|
2020-06-07 15:21:03 +00:00
|
|
|
for (uint16_t i = 0; i < this->_samples; i++)
|
2018-11-24 10:42:59 +00:00
|
|
|
{
|
|
|
|
this->_vReal[i] -= mean;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void arduinoFFT::DCRemoval(double *vData, uint16_t samples)
|
|
|
|
{
|
|
|
|
// calculate the mean of vData
|
2020-01-27 06:27:27 +00:00
|
|
|
#warning("This method is deprecated and may be removed on future revisions.")
|
2018-11-24 10:42:59 +00:00
|
|
|
double mean = 0;
|
2020-06-07 15:21:03 +00:00
|
|
|
for (uint16_t i = 0; i < samples; i++)
|
2018-11-24 10:42:59 +00:00
|
|
|
{
|
|
|
|
mean += vData[i];
|
|
|
|
}
|
|
|
|
mean /= samples;
|
|
|
|
// Subtract the mean from vData
|
2020-06-07 15:21:03 +00:00
|
|
|
for (uint16_t i = 0; i < samples; i++)
|
2018-11-24 10:42:59 +00:00
|
|
|
{
|
|
|
|
vData[i] -= mean;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 20:01:14 +00:00
|
|
|
void arduinoFFT::Windowing(uint8_t windowType, uint8_t dir)
|
|
|
|
{// Weighing factors are computed once before multiple use of FFT
|
|
|
|
// The weighing function is symetric; half the weighs are recorded
|
|
|
|
double samplesMinusOne = (double(this->_samples) - 1.0);
|
|
|
|
for (uint16_t i = 0; i < (this->_samples >> 1); i++) {
|
|
|
|
double indexMinusOne = double(i);
|
|
|
|
double ratio = (indexMinusOne / samplesMinusOne);
|
|
|
|
double weighingFactor = 1.0;
|
|
|
|
// Compute and record weighting factor
|
|
|
|
switch (windowType) {
|
|
|
|
case FFT_WIN_TYP_RECTANGLE: // rectangle (box car)
|
|
|
|
weighingFactor = 1.0;
|
|
|
|
break;
|
|
|
|
case FFT_WIN_TYP_HAMMING: // hamming
|
|
|
|
weighingFactor = 0.54 - (0.46 * cos(twoPi * ratio));
|
|
|
|
break;
|
|
|
|
case FFT_WIN_TYP_HANN: // hann
|
|
|
|
weighingFactor = 0.54 * (1.0 - cos(twoPi * ratio));
|
|
|
|
break;
|
|
|
|
case FFT_WIN_TYP_TRIANGLE: // triangle (Bartlett)
|
|
|
|
weighingFactor = 1.0 - ((2.0 * abs(indexMinusOne - (samplesMinusOne / 2.0))) / samplesMinusOne);
|
|
|
|
break;
|
2018-10-26 10:24:13 +00:00
|
|
|
case FFT_WIN_TYP_NUTTALL: // nuttall
|
|
|
|
weighingFactor = 0.355768 - (0.487396 * (cos(twoPi * ratio))) + (0.144232 * (cos(fourPi * ratio))) - (0.012604 * (cos(sixPi * ratio)));
|
|
|
|
break;
|
|
|
|
case FFT_WIN_TYP_BLACKMAN: // blackman
|
2018-02-10 20:01:14 +00:00
|
|
|
weighingFactor = 0.42323 - (0.49755 * (cos(twoPi * ratio))) + (0.07922 * (cos(fourPi * ratio)));
|
|
|
|
break;
|
2018-10-26 10:24:13 +00:00
|
|
|
case FFT_WIN_TYP_BLACKMAN_NUTTALL: // blackman nuttall
|
|
|
|
weighingFactor = 0.3635819 - (0.4891775 * (cos(twoPi * ratio))) + (0.1365995 * (cos(fourPi * ratio))) - (0.0106411 * (cos(sixPi * ratio)));
|
|
|
|
break;
|
|
|
|
case FFT_WIN_TYP_BLACKMAN_HARRIS: // blackman harris
|
|
|
|
weighingFactor = 0.35875 - (0.48829 * (cos(twoPi * ratio))) + (0.14128 * (cos(fourPi * ratio))) - (0.01168 * (cos(sixPi * ratio)));
|
|
|
|
break;
|
2018-02-10 20:01:14 +00:00
|
|
|
case FFT_WIN_TYP_FLT_TOP: // flat top
|
|
|
|
weighingFactor = 0.2810639 - (0.5208972 * cos(twoPi * ratio)) + (0.1980399 * cos(fourPi * ratio));
|
|
|
|
break;
|
|
|
|
case FFT_WIN_TYP_WELCH: // welch
|
|
|
|
weighingFactor = 1.0 - sq((indexMinusOne - samplesMinusOne / 2.0) / (samplesMinusOne / 2.0));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (dir == FFT_FORWARD) {
|
|
|
|
this->_vReal[i] *= weighingFactor;
|
|
|
|
this->_vReal[this->_samples - (i + 1)] *= weighingFactor;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->_vReal[i] /= weighingFactor;
|
|
|
|
this->_vReal[this->_samples - (i + 1)] /= weighingFactor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-25 18:01:06 +00:00
|
|
|
void arduinoFFT::Windowing(double *vData, uint16_t samples, uint8_t windowType, uint8_t dir)
|
2018-02-10 20:01:14 +00:00
|
|
|
{// Weighing factors are computed once before multiple use of FFT
|
|
|
|
// The weighing function is symetric; half the weighs are recorded
|
2020-01-27 06:27:27 +00:00
|
|
|
#warning("This method is deprecated and may be removed on future revisions.")
|
2014-07-20 01:08:55 +00:00
|
|
|
double samplesMinusOne = (double(samples) - 1.0);
|
|
|
|
for (uint16_t i = 0; i < (samples >> 1); i++) {
|
|
|
|
double indexMinusOne = double(i);
|
|
|
|
double ratio = (indexMinusOne / samplesMinusOne);
|
|
|
|
double weighingFactor = 1.0;
|
2018-02-10 20:01:14 +00:00
|
|
|
// Compute and record weighting factor
|
2014-07-20 01:08:55 +00:00
|
|
|
switch (windowType) {
|
2018-02-10 20:01:14 +00:00
|
|
|
case FFT_WIN_TYP_RECTANGLE: // rectangle (box car)
|
2014-07-20 01:08:55 +00:00
|
|
|
weighingFactor = 1.0;
|
|
|
|
break;
|
2018-02-10 20:01:14 +00:00
|
|
|
case FFT_WIN_TYP_HAMMING: // hamming
|
2014-07-20 01:08:55 +00:00
|
|
|
weighingFactor = 0.54 - (0.46 * cos(twoPi * ratio));
|
|
|
|
break;
|
2018-02-10 20:01:14 +00:00
|
|
|
case FFT_WIN_TYP_HANN: // hann
|
2014-07-20 01:08:55 +00:00
|
|
|
weighingFactor = 0.54 * (1.0 - cos(twoPi * ratio));
|
|
|
|
break;
|
2018-02-10 20:01:14 +00:00
|
|
|
case FFT_WIN_TYP_TRIANGLE: // triangle (Bartlett)
|
2014-07-20 01:08:55 +00:00
|
|
|
weighingFactor = 1.0 - ((2.0 * abs(indexMinusOne - (samplesMinusOne / 2.0))) / samplesMinusOne);
|
|
|
|
break;
|
2018-10-26 10:24:13 +00:00
|
|
|
case FFT_WIN_TYP_NUTTALL: // nuttall
|
|
|
|
weighingFactor = 0.355768 - (0.487396 * (cos(twoPi * ratio))) + (0.144232 * (cos(fourPi * ratio))) - (0.012604 * (cos(sixPi * ratio)));
|
|
|
|
break;
|
|
|
|
case FFT_WIN_TYP_BLACKMAN: // blackman
|
2014-07-20 01:08:55 +00:00
|
|
|
weighingFactor = 0.42323 - (0.49755 * (cos(twoPi * ratio))) + (0.07922 * (cos(fourPi * ratio)));
|
|
|
|
break;
|
2018-10-26 10:24:13 +00:00
|
|
|
case FFT_WIN_TYP_BLACKMAN_NUTTALL: // blackman nuttall
|
|
|
|
weighingFactor = 0.3635819 - (0.4891775 * (cos(twoPi * ratio))) + (0.1365995 * (cos(fourPi * ratio))) - (0.0106411 * (cos(sixPi * ratio)));
|
|
|
|
break;
|
|
|
|
case FFT_WIN_TYP_BLACKMAN_HARRIS: // blackman harris
|
|
|
|
weighingFactor = 0.35875 - (0.48829 * (cos(twoPi * ratio))) + (0.14128 * (cos(fourPi * ratio))) - (0.01168 * (cos(sixPi * ratio)));
|
|
|
|
break;
|
2018-02-10 20:01:14 +00:00
|
|
|
case FFT_WIN_TYP_FLT_TOP: // flat top
|
2014-07-20 01:08:55 +00:00
|
|
|
weighingFactor = 0.2810639 - (0.5208972 * cos(twoPi * ratio)) + (0.1980399 * cos(fourPi * ratio));
|
|
|
|
break;
|
2018-02-10 20:01:14 +00:00
|
|
|
case FFT_WIN_TYP_WELCH: // welch
|
2014-07-20 01:08:55 +00:00
|
|
|
weighingFactor = 1.0 - sq((indexMinusOne - samplesMinusOne / 2.0) / (samplesMinusOne / 2.0));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (dir == FFT_FORWARD) {
|
|
|
|
vData[i] *= weighingFactor;
|
|
|
|
vData[samples - (i + 1)] *= weighingFactor;
|
|
|
|
}
|
|
|
|
else {
|
2017-04-25 18:01:06 +00:00
|
|
|
vData[i] /= weighingFactor;
|
2014-07-20 01:08:55 +00:00
|
|
|
vData[samples - (i + 1)] /= weighingFactor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 20:01:14 +00:00
|
|
|
double arduinoFFT::MajorPeak()
|
2018-02-01 13:02:38 +00:00
|
|
|
{
|
2018-02-10 20:01:14 +00:00
|
|
|
double maxY = 0;
|
|
|
|
uint16_t IndexOfMaxY = 0;
|
|
|
|
//If sampling_frequency = 2 * max_frequency in signal,
|
|
|
|
//value would be stored at position samples/2
|
|
|
|
for (uint16_t i = 1; i < ((this->_samples >> 1) + 1); i++) {
|
|
|
|
if ((this->_vReal[i-1] < this->_vReal[i]) && (this->_vReal[i] > this->_vReal[i+1])) {
|
|
|
|
if (this->_vReal[i] > maxY) {
|
|
|
|
maxY = this->_vReal[i];
|
|
|
|
IndexOfMaxY = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
double delta = 0.5 * ((this->_vReal[IndexOfMaxY-1] - this->_vReal[IndexOfMaxY+1]) / (this->_vReal[IndexOfMaxY-1] - (2.0 * this->_vReal[IndexOfMaxY]) + this->_vReal[IndexOfMaxY+1]));
|
|
|
|
double interpolatedX = ((IndexOfMaxY + delta) * this->_samplingFrequency) / (this->_samples-1);
|
|
|
|
if(IndexOfMaxY==(this->_samples >> 1)) //To improve calculation on edge values
|
|
|
|
interpolatedX = ((IndexOfMaxY + delta) * this->_samplingFrequency) / (this->_samples);
|
2019-01-02 16:32:15 +00:00
|
|
|
// returned value: interpolated frequency peak apex
|
2018-02-10 20:01:14 +00:00
|
|
|
return(interpolatedX);
|
2018-02-01 13:02:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 16:32:15 +00:00
|
|
|
void arduinoFFT::MajorPeak(double *f, double *v)
|
|
|
|
{
|
|
|
|
double maxY = 0;
|
|
|
|
uint16_t IndexOfMaxY = 0;
|
|
|
|
//If sampling_frequency = 2 * max_frequency in signal,
|
|
|
|
//value would be stored at position samples/2
|
|
|
|
for (uint16_t i = 1; i < ((this->_samples >> 1) + 1); i++) {
|
|
|
|
if ((this->_vReal[i - 1] < this->_vReal[i]) && (this->_vReal[i] > this->_vReal[i + 1])) {
|
|
|
|
if (this->_vReal[i] > maxY) {
|
|
|
|
maxY = this->_vReal[i];
|
|
|
|
IndexOfMaxY = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
double delta = 0.5 * ((this->_vReal[IndexOfMaxY - 1] - this->_vReal[IndexOfMaxY + 1]) / (this->_vReal[IndexOfMaxY - 1] - (2.0 * this->_vReal[IndexOfMaxY]) + this->_vReal[IndexOfMaxY + 1]));
|
|
|
|
double interpolatedX = ((IndexOfMaxY + delta) * this->_samplingFrequency) / (this->_samples - 1);
|
|
|
|
if (IndexOfMaxY == (this->_samples >> 1)) //To improve calculation on edge values
|
|
|
|
interpolatedX = ((IndexOfMaxY + delta) * this->_samplingFrequency) / (this->_samples);
|
|
|
|
// returned value: interpolated frequency peak apex
|
|
|
|
*f = interpolatedX;
|
|
|
|
*v = abs(this->_vReal[IndexOfMaxY - 1] - (2.0 * this->_vReal[IndexOfMaxY]) + this->_vReal[IndexOfMaxY + 1]);
|
|
|
|
}
|
|
|
|
|
2017-04-25 18:01:06 +00:00
|
|
|
double arduinoFFT::MajorPeak(double *vD, uint16_t samples, double samplingFrequency)
|
2014-07-20 01:08:55 +00:00
|
|
|
{
|
2020-01-27 06:27:27 +00:00
|
|
|
#warning("This method is deprecated and may be removed on future revisions.")
|
2014-07-20 01:08:55 +00:00
|
|
|
double maxY = 0;
|
|
|
|
uint16_t IndexOfMaxY = 0;
|
2018-02-10 20:01:14 +00:00
|
|
|
//If sampling_frequency = 2 * max_frequency in signal,
|
|
|
|
//value would be stored at position samples/2
|
|
|
|
for (uint16_t i = 1; i < ((samples >> 1) + 1); i++) {
|
2014-07-20 01:08:55 +00:00
|
|
|
if ((vD[i-1] < vD[i]) && (vD[i] > vD[i+1])) {
|
|
|
|
if (vD[i] > maxY) {
|
|
|
|
maxY = vD[i];
|
|
|
|
IndexOfMaxY = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
double delta = 0.5 * ((vD[IndexOfMaxY-1] - vD[IndexOfMaxY+1]) / (vD[IndexOfMaxY-1] - (2.0 * vD[IndexOfMaxY]) + vD[IndexOfMaxY+1]));
|
|
|
|
double interpolatedX = ((IndexOfMaxY + delta) * samplingFrequency) / (samples-1);
|
2018-02-10 20:01:14 +00:00
|
|
|
if(IndexOfMaxY==(samples >> 1)) //To improve calculation on edge values
|
|
|
|
interpolatedX = ((IndexOfMaxY + delta) * samplingFrequency) / (samples);
|
|
|
|
// returned value: interpolated frequency peak apex
|
2014-07-20 01:08:55 +00:00
|
|
|
return(interpolatedX);
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:32:15 +00:00
|
|
|
void arduinoFFT::MajorPeak(double *vD, uint16_t samples, double samplingFrequency, double *f, double *v)
|
|
|
|
{
|
2020-01-27 06:27:27 +00:00
|
|
|
#warning("This method is deprecated and may be removed on future revisions.")
|
2019-01-02 16:32:15 +00:00
|
|
|
double maxY = 0;
|
|
|
|
uint16_t IndexOfMaxY = 0;
|
|
|
|
//If sampling_frequency = 2 * max_frequency in signal,
|
|
|
|
//value would be stored at position samples/2
|
|
|
|
for (uint16_t i = 1; i < ((samples >> 1) + 1); i++) {
|
|
|
|
if ((vD[i - 1] < vD[i]) && (vD[i] > vD[i + 1])) {
|
|
|
|
if (vD[i] > maxY) {
|
|
|
|
maxY = vD[i];
|
|
|
|
IndexOfMaxY = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
double delta = 0.5 * ((vD[IndexOfMaxY - 1] - vD[IndexOfMaxY + 1]) / (vD[IndexOfMaxY - 1] - (2.0 * vD[IndexOfMaxY]) + vD[IndexOfMaxY + 1]));
|
|
|
|
double interpolatedX = ((IndexOfMaxY + delta) * samplingFrequency) / (samples - 1);
|
|
|
|
//double popo =
|
|
|
|
if (IndexOfMaxY == (samples >> 1)) //To improve calculation on edge values
|
|
|
|
interpolatedX = ((IndexOfMaxY + delta) * samplingFrequency) / (samples);
|
|
|
|
// returned value: interpolated frequency peak apex
|
|
|
|
*f = interpolatedX;
|
|
|
|
*v = abs(vD[IndexOfMaxY - 1] - (2.0 * vD[IndexOfMaxY]) + vD[IndexOfMaxY + 1]);
|
|
|
|
}
|
|
|
|
|
2018-02-01 13:02:38 +00:00
|
|
|
uint8_t arduinoFFT::Exponent(uint16_t value)
|
|
|
|
{
|
2020-01-27 06:27:27 +00:00
|
|
|
#warning("This method may not be accessible on future revisions.")
|
2018-02-10 20:01:14 +00:00
|
|
|
// Calculates the base 2 logarithm of a value
|
2018-02-01 13:02:38 +00:00
|
|
|
uint8_t result = 0;
|
|
|
|
while (((value >> result) & 1) != 1) result++;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2018-02-10 20:01:14 +00:00
|
|
|
// Private functions
|
2014-07-20 01:08:55 +00:00
|
|
|
|
2017-04-25 18:01:06 +00:00
|
|
|
void arduinoFFT::Swap(double *x, double *y)
|
2014-07-20 01:08:55 +00:00
|
|
|
{
|
|
|
|
double temp = *x;
|
|
|
|
*x = *y;
|
|
|
|
*y = temp;
|
|
|
|
}
|