A low cost DIY sound pressure level sensor for enabling environmental noise awareness. https://lukasschwarz.org/noise-sensor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

arduinoFFT.h 3.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. FFT libray
  3. Copyright (C) 2010 Didier Longueville
  4. Copyright (C) 2014 Enrique Condes
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef arduinoFFT_h /* Prevent loading library twice */
  17. #define arduinoFFT_h
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <math.h>
  21. #include <defs.h>
  22. #include <types.h>
  23. #define FFT_LIB_REV 0x14
  24. /* Custom constants */
  25. #define FFT_FORWARD 0x01
  26. #define FFT_REVERSE 0x00
  27. /* Windowing type */
  28. #define FFT_WIN_TYP_RECTANGLE 0x00 /* rectangle (Box car) */
  29. #define FFT_WIN_TYP_HAMMING 0x01 /* hamming */
  30. #define FFT_WIN_TYP_HANN 0x02 /* hann */
  31. #define FFT_WIN_TYP_TRIANGLE 0x03 /* triangle (Bartlett) */
  32. #define FFT_WIN_TYP_NUTTALL 0x04 /* nuttall */
  33. #define FFT_WIN_TYP_BLACKMAN 0x05 /* blackman */
  34. #define FFT_WIN_TYP_BLACKMAN_NUTTALL 0x06 /* blackman nuttall */
  35. #define FFT_WIN_TYP_BLACKMAN_HARRIS 0x07 /* blackman harris*/
  36. #define FFT_WIN_TYP_FLT_TOP 0x08 /* flat top */
  37. #define FFT_WIN_TYP_WELCH 0x09 /* welch */
  38. /*Mathematial constants*/
  39. #define twoPi 6.28318531
  40. #define fourPi 12.56637061
  41. #define sixPi 18.84955593
  42. #ifdef __AVR__
  43. static const double _c1[]PROGMEM = {0.0000000000, 0.7071067812, 0.9238795325, 0.9807852804,
  44. 0.9951847267, 0.9987954562, 0.9996988187, 0.9999247018,
  45. 0.9999811753, 0.9999952938, 0.9999988235, 0.9999997059,
  46. 0.9999999265, 0.9999999816, 0.9999999954, 0.9999999989,
  47. 0.9999999997};
  48. static const double _c2[]PROGMEM = {1.0000000000, 0.7071067812, 0.3826834324, 0.1950903220,
  49. 0.0980171403, 0.0490676743, 0.0245412285, 0.0122715383,
  50. 0.0061358846, 0.0030679568, 0.0015339802, 0.0007669903,
  51. 0.0003834952, 0.0001917476, 0.0000958738, 0.0000479369,
  52. 0.0000239684};
  53. #endif
  54. class arduinoFFT {
  55. public:
  56. /* Constructor */
  57. arduinoFFT(void);
  58. arduinoFFT(double *vReal, double *vImag, uint16_t samples, double samplingFrequency);
  59. /* Destructor */
  60. ~arduinoFFT(void);
  61. /* Functions */
  62. uint8_t Revision(void);
  63. uint8_t Exponent(uint16_t value);
  64. void ComplexToMagnitude(double *vReal, double *vImag, uint16_t samples);
  65. void Compute(double *vReal, double *vImag, uint16_t samples, uint8_t dir);
  66. void Compute(double *vReal, double *vImag, uint16_t samples, uint8_t power, uint8_t dir);
  67. void DCRemoval(double *vData, uint16_t samples);
  68. double MajorPeak(double *vD, uint16_t samples, double samplingFrequency);
  69. void MajorPeak(double *vD, uint16_t samples, double samplingFrequency, double *f, double *v);
  70. void Windowing(double *vData, uint16_t samples, uint8_t windowType, uint8_t dir);
  71. void ComplexToMagnitude();
  72. void Compute(uint8_t dir);
  73. void DCRemoval();
  74. void MajorPeakAndMagnitude(double *freq_interpolated, double *mag_interpolated); // Added by me
  75. double MajorPeak();
  76. void MajorPeak(double *f, double *v);
  77. void Windowing(uint8_t windowType, uint8_t dir);
  78. private:
  79. /* Variables */
  80. uint16_t _samples;
  81. double _samplingFrequency;
  82. double *_vReal;
  83. double *_vImag;
  84. uint8_t _power;
  85. /* Functions */
  86. void Swap(double *x, double *y);
  87. };
  88. #endif