Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
arduinoFFT.h
1/*
2
3 FFT library
4 Copyright (C) 2010 Didier Longueville
5 Copyright (C) 2014 Enrique Condes
6 Copyright (C) 2020 Bim Overbohm (template, speed improvements)
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21*/
22
23#ifndef ArduinoFFT_h /* Prevent loading library twice */
24#define ArduinoFFT_h
25#ifdef ARDUINO
26#if ARDUINO >= 100
27#include "Arduino.h"
28#else
29#include "WProgram.h" /* This is where the standard Arduino code lies */
30#endif
31#else
32#include <stdio.h>
33#include <stdlib.h>
34
35#ifdef __AVR__
36#include <avr/io.h>
37#include <avr/pgmspace.h>
38#endif
39#include "defs.h"
40#include "types.h"
41#include <math.h>
42#include <stdint.h>
43#endif
44#include "enumsFFT.h"
45
46// This definition uses a low-precision square root approximation instead of the
47// regular sqrt() call
48// This might only work for specific use cases, but is significantly faster.
49
50#ifndef FFT_SQRT_APPROXIMATION
51 #ifndef sqrt_internal
52 #define sqrt_internal sqrt
53 #endif
54#endif
55
56#define FFT_LIB_REV 0x20
57
58template <typename T> class ArduinoFFT {
59public:
60 ArduinoFFT();
61 ArduinoFFT(T *vReal, T *vImag, uint_fast16_t samples, T samplingFrequency,
62 bool windowingFactors = false);
63
65
66 void complexToMagnitude(void) const;
67 void complexToMagnitude(T *vReal, T *vImag, uint_fast16_t samples) const;
68
69 void compute(FFTDirection dir) const;
70 void compute(T *vReal, T *vImag, uint_fast16_t samples,
71 FFTDirection dir) const;
72 void compute(T *vReal, T *vImag, uint_fast16_t samples, uint_fast8_t power,
73 FFTDirection dir) const;
74
75 void dcRemoval(void) const;
76 void dcRemoval(T *vData, uint_fast16_t samples) const;
77
78 T majorPeak(void) const;
79 void majorPeak(T *f, T *v) const;
80 T majorPeak(T *vData, uint_fast16_t samples, T samplingFrequency) const;
81 void majorPeak(T *vData, uint_fast16_t samples, T samplingFrequency,
82 T *frequency, T *magnitude) const;
83
84 T majorPeakParabola(void) const;
85 void majorPeakParabola(T *frequency, T *magnitude) const;
86 T majorPeakParabola(T *vData, uint_fast16_t samples,
87 T samplingFrequency) const;
88 void majorPeakParabola(T *vData, uint_fast16_t samples, T samplingFrequency,
89 T *frequency, T *magnitude) const;
90
91 uint8_t revision(void);
92
93 void setArrays(T *vReal, T *vImag, uint_fast16_t samples = 0);
94
95 void windowing(FFTWindow windowType, FFTDirection dir,
96 bool withCompensation = false);
97 void windowing(T *vData, uint_fast16_t samples, FFTWindow windowType,
98 FFTDirection dir, T *windowingFactors = nullptr,
99 bool withCompensation = false);
100
101private:
102 /* Variables */
103 static const T _WindowCompensationFactors[11];
104#ifdef FFT_SPEED_OVER_PRECISION
105 T _oneOverSamples = 0.0;
106#endif
107 bool _isPrecompiled = false;
108 bool _precompiledWithCompensation = false;
109 uint_fast8_t _power = 0;
110 T *_precompiledWindowingFactors = nullptr;
111 uint_fast16_t _samples;
112 T _samplingFrequency;
113 T *_vImag;
114 T *_vReal;
115 FFTWindow _windowFunction;
116 /* Functions */
117 uint_fast8_t exponent(uint_fast16_t value) const;
118 void findMaxY(T *vData, uint_fast16_t length, T *maxY,
119 uint_fast16_t *index) const;
120 void parabola(T x1, T y1, T x2, T y2, T x3, T y3, T *a, T *b, T *c) const;
121 void swap(T *a, T *b) const;
122
123#ifdef FFT_SQRT_APPROXIMATION
124 float sqrt_internal(float x) const;
125 double sqrt_internal(double x) const;
126#endif
127};
128
129#if defined(__AVR__) && defined(USE_AVR_PROGMEM)
130static const float _c1[] PROGMEM = {
131 0.0000000000, 0.7071067812, 0.9238795325, 0.9807852804, 0.9951847267,
132 0.9987954562, 0.9996988187, 0.9999247018, 0.9999811753, 0.9999952938,
133 0.9999988235, 0.9999997059, 0.9999999265, 0.9999999816, 0.9999999954,
134 0.9999999989, 0.9999999997};
135static const float _c2[] PROGMEM = {
136 1.0000000000, 0.7071067812, 0.3826834324, 0.1950903220, 0.0980171403,
137 0.0490676743, 0.0245412285, 0.0122715383, 0.0061358846, 0.0030679568,
138 0.0015339802, 0.0007669903, 0.0003834952, 0.0001917476, 0.0000958738,
139 0.0000479369, 0.0000239684};
140#endif
141
142#endif
Definition: arduinoFFT.h:58