arduino-audio-tools
AudioRealFFT.h
1 #pragma once
2 
3 #include "AudioFFT.h"
4 #include "FFT/FFTReal.h"
5 
12 namespace audio_tools {
13 
20 class FFTDriverRealFFT : public FFTDriver {
21  public:
22  bool begin(int len) override {
23  this->len = len;
24  if (p_fft_object==nullptr) p_fft_object = new ffft::FFTReal<float>(len);
25  if (p_x==nullptr) p_x = new float[len];
26  if (p_f==nullptr) p_f = new float[len];
27  assert(p_fft_object!=nullptr);
28  assert(p_x!=nullptr);
29  assert(p_f!=nullptr);
30  return p_fft_object!=nullptr && p_x!=nullptr && p_f!=nullptr;
31  }
32  void end()override{
33  if (p_fft_object!=nullptr) delete p_fft_object;
34  if (p_x!=nullptr) delete[] p_x;
35  if (p_f!=nullptr) delete[] p_f;
36  p_fft_object = nullptr;
37  p_x = nullptr;
38  p_f = nullptr;
39  }
40  void setValue(int idx, float value) override{
41  p_x[idx] = value;
42  }
43 
44  void fft() override{
45  memset(p_f,0,len*sizeof(float));
46  p_fft_object->do_fft(p_f, p_x);
47  };
48 
50  void rfft() override{
51  // ifft
52  p_fft_object->do_ifft(p_f, p_x);
53  }
54 
55  bool isReverseFFT() override { return true;}
56 
57  float magnitude(int idx) override {
58  return sqrt(magnitudeFast(idx));
59  }
60 
62  float magnitudeFast(int idx) override {
63  return (p_x[idx] * p_x[idx] + p_f[idx] * p_f[idx]);
64  }
65 
66  bool isValid() override{ return p_fft_object!=nullptr; }
67 
69  float getValue(int idx) override { return p_x[idx];}
70 
71  bool setBin(int pos, float real, float img) override {
72  if (pos>=len) return false;
73  p_x[pos] = real;
74  p_f[pos] = img;
75  return true;
76  }
77  bool getBin(int pos, FFTBin &bin) override {
78  if (pos>=len) return false;
79  bin.real = p_x[pos];
80  bin.img = p_f[pos];
81  return true;
82  }
83 
84  ffft::FFTReal <float> *p_fft_object=nullptr;
85  float *p_x = nullptr; // real
86  float *p_f = nullptr; // complex
87  int len;
88 
89 };
90 
97 class AudioRealFFT : public AudioFFTBase {
98  public:
100 
102  setOutput(out);
103  }
104 
106  float* realArray() {
107  return driverEx()->p_x;
108  }
109 
111  float *imgArray() {
112  return driverEx()->p_f;
113  }
114 
115  FFTDriverRealFFT* driverEx() {
116  return (FFTDriverRealFFT*)driver();
117  }
118 };
119 
120 }
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
void setOutput(Print &out)
Define final output for reverse ffft.
Definition: AudioFFT.h:345
AudioFFT using RealFFT.
Definition: AudioRealFFT.h:97
float * imgArray()
Provides the complex array returned by the FFT
Definition: AudioRealFFT.h:111
float * realArray()
Provides the real array returned by the FFT.
Definition: AudioRealFFT.h:106
Abstract Class which defines the basic FFT functionality.
Definition: AudioFFT.h:92
Driver for RealFFT.
Definition: AudioRealFFT.h:20
float magnitudeFast(int idx) override
magnitude w/o sqrt
Definition: AudioRealFFT.h:62
void setValue(int idx, float value) override
Sets the real value.
Definition: AudioRealFFT.h:40
bool isReverseFFT() override
Returns true if reverse FFT is supported.
Definition: AudioRealFFT.h:55
bool setBin(int pos, float real, float img) override
sets the value of a bin
Definition: AudioRealFFT.h:71
bool getBin(int pos, FFTBin &bin) override
gets the value of a bin
Definition: AudioRealFFT.h:77
float magnitude(int idx) override
Calculate the magnitude (fft result) at index (sqr(i² + r²))
Definition: AudioRealFFT.h:57
void fft() override
Perform FFT.
Definition: AudioRealFFT.h:44
void rfft() override
Inverse fft - convert fft result back to time domain (samples)
Definition: AudioRealFFT.h:50
float getValue(int idx) override
get Real value
Definition: AudioRealFFT.h:69
Definition: NoArduino.h:58
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823
And individual FFT Bin.
Definition: AudioFFT.h:61