arduino-audio-tools
AudioESP32FFT.h
1 #pragma once
2 
3 #include "AudioFFT.h"
4 #include "fft.h"
5 
13 namespace audio_tools {
14 
21 class FFTDriverESP32FFT : public FFTDriver {
22  public:
23  bool begin(int len) override {
24  this->len = len;
25  if (p_fft_object==nullptr) p_fft_object = fft_init(len, FFT_REAL, FFT_FORWARD, NULL, NULL);
26  assert(p_fft_object!=nullptr);
27  return p_fft_object!=nullptr;
28  }
29  void end()override{
30  if (p_fft_object!=nullptr) fft_destroy(p_fft_object);
31  }
32  void setValue(int idx, float value) override{
33  p_fft_object->input[idx] = value;
34  }
35 
36  void fft() override{
37  fft_execute(p_fft_object);
38  };
39 
40  void rfft() override {
41  irfft(p_fft_object->input, p_fft_object->output, p_fft_object->twiddle_factors, p_fft_object->size);
42  }
43 
44  float magnitude(int idx) override {
45  return sqrt(magnitudeFast(idx));
46  }
47 
49  float magnitudeFast(int idx) override {
50  return (pow(p_fft_object->output[2*idx],2) + pow(p_fft_object->output[2*idx+1],2));
51  }
52 
53  float getValue(int idx) { return p_fft_object->input[idx];}
54 
55  bool setBin(int pos, float real, float img) override {
56  if (pos>=len) return false;
57  p_fft_object->output[2*pos] = real;
58  p_fft_object->output[2*pos+1] = img;
59  return true;
60  }
61  bool getBin(int pos, FFTBin &bin) override {
62  if (pos>=len) return false;
63  bin.real = p_fft_object->output[2*pos];
64  bin.img = p_fft_object->output[2*pos+1];
65  return true;
66  }
67 
68  bool isReverseFFT() override {return true;}
69 
70  bool isValid() override{ return p_fft_object!=nullptr; }
71 
72  fft_config_t *p_fft_object=nullptr;
73  int len;
74 
75 };
76 
84 class AudioESP32FFT : public AudioFFTBase {
85  public:
87 
89  float* array() {
90  return driverEx()->p_fft_object->output;
91  }
92 
93  FFTDriverESP32FFT* driverEx() {
94  return (FFTDriverESP32FFT*)driver();
95  }
96 };
97 
98 }
AudioFFT using RealFFT.
Definition: AudioESP32FFT.h:84
float * array()
Provides the result array returned by the FFT: The real part of a magnitude at a frequency is followe...
Definition: AudioESP32FFT.h:89
Executes FFT using audio data. The Driver which is passed in the constructor selects a specifc FFT im...
Definition: AudioFFT.h:125
AudioFFTBase(FFTDriver *driver)
Default Constructor. The len needs to be of the power of 2 (e.g. 512, 1024, 2048, 4096,...
Definition: AudioFFT.h:128
FFTDriver * driver()
provides access to the FFTDriver which implements the basic FFT functionality
Definition: AudioFFT.h:289
Driver for ESP32-FFT https://github.com/pschatzmann/esp32-fft
Definition: AudioESP32FFT.h:21
float magnitudeFast(int idx) override
magnitude w/o sqrt
Definition: AudioESP32FFT.h:49
float getValue(int idx)
Get result value from Reverse FFT.
Definition: AudioESP32FFT.h:53
void setValue(int idx, float value) override
Sets the real value.
Definition: AudioESP32FFT.h:32
bool isReverseFFT() override
Returns true if reverse FFT is supported.
Definition: AudioESP32FFT.h:68
bool setBin(int pos, float real, float img) override
sets the value of a bin
Definition: AudioESP32FFT.h:55
bool getBin(int pos, FFTBin &bin) override
gets the value of a bin
Definition: AudioESP32FFT.h:61
float magnitude(int idx) override
Calculate the magnitude (fft result) at index (sqr(i² + r²))
Definition: AudioESP32FFT.h:44
void fft() override
Perform FFT.
Definition: AudioESP32FFT.h:36
void rfft() override
Calculate reverse FFT.
Definition: AudioESP32FFT.h:40
Abstract Class which defines the basic FFT functionality.
Definition: AudioFFT.h:92
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823
And individual FFT Bin.
Definition: AudioFFT.h:61