FFT / iFFT?

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 :slight_smile:

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;
		}
    }
}
1 Like

Hello --as a way to finally get FFT to work on the daisy I copied your code to try (and switched it to the patch)

I put (i think) all the relevant files in the makefile (see this post)

and i changed the name of that one file to a lower case S. (so the asm file would work)

BUT now I am getting these new errors

/Users/cricketbee/desktop/daisyexamples/patch/blank/blank.cpp:71: undefined reference to arm_cmplx_mag_f32' /Applications/ARM/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: /Users/cricketbee/desktop/daisyexamples/patch/blank/blank.cpp:74: undefined reference to arm_max_f32’

I have those in my makefile

Project Name

TARGET = Blank

Sources

CPP_SOURCES = blank.cpp

OPT = -Os

Library Locations

LIBDAISY_DIR = …/…/libDaisy
DAISYSP_DIR = …/…/DaisySP
ASM_SOURCES =
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/TransformFunctions/arm_bitreversal2.s
C_SOURCES =
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/TransformFunctions/arm_rfft_fast_f32.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/TransformFunctions/arm_rfft_fast_init_f32.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/CommonTables/arm_common_tables.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/CommonTables/arm_const_structs.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/TransformFunctions/arm_cfft_f32.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/TransformFunctions/arm_bitreversal.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/TransformFunctions/arm_cfft_radix8_f32.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/TransformFunctions/arm_cfft_radix4_f32.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/TransformFunctions/arm_cfft_radix4_f32.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/ComplexMathFunctions/arm_cmplx_mag_f32.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/ComplexMathFunctions/arm_max_f32.c
C_INCLUDES +=
-I$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Include

Core location, and generic Makefile.

SYSTEM_FILES_DIR = $(LIBDAISY_DIR)/core
include $(SYSTEM_FILES_DIR)/Makefile


thanks for any help

Here’s what I have in the Makefile for the CMSIS stuff:
C_SOURCES =
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/TransformFunctions/arm_cfft_f32.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/TransformFunctions/arm_cfft_radix8_f32.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/TransformFunctions/arm_bitreversal.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/ComplexMathFunctions/arm_cmplx_mag_f32.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/CommonTables/arm_const_structs.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/CommonTables/arm_common_tables.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/StatisticsFunctions/arm_max_f32.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/TransformFunctions/arm_rfft_fast_init_f32.c
$(LIBDAISY_DIR)/Drivers/CMSIS/DSP/Source/TransformFunctions/arm_rfft_fast_f32.c

@cricketbee

as a way to finally get FFT to work on the daisy

please create your own post, it is not good practice to highjack other people threads, especially if you encounter compilation issues that concerns your setup

I am getting spammed on my post for topics that do not concern me
thx for your comprehension

if people were doing the same, a whole site would need to be dedicated to one single thread

oh sorry about that. I actually thought the opposite was true especially since there are so few threads about FFT on this forum. I thought it would be good to put it here so that you would see it as I was hoping you might have had some sucess with your FFT project. And since I was responding to not being able to getting the code running that TelePlayer posted in this thread (see above) It seemed to make the most sense to post it here.

Do you think it would be better to make a new thread for his code?

thanks for your input