|
|
@ -1,68 +1,81 @@ |
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* FFT_Overrlapped_F32 |
|
|
|
* FFT_Overrlapped_OA_F32 |
|
|
|
*
|
|
|
|
* |
|
|
|
* Purpose: Encapsulate the ARM floating point FFT/IFFT functions |
|
|
|
* Purpose: Encapsulate the ARM floating point FFT/IFFT functions |
|
|
|
* in a way that naturally interfaces to my float32 |
|
|
|
* in a way that naturally interfaces to my float32 |
|
|
|
* extension of the Teensy Audio Library. |
|
|
|
* extension of the Teensy Audio Library. |
|
|
|
*
|
|
|
|
* |
|
|
|
* Provides functionality to do overlapped FFT/IFFT where |
|
|
|
* Provides functionality to do overlapped FFT/IFFT where |
|
|
|
* each audio block is a fraction (1, 1/2, 1/4) of the |
|
|
|
* each audio block is a fraction (1, 1/2, 1/4) of the |
|
|
|
* totaly FFT length. This class handles all of the |
|
|
|
* totaly FFT length. This class handles all of the |
|
|
|
* data shuffling to composite the previous data blocks |
|
|
|
* data shuffling to composite the previous data blocks |
|
|
|
* with the current data block to provide the full FFT. |
|
|
|
* with the current data block to provide the full FFT. |
|
|
|
* Does similar data shuffling (overlapp-add) for IFFT. |
|
|
|
* Does similar data shuffling (overlapp-add) for IFFT. |
|
|
|
*
|
|
|
|
* |
|
|
|
* Created: Chip Audette (openaudio.blogspot.com) |
|
|
|
* Created: Chip Audette (openaudio.blogspot.com) |
|
|
|
* Jan-Jul 2017
|
|
|
|
* Jan-Jul 2017 |
|
|
|
*
|
|
|
|
* |
|
|
|
* Typical Usage as FFT: |
|
|
|
* Typical Usage as FFT: |
|
|
|
*
|
|
|
|
|
|
|
|
* //setup the audio stuff
|
|
|
|
* //setup the audio stuff
|
|
|
|
* float sample_rate_Hz = 44100.0; //define sample rate
|
|
|
|
* float sample_rate_Hz = 44100.0; //define sample rate
|
|
|
|
* int audio_block_samples = 32; //define size of audio blocks
|
|
|
|
* int audio_block_samples = 32; //define size of audio blocks
|
|
|
|
* AudioSettings_F32 audio_settings(sample_rate_Hz, audio_block_samples); |
|
|
|
* AudioSettings_F32 audio_settings(sample_rate_Hz, audio_block_samples); |
|
|
|
* // ... continue creating all of your Audio Processing Blocks ...
|
|
|
|
* // ... continue creating all of your Audio Processing Blocks ...
|
|
|
|
*
|
|
|
|
* |
|
|
|
* // within a custom audio processing algorithm that you've written
|
|
|
|
* // within a custom audio processing algorithm that you've written
|
|
|
|
* // you'd create the FFT and IFFT elements
|
|
|
|
* // you'd create the FFT and IFFT elements
|
|
|
|
* int NFFT = 128; //define length of FFT that you want (multiple of audio_block_samples)
|
|
|
|
* int NFFT = 128; //define length of FFT that you want (multiple of audio_block_samples)
|
|
|
|
* FFT_Overrlapped_F32 FFT_obj(audio_settings,NFFT); //Creare FFT object
|
|
|
|
* FFT_Overrlapped_F32 FFT_obj(audio_settings,NFFT); //Creare FFT object
|
|
|
|
* FFT_Overrlapped_F32 IFFT_obj(audio_settings,NFFT); //Creare IFFT object
|
|
|
|
* FFT_Overrlapped_F32 IFFT_obj(audio_settings,NFFT); //Creare IFFT object
|
|
|
|
* float complex_2N_buffer[2*NFFT]; //create buffer to hold the FFT output
|
|
|
|
* float complex_2N_buffer[2*NFFT]; //create buffer to hold the FFT output
|
|
|
|
*
|
|
|
|
* |
|
|
|
* // within your own algorithm's "update()" function (which is what
|
|
|
|
* // within your own algorithm's "update()" function (which is what
|
|
|
|
* // is called automatically by the Teensy Audio Libarary approach
|
|
|
|
* // is called automatically by the Teensy Audio Libarary approach
|
|
|
|
* // to audio processing), you can execute the FFT and IFFT
|
|
|
|
* // to audio processing), you can execute the FFT and IFFT
|
|
|
|
*
|
|
|
|
* |
|
|
|
* // First, get the audio and convert to frequency-domain using an FFT
|
|
|
|
* // First, get the audio and convert to frequency-domain using an FFT
|
|
|
|
* audio_block_f32_t *in_audio_block = AudioStream_F32::receiveReadOnly_f32(); |
|
|
|
* audio_block_f32_t *in_audio_block = AudioStream_F32::receiveReadOnly_f32(); |
|
|
|
* FFT_obj.execute(in_audio_block, complex_2N_buffer); //output is in complex_2N_buffer
|
|
|
|
* FFT_obj.execute(in_audio_block, complex_2N_buffer); //output is in complex_2N_buffer
|
|
|
|
* AudioStream_F32::release(in_audio_block); //We just passed ownership to FFT_obj, so release it here.
|
|
|
|
* AudioStream_F32::release(in_audio_block); //We just passed ownership to FFT_obj, so release it here.
|
|
|
|
*
|
|
|
|
* |
|
|
|
* // Next do whatever processing you'd like on the frequency domain data
|
|
|
|
* // Next do whatever processing you'd like on the frequency domain data
|
|
|
|
* // that is held in complex_2N_buffer
|
|
|
|
* // that is held in complex_2N_buffer
|
|
|
|
*
|
|
|
|
* |
|
|
|
* // Finally, you can convert back to the time domain via IFFT
|
|
|
|
* // Finally, you can convert back to the time domain via IFFT
|
|
|
|
* audio_block_f32_t *out_audio_block = IFFT_obj.execute(complex_2N_buffer);
|
|
|
|
* audio_block_f32_t *out_audio_block = IFFT_obj.execute(complex_2N_buffer); |
|
|
|
* //note that the "out_audio_block" is mananged by IFFT_obj, so don't worry about releasing it.
|
|
|
|
* //note that the "out_audio_block" is mananged by IFFT_obj, so don't worry about releasing it.
|
|
|
|
*
|
|
|
|
* |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* https://forum.pjrc.com/threads/53668-fft-ifft?highlight=IFFT willie.from.texas 9-10-2018
|
|
|
|
|
|
|
|
* I've been using the CMSIS Version 5.3.0 DSP Library since May 2018 (see github.com/ARM-software/CMSIS_5). |
|
|
|
|
|
|
|
* I might be the only person on this forum using it. The library allows me to use the 32-bit floating point |
|
|
|
|
|
|
|
* capability of the Teensy 3.6. I have been able to use it in real-time with 16-bit sample rates |
|
|
|
|
|
|
|
* out of both ADCs up to around 450 kHz (i-q data). I'm very happy with the performance I am obtaining. |
|
|
|
|
|
|
|
* Here is the FFT/IFFT performance I am getting with the library: |
|
|
|
|
|
|
|
* # Points Forward rfft Inverse rfft Forward cfft Inverse cfft |
|
|
|
|
|
|
|
* 512 201 us 247 us 239 us 294 us |
|
|
|
|
|
|
|
* 1024 362 us 454 us 588 us 714 us |
|
|
|
|
|
|
|
* 2048 846 us 1066 us 1376 us 1620 us |
|
|
|
|
|
|
|
* 4096 1860 us 2304 us 2504 us 2990 us |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* |
|
|
|
* License: MIT License |
|
|
|
* License: MIT License |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
#ifndef _FFT_Overlapped_F32_h |
|
|
|
#ifndef _FFT_Overlapped_OA_F32_h |
|
|
|
#define _FFT_Overlapped_F32_h |
|
|
|
#define _FFT_Overlapped_OA_F32_h |
|
|
|
|
|
|
|
|
|
|
|
#include "AudioStream_F32.h" |
|
|
|
#include "AudioStream_F32.h" |
|
|
|
#include <arm_math.h> |
|
|
|
#include <arm_math.h> |
|
|
|
#include "FFT_F32.h" |
|
|
|
#include "FFT_OA_F32.h" |
|
|
|
//#include "utility/dspinst.h" //copied from analyze_fft256.cpp. Do we need this?
|
|
|
|
//#include "utility/dspinst.h" //copied from analyze_fft256.cpp. Do we need this?
|
|
|
|
|
|
|
|
|
|
|
|
// set the max amount of allowed overlap...some number larger than you'll want to use
|
|
|
|
// set the max amount of allowed overlap...some number larger than you'll want to use
|
|
|
|
#define MAX_N_BUFF_BLOCKS 32 //32 blocks x 16 sample blocks enables NFFT = 512, if the Teensy could keep up.
|
|
|
|
#define MAX_N_BUFF_BLOCKS 32 //32 blocks x 16 sample blocks enables NFFT = 512, if the Teensy could keep up.
|
|
|
|
|
|
|
|
|
|
|
|
class FFT_Overlapped_Base_F32 { //handles all the data structures for the overlapping stuff. Doesn't care if FFT or IFFT
|
|
|
|
class FFT_Overlapped_Base_OA_F32 { //handles all the data structures for the overlapping stuff. Doesn't care if FFT or IFFT
|
|
|
|
public: |
|
|
|
public: |
|
|
|
FFT_Overlapped_Base_F32(void) {}; |
|
|
|
FFT_Overlapped_Base_OA_F32(void) {}; |
|
|
|
~FFT_Overlapped_Base_F32(void) { |
|
|
|
~FFT_Overlapped_Base_OA_F32(void) { |
|
|
|
if (N_BUFF_BLOCKS > 0) { |
|
|
|
if (N_BUFF_BLOCKS > 0) { |
|
|
|
for (int i = 0; i < N_BUFF_BLOCKS; i++) { |
|
|
|
for (int i = 0; i < N_BUFF_BLOCKS; i++) { |
|
|
|
if (buff_blocks[i] != NULL) AudioStream_F32::release(buff_blocks[i]); |
|
|
|
if (buff_blocks[i] != NULL) AudioStream_F32::release(buff_blocks[i]); |
|
|
@ -73,16 +86,16 @@ class FFT_Overlapped_Base_F32 { //handles all the data structures for the overl |
|
|
|
|
|
|
|
|
|
|
|
virtual int setup(const AudioSettings_F32 &settings, const int _N_FFT) { |
|
|
|
virtual int setup(const AudioSettings_F32 &settings, const int _N_FFT) { |
|
|
|
int N_FFT; |
|
|
|
int N_FFT; |
|
|
|
|
|
|
|
|
|
|
|
///choose valid _N_FFT
|
|
|
|
///choose valid _N_FFT
|
|
|
|
if (!FFT_F32::is_valid_N_FFT(_N_FFT)) { |
|
|
|
if (!FFT_F32::is_valid_N_FFT(_N_FFT)) { |
|
|
|
Serial.println(F("FFT_Overlapped_Base_F32: *** ERROR ***")); |
|
|
|
Serial.println(F("FFT_Overlapped_Base_F32: *** ERROR ***")); |
|
|
|
Serial.print(F(" : N_FFT ")); Serial.print(_N_FFT);
|
|
|
|
Serial.print(F(" : N_FFT ")); Serial.print(_N_FFT); |
|
|
|
Serial.print(F(" is not allowed. Try a power of 2 between 16 and 2048")); |
|
|
|
Serial.print(F(" is not allowed. Try a power of 2 between 16 and 2048")); |
|
|
|
N_FFT = -1; |
|
|
|
N_FFT = -1; |
|
|
|
return N_FFT; |
|
|
|
return N_FFT; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//how many buffers will compose each FFT?
|
|
|
|
//how many buffers will compose each FFT?
|
|
|
|
audio_block_samples = settings.audio_block_samples; |
|
|
|
audio_block_samples = settings.audio_block_samples; |
|
|
|
N_BUFF_BLOCKS = _N_FFT / audio_block_samples; //truncates!
|
|
|
|
N_BUFF_BLOCKS = _N_FFT / audio_block_samples; //truncates!
|
|
|
@ -93,13 +106,13 @@ class FFT_Overlapped_Base_F32 { //handles all the data structures for the overl |
|
|
|
|
|
|
|
|
|
|
|
//allocate memory for buffers...this is dynamic allocation. Always dangerous.
|
|
|
|
//allocate memory for buffers...this is dynamic allocation. Always dangerous.
|
|
|
|
complex_buffer = new float32_t[2*N_FFT]; //should I check to see if it was successfully allcoated?
|
|
|
|
complex_buffer = new float32_t[2*N_FFT]; //should I check to see if it was successfully allcoated?
|
|
|
|
|
|
|
|
|
|
|
|
//initialize the blocks for holding the previous data
|
|
|
|
//initialize the blocks for holding the previous data
|
|
|
|
for (int i = 0; i < N_BUFF_BLOCKS; i++) { |
|
|
|
for (int i = 0; i < N_BUFF_BLOCKS; i++) { |
|
|
|
buff_blocks[i] = AudioStream_F32::allocate_f32(); |
|
|
|
buff_blocks[i] = AudioStream_F32::allocate_f32(); |
|
|
|
clear_audio_block(buff_blocks[i]); |
|
|
|
clear_audio_block(buff_blocks[i]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return N_FFT; |
|
|
|
return N_FFT; |
|
|
|
} |
|
|
|
} |
|
|
|
virtual int getNFFT(void) = 0; |
|
|
|
virtual int getNFFT(void) = 0; |
|
|
@ -108,61 +121,60 @@ class FFT_Overlapped_Base_F32 { //handles all the data structures for the overl |
|
|
|
protected: |
|
|
|
protected: |
|
|
|
int N_BUFF_BLOCKS = 0; |
|
|
|
int N_BUFF_BLOCKS = 0; |
|
|
|
int audio_block_samples; |
|
|
|
int audio_block_samples; |
|
|
|
|
|
|
|
|
|
|
|
audio_block_f32_t *buff_blocks[MAX_N_BUFF_BLOCKS]; |
|
|
|
audio_block_f32_t *buff_blocks[MAX_N_BUFF_BLOCKS]; |
|
|
|
float32_t *complex_buffer; |
|
|
|
float32_t *complex_buffer; |
|
|
|
|
|
|
|
|
|
|
|
void clear_audio_block(audio_block_f32_t *block) { |
|
|
|
void clear_audio_block(audio_block_f32_t *block) { |
|
|
|
for (int i = 0; i < block->length; i++) block->data[i] = 0.f; |
|
|
|
for (int i = 0; i < block->length; i++) block->data[i] = 0.f; |
|
|
|
}
|
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
class FFT_Overlapped_F32: public FFT_Overlapped_Base_F32 |
|
|
|
class FFT_Overlapped_OA_F32: public FFT_Overlapped_Base_F32 |
|
|
|
{ |
|
|
|
{ |
|
|
|
public: |
|
|
|
public: |
|
|
|
//constructors
|
|
|
|
//constructors
|
|
|
|
FFT_Overlapped_F32(void): FFT_Overlapped_Base_F32() {}; |
|
|
|
FFT_Overlapped_OA_F32(void): FFT_Overlapped_Base_F32() {}; |
|
|
|
FFT_Overlapped_F32(const AudioSettings_F32 &settings): FFT_Overlapped_Base_F32() { } |
|
|
|
FFT_Overlapped_OA_F32(const AudioSettings_F32 &settings): FFT_Overlapped_Base_F32() { } |
|
|
|
FFT_Overlapped_F32(const AudioSettings_F32 &settings, const int _N_FFT): FFT_Overlapped_Base_F32() {
|
|
|
|
FFT_Overlapped_OA_F32(const AudioSettings_F32 &settings, const int _N_FFT): FFT_Overlapped_Base_F32() { |
|
|
|
setup(settings,_N_FFT); |
|
|
|
setup(settings,_N_FFT); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
virtual int setup(const AudioSettings_F32 &settings, const int _N_FFT) { |
|
|
|
virtual int setup(const AudioSettings_F32 &settings, const int _N_FFT) { |
|
|
|
int N_FFT = FFT_Overlapped_Base_F32::setup(settings, _N_FFT); |
|
|
|
int N_FFT = FFT_Overlapped_Base_F32::setup(settings, _N_FFT); |
|
|
|
|
|
|
|
|
|
|
|
//setup the FFT routines
|
|
|
|
//setup the FFT routines
|
|
|
|
N_FFT = myFFT.setup(N_FFT);
|
|
|
|
N_FFT = myFFT.setup(N_FFT); |
|
|
|
return N_FFT; |
|
|
|
return N_FFT; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
virtual void execute(audio_block_f32_t *block, float *complex_2N_buffer); |
|
|
|
virtual void execute(audio_block_f32_t *block, float *complex_2N_buffer); |
|
|
|
virtual int getNFFT(void) { return myFFT.getNFFT(); }; |
|
|
|
virtual int getNFFT(void) { return myFFT.getNFFT(); }; |
|
|
|
FFT_F32* getFFTObject(void) { return &myFFT; }; |
|
|
|
FFT_F32* getFFTObject(void) { return &myFFT; }; |
|
|
|
virtual void rebuildNegativeFrequencySpace(float *complex_2N_buffer) { myFFT.rebuildNegativeFrequencySpace(complex_2N_buffer); } |
|
|
|
virtual void rebuildNegativeFrequencySpace(float *complex_2N_buffer) { myFFT.rebuildNegativeFrequencySpace(complex_2N_buffer); } |
|
|
|
|
|
|
|
|
|
|
|
private: |
|
|
|
private: |
|
|
|
FFT_F32 myFFT; |
|
|
|
FFT_F32 myFFT; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IFFT_Overlapped_OA_F32: public FFT_Overlapped_Base_F32 |
|
|
|
class IFFT_Overlapped_F32: public FFT_Overlapped_Base_F32 |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
public: |
|
|
|
public: |
|
|
|
//constructors
|
|
|
|
//constructors
|
|
|
|
IFFT_Overlapped_F32(void): FFT_Overlapped_Base_F32() {}; |
|
|
|
IFFT_Overlapped_OA_F32(void): FFT_Overlapped_Base_F32() {}; |
|
|
|
IFFT_Overlapped_F32(const AudioSettings_F32 &settings): FFT_Overlapped_Base_F32() { } |
|
|
|
IFFT_Overlapped_OA_F32(const AudioSettings_F32 &settings): FFT_Overlapped_Base_F32() { } |
|
|
|
IFFT_Overlapped_F32(const AudioSettings_F32 &settings, const int _N_FFT): FFT_Overlapped_Base_F32() {
|
|
|
|
IFFT_Overlapped_OA_F32(const AudioSettings_F32 &settings, const int _N_FFT): FFT_Overlapped_Base_F32() { |
|
|
|
setup(settings,_N_FFT); |
|
|
|
setup(settings,_N_FFT); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
virtual int setup(const AudioSettings_F32 &settings, const int _N_FFT) { |
|
|
|
virtual int setup(const AudioSettings_F32 &settings, const int _N_FFT) { |
|
|
|
int N_FFT = FFT_Overlapped_Base_F32::setup(settings, _N_FFT); |
|
|
|
int N_FFT = FFT_Overlapped_Base_F32::setup(settings, _N_FFT); |
|
|
|
|
|
|
|
|
|
|
|
//setup the FFT routines
|
|
|
|
//setup the FFT routines
|
|
|
|
N_FFT = myIFFT.setup(N_FFT);
|
|
|
|
N_FFT = myIFFT.setup(N_FFT); |
|
|
|
return N_FFT; |
|
|
|
return N_FFT; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
virtual audio_block_f32_t* execute(float *complex_2N_buffer); |
|
|
|
virtual audio_block_f32_t* execute(float *complex_2N_buffer); |
|
|
|
virtual int getNFFT(void) { return myIFFT.getNFFT(); }; |
|
|
|
virtual int getNFFT(void) { return myIFFT.getNFFT(); }; |
|
|
|
IFFT_F32* getFFTObject(void) { return &myIFFT; }; |
|
|
|
IFFT_F32* getFFTObject(void) { return &myIFFT; }; |
|
|
@ -170,8 +182,4 @@ class IFFT_Overlapped_F32: public FFT_Overlapped_Base_F32 |
|
|
|
private: |
|
|
|
private: |
|
|
|
IFFT_F32 myIFFT; |
|
|
|
IFFT_F32 myIFFT; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
#endif |