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, int 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 
49  float magnitude(int idx) override {
50  return sqrt(p_x[idx] * p_x[idx] + p_f[idx] * p_f[idx]);
51  }
52 
54  float magnitudeFast(int idx) override {
55  return (p_x[idx] * p_x[idx] + p_f[idx] * p_f[idx]);
56  }
57 
58  virtual bool isValid() override{ return p_fft_object!=nullptr; }
59 
60  ffft::FFTReal <float> *p_fft_object=nullptr;
61  float *p_x = nullptr; // real
62  float *p_f = nullptr; // complex
63  int len;
64 
65 };
66 
73 class AudioRealFFT : public AudioFFTBase {
74  public:
76 
78  float* realArray() {
79  return driverEx()->p_x;
80  }
81 
83  float *imgArray() {
84  return driverEx()->p_f;
85  }
86 
88  float* ifft(float *real, float* complex){
89  // update mirrored values
90  int len = length();
91  static_cast<FFTDriverRealFFT*>(driver())->p_fft_object->do_ifft(real, complex);
92  return real;
93  }
94 
95  FFTDriverRealFFT* driverEx() {
96  return (FFTDriverRealFFT*)driver();
97  }
98 };
99 
100 }
Executes FFT using audio data. The Driver which is passed in the constructor selects a specifc FFT im...
Definition: AudioFFT.h:82
int length()
The number of samples.
Definition: AudioFFT.h:182
AudioFFTBase(FFTDriver *driver)
Default Constructor. The len needs to be of the power of 2 (e.g. 512, 1024, 2048, 4096,...
Definition: AudioFFT.h:85
FFTDriver * driver()
provides access to the FFTDriver which implements the basic FFT functionality
Definition: AudioFFT.h:240
AudioFFT using RealFFT.
Definition: AudioRealFFT.h:73
float * imgArray()
Provides the complex array returned by the FFT
Definition: AudioRealFFT.h:83
float * realArray()
Provides the real array returned by the FFT.
Definition: AudioRealFFT.h:78
float * ifft(float *real, float *complex)
Inverse fft - convert fft result back to time domain (samples)
Definition: AudioRealFFT.h:88
Abstract Class which defines the basic FFT functionality.
Definition: AudioFFT.h:65
Driver for RealFFT.
Definition: AudioRealFFT.h:20
float magnitudeFast(int idx) override
magnitude w/o sqrt
Definition: AudioRealFFT.h:54
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AnalogAudioArduino.h:12