arduino-audio-tools
AudioCmsisFFT.h
1 #pragma once
2 #include "AudioFFT.h"
3 #ifdef STM32
4 # include "CMSIS_DSP.h"
5 #endif
6 #if defined(ARDUINO_ARCH_RENESAS) || defined(ARDUINO_ARCH_RP2040)
7 # include "arm_vec_fft.h"
8 #endif
15 namespace audio_tools {
16 
23 class FFTDriverCmsisFFT : public FFTDriver {
24  public:
25  bool begin(int len) override {
26  TRACEI();
27  this->len = len;
28  input = new float[len];
29  output = new float[len*2];
30  output_magn = new float[len];
31  status = arm_rfft_fast_init_f32(&fft_instance, len);
32  if (status!=ARM_MATH_SUCCESS){
33  LOGE("arm_rfft_fast_init_f32: %d", status);
34  }
35  assert(input!=nullptr);
36  assert(output!=nullptr);
37  assert(output_magn != nullptr);
38  return input!=nullptr && output != nullptr && output_magn != nullptr;
39  }
40  void end()override{
41  TRACEI();
42  if (input!=nullptr) delete input;
43  if (output!=nullptr) delete output;
44  if (output_magn!=nullptr) delete output_magn;
45  input = nullptr;
46  output = nullptr;
47  output_magn = nullptr;
48  }
49 
50  void setValue(int idx, float value) override{
51  input[idx] = value;
52  }
53 
54  void fft() override {
55  TRACED();
56  arm_rfft_fast_f32(&fft_instance, input, output, false);
57  arm_cmplx_mag_f32(output, output_magn, len / 2);
58  /* Calculates maxValue and returns corresponding BIN value */
59  arm_max_f32(output_magn, len / 2, &result_max_value, &result_index);
60  TRACED();
61  };
62 
63  void rfft() override {
64  arm_rfft_fast_f32(&fft_instance, output, input, true);
65  }
66 
67  float magnitude(int idx) override {
68  return output_magn[idx];
69  }
70 
72  float magnitudeFast(int idx) override {
73  return output_magn[idx];
74  }
75 
76  float getValue(int idx) override { return input[idx];}
77 
78  bool setBin(int pos, float real, float img) override {
79  if (pos>=len) return false;
80  output[pos*2] = real;
81  output[pos*2+1] = img;
82  return true;
83  }
84  bool getBin(int pos, FFTBin &bin) override {
85  if (pos>=len) return false;
86  bin.real = output[pos*2];
87  bin.img = output[pos*2+1];
88  return true;
89  }
90 
91  bool isReverseFFT() override {return true;}
92 
93  bool isValid() override{ return status==ARM_MATH_SUCCESS; }
94 
95  arm_rfft_fast_instance_f32 fft_instance;
96  arm_status status;
97  int len;
98  float *input=nullptr;
99  float *output_magn=nullptr;
100  float *output=nullptr;
101  float result_max_value;
102  uint32_t result_index = 0;
103 
104 };
105 
112 class AudioCmsisFFT : public AudioFFTBase {
113  public:
115 
117  float* array() {
118  return driverEx()->output;
119  }
120 
121  float* magnitudes() {
122  return driverEx()->output_magn;
123  }
124 
125  AudioFFTResult result() {
126  AudioFFTResult ret_value;
127  ret_value.magnitude = driverEx()->result_max_value;
128  ret_value.bin = driverEx()->result_index;
129  return ret_value;
130  }
131 
132  FFTDriverCmsisFFT* driverEx() {
133  return (FFTDriverCmsisFFT*)driver();
134  }
135 };
136 
137 
138 }
AudioFFT for ARM processors that provided Cmsis DSP.
Definition: AudioCmsisFFT.h:112
float * array()
Provides the result array returned by CMSIS FFT.
Definition: AudioCmsisFFT.h:117
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 Cmsis-FFT see https://arm-software.github.io/CMSIS_5/DSP.
Definition: AudioCmsisFFT.h:23
float magnitudeFast(int idx) override
same as magnitude
Definition: AudioCmsisFFT.h:72
void setValue(int idx, float value) override
Sets the real value.
Definition: AudioCmsisFFT.h:50
bool isReverseFFT() override
Returns true if reverse FFT is supported.
Definition: AudioCmsisFFT.h:91
bool setBin(int pos, float real, float img) override
sets the value of a bin
Definition: AudioCmsisFFT.h:78
bool getBin(int pos, FFTBin &bin) override
gets the value of a bin
Definition: AudioCmsisFFT.h:84
float magnitude(int idx) override
Calculate the magnitude (fft result) at index (sqr(i² + r²))
Definition: AudioCmsisFFT.h:67
void fft() override
Perform FFT.
Definition: AudioCmsisFFT.h:54
void rfft() override
Calculate reverse FFT.
Definition: AudioCmsisFFT.h:63
float getValue(int idx) override
Get result value from Reverse FFT.
Definition: AudioCmsisFFT.h:76
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