arduino-audio-tools
AudioEspressifFFT.h
1 #pragma once
2 
3 #include "AudioFFT.h"
4 #include "esp_dsp.h"
5 
12 namespace audio_tools {
13 
21  public:
22  bool begin(int len) override {
23  this->len = len;
24  if (p_data==nullptr){
25  p_data = new float[len*2];
26  if (p_data==nullptr){
27  LOGE("not enough memory");
28  }
29  }
30  assert(p_data!=nullptr);
31  ret = dsps_fft2r_init_fc32(NULL, CONFIG_DSP_MAX_FFT_SIZE);
32  if (ret != ESP_OK){
33  LOGE("dsps_fft2r_init_fc32 %d", ret);
34  }
35  return p_data!=nullptr && ret == ESP_OK;
36  }
37 
38  void end() override {
39  dsps_fft2r_deinit_fc32();
40  if (p_data==nullptr){
41  delete p_data;
42  p_data = nullptr;
43  }
44  }
45 
46  void setValue(int idx, float value) override {
47  if (idx<len){
48  p_data[idx*2] = value;
49  p_data[idx*2 + 1] = 0.0f;
50  }
51  }
52 
53  float getValue(int idx) override { return p_data[idx * 2]; }
54 
55  void fft() override {
56  ret = dsps_fft2r_fc32(p_data, len);
57  if (ret != ESP_OK){
58  LOGE("dsps_fft2r_fc32 %d", ret);
59  }
60  // Bit reverse
61  ret = dsps_bit_rev_fc32(p_data, len);
62  if (ret != ESP_OK){
63  LOGE("dsps_bit_rev_fc32 %d", ret);
64  }
65  // Convert one complex vector to two complex vectors
66  ret = dsps_cplx2reC_fc32(p_data, len);
67  if (ret != ESP_OK){
68  LOGE("dsps_cplx2reC_fc32 %d", ret);
69  }
70  };
71 
72  void rfft() override {
73  conjugate();
74  ret = dsps_fft2r_fc32(p_data, len);
75  if (ret != ESP_OK){
76  LOGE("dsps_fft2r_fc32 %d", ret);
77  }
78  conjugate();
79  // Bit reverse
80  ret = dsps_bit_rev_fc32(p_data, len);
81  if (ret != ESP_OK){
82  LOGE("dsps_bit_rev_fc32 %d", ret);
83  }
84  // Convert one complex vector to two complex vectors
85  ret = dsps_cplx2reC_fc32(p_data, len);
86  if (ret != ESP_OK){
87  LOGE("dsps_cplx2reC_fc32 %d", ret);
88  }
89  }
90 
91  void conjugate(){
92  FFTBin bin;
93  for (int j=0;j<len;j++){
94  getBin(j, bin);
95  bin.conjugate();
96  setBin(j, bin);
97  }
98  }
99 
100  float magnitude(int idx) override {
101  return sqrt(magnitudeFast(idx));
102  }
103 
105  float magnitudeFast(int idx) override {
106  return (p_data[idx*2] * p_data[idx*2] + p_data[idx*2+1] * p_data[idx*2+1]);
107  }
108  bool setBin(int pos, float real, float img) override {
109  if (pos>=len) return false;
110  p_data[pos*2] = real;
111  p_data[pos*2+1] = img;
112  return true;
113  }
114 
115  bool setBin(int pos, FFTBin &bin) { return FFTDriver::setBin(pos, bin);}
116 
117  bool getBin(int pos, FFTBin &bin) override {
118  if (pos>=len) return false;
119  bin.real = p_data[pos*2];
120  bin.img = p_data[pos*2+1];
121  return true;
122  }
123 
124  bool isReverseFFT() override {return true;}
125 
126  bool isValid() override{ return p_data!=nullptr && ret==ESP_OK; }
127 
128  esp_err_t ret;
129  float *p_data = nullptr;
130  int len=0;
131 
132 };
140  public:
142 
144  float *dataArray() {
145  return driverEx()->p_data;
146  }
147 
148  FFTDriverEspressifFFT* driverEx() {
149  return (FFTDriverEspressifFFT*)driver();
150  }
151 };
152 
153 
154 }
AudioFFT using FFTReal. The only specific functionality is the access to the dataArray.
Definition: AudioEspressifFFT.h:139
float * dataArray()
Provides the complex array returned by the FFT
Definition: AudioEspressifFFT.h:144
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
fft Driver for espressif dsp library: https://espressif-docs.readthedocs-hosted.com/projects/esp-dsp/...
Definition: AudioEspressifFFT.h:20
float magnitudeFast(int idx) override
magnitude w/o sqrt
Definition: AudioEspressifFFT.h:105
void setValue(int idx, float value) override
Sets the real value.
Definition: AudioEspressifFFT.h:46
bool isReverseFFT() override
Returns true if reverse FFT is supported.
Definition: AudioEspressifFFT.h:124
bool setBin(int pos, float real, float img) override
sets the value of a bin
Definition: AudioEspressifFFT.h:108
bool getBin(int pos, FFTBin &bin) override
gets the value of a bin
Definition: AudioEspressifFFT.h:117
float magnitude(int idx) override
Calculate the magnitude (fft result) at index (sqr(i² + r²))
Definition: AudioEspressifFFT.h:100
void fft() override
Perform FFT.
Definition: AudioEspressifFFT.h:55
void rfft() override
Calculate reverse FFT.
Definition: AudioEspressifFFT.h:72
float getValue(int idx) override
Get result value from Reverse FFT.
Definition: AudioEspressifFFT.h:53
Abstract Class which defines the basic FFT functionality.
Definition: AudioFFT.h:92
virtual bool setBin(int idx, float real, float img)
sets the value of a bin
Definition: AudioFFT.h:112
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823
And individual FFT Bin.
Definition: AudioFFT.h:61