Hi

I see a lot of fft functions in the core code, but I cannot find any examples on how to FFT:iFFT a sample

thanks for pointing me to the right direction

Phil

Hi

I see a lot of fft functions in the core code, but I cannot find any examples on how to FFT:iFFT a sample

thanks for pointing me to the right direction

Phil

Hey Phil!

I hope all is well with you since we last talked.

Creating examples and guide on FFT is certainly on the list of tasks to do. I’m sorry for the lack of one as of right now.

no worries, but now I got bigger issues

section

`.data' will not fit in region`

FLASH’

and my program is basically doing nothing but FFT->iFFT

```
#include "daisy_seed.h"
#include "daisysp.h"
#include "arm_math.h"
// Use the daisy namespace to prevent having to type
// daisy:: before all libdaisy functions
using namespace daisy;
using namespace daisysp;
// Declare a DaisySeed object called hardware
DaisySeed hardware;
Oscillator osc;
AdEnv env;
arm_rfft_fast_instance_f32* fftInstance;
float32_t *fftBuffer;
void AudioCallback(AudioHandle::InterleavingInputBuffer in,
AudioHandle::InterleavingOutputBuffer out,
size_t size)
{
float osc_out;
//Fill the block with samples
for(size_t i = 0; i < size; i += 2)
{
osc_out = osc.Process();
//Set the left and right outputs
out[i] = osc_out;
// out[i + 1] = osc_out;
out[i+1] = in[i+1];
arm_rfft_fast_f32(fftInstance,(float32_t*)&in[0],fftBuffer,0);
arm_rfft_fast_f32(fftInstance,fftBuffer,(float32_t*)&out[0],1);
}
}
int main(void)
{
// Declare a variable to store the state we want to set for the LED.
bool led_state;
led_state = true;
// Configure and Initialize the Daisy Seed
// These are separate to allow reconfiguration of any of the internal
// components before initialization.
hardware.Configure();
hardware.Init();
hardware.SetAudioBlockSize(4);
//How many samples we'll output per second
float samplerate = hardware.AudioSampleRate();
fftInstance = new arm_rfft_fast_instance_f32;
arm_status status = arm_rfft_fast_init_f32(fftInstance, hardware.AudioBlockSize());
fftBuffer = new float32_t[hardware.AudioBlockSize()];
//Set up oscillator
osc.Init(samplerate);
osc.SetWaveform(osc.WAVE_SIN);
osc.SetAmp(1.f);
osc.SetFreq(440);
hardware.StartAudio(AudioCallback);
// Loop forever
for(;;)
{
// Set the onboard LED
hardware.SetLed(led_state);
// Toggle the LED state for the next time around.
led_state = !led_state;
// Wait 500ms
System::Delay(500);
}
}
```

Hey Phil!

I’m sorry for the delay in response. I answered this in the other thread where you also mentioned it.

If you Google about CMSIS DSP flash Usage, you’ll see some stuff about large tables being needlessly linked, and suggestions for how to reduce this.

In my opinion, CMSIS DSP is poorly documented, and not written to efficiently use flash. It might be great functionally, I don’t know.

I’ve read some stuff suggesting using complex FFT, and stuffing the imaginary part with 0, to reduce storage requirements.

Interesting stuff from Teensy forum, years ago, but on this topic:

1 Like

funny that I stopped wasting my time with teensies becos of SNR issues

now I don’t see how settings array values to zero at runtime would reduce the size of the storage requirement

The way the CMSIS constant tables are set up for real numbers is different from complex. Maybe it’s non-intuitive, but it is true. Complex FFT of size 2048 easily fits in Daisy flash, the only down side is some wasted RAM for the 0 imaginary component.

SNR: sound/noise ratio is 50% on teensies

so they fool people with an amazing audio library

yet you have to interface the teensy with a ADC breakout board just to sample audio

and I2S is quiet hard to debug

The main thing I prefer about Daisy is the builtin codec, and patch_sm is very handy for eurorack modules.

On the other hand, I2S might be tricky, but in my experience with Teensy, using several different I2S devices, I2S just worked, with no debugging of I2S.

Anyway, both systems have their pros and cons.

here’s an example which displays the frequency of audio input, using cfft of size 2048:

```
// Using FFT size of 2048 needs 16K of sample buffer, and only gets a resolution of
// 47Hz.
#include "daisysp.h"
#include "../../kxmx_bluemchen/src/kxmx_bluemchen.h"
#include <string.h>
#include "arm_math.h"
#include "arm_const_structs.h"
using namespace kxmx;
using namespace daisy;
using namespace daisysp;
Bluemchen hw;
// OLED is 64x32
void UpdateOled(float fl)
{
hw.display.Fill(false);
std::string str = std::to_string(static_cast<int>(fl));
char *cstr = &str[0];
hw.display.SetCursor(0, 0);
// Font_16x26 4 digits
// Font_11x18 5 digits BEST for this
// Font_6x8 10 digits
hw.display.WriteString(cstr, Font_11x18, true);
hw.display.Update();
}
#define FFT_SIZE 2048
#define FFT_BUF_SIZE (2*FFT_SIZE)
float fft_buf[FFT_BUF_SIZE];
float output[FFT_SIZE];
// global, so do_fft() knows when to run
int fft_buf_index = 0;
void AudioCallback(AudioHandle::InputBuffer in, AudioHandle::OutputBuffer out, size_t size)
{
for(size_t i = 0; i < size; i++)
{
if (fft_buf_index < FFT_BUF_SIZE) {
fft_buf[fft_buf_index++] = in[0][i];
fft_buf[fft_buf_index++] = 0.f;
}
out[0][i] = in[0][i];
out[1][i] = in[1][i];
}
}
float do_fft(float *input, float *output)
{
float32_t maxValue;
uint32_t index = 0;
int Fmax = hw.AudioSampleRate() / 2; // 24000
uint32_t Nbins = FFT_SIZE / 2;
/* Process the data through the CFFT/CIFFT module */
/* this must match FFT_SIZE, and 2048 is as high as Daisy can fit */
arm_cfft_f32(&arm_cfft_sR_f32_len2048, input, 0 /*ifftFlag*/, 1/*doBitReverse*/);
/* Process the data through the Complex Magnitude Module for
calculating the magnitude at each bin */
arm_cmplx_mag_f32(input, output, FFT_SIZE /*fftSize*/);
/* Calculates maxValue and returns corresponding BIN value */
arm_max_f32(output, FFT_SIZE /*fftSize*/, &maxValue, &index);
/*
to calculate the freq of the selected bin
N (Bins) = FFT Size/2
FR = Fmax/N(Bins)
*/
// error test
if (index >= Nbins)
return -1.f;
// good result
return (float) index * ((float)Fmax / Nbins);
}
int main(void)
{
hw.Init();
hw.StartAdc();
hw.StartAudio(AudioCallback);
while (1)
{
float f;
static float old_f = -1.f;
if (fft_buf_index == FFT_BUF_SIZE) {
// do FFT
f = do_fft(fft_buf, output);
// start collecting samples again
fft_buf_index = 0;
} else {
continue;
}
// if frequency value has changed, update display
if ((f > 0.f) && (f != old_f)) {
UpdateOled(f);
old_f = f;
}
}
}
```