updating 8bit fft to make it work

hello guys,

i want to make an audio spektrum analyser thing, so i stumbled across this: http://forum.arduino.cc/index.php/topic,38153.0.html

when i tried to run it, it didnt compile at all. then i changed

#include <WProgram.h>

to

#if ARDUINO >= 100
  #include "Arduino.h"
#else
  #include "WProgram.h"
#endif

but there are still many errors which i cant seem to work out:

C:\Users\thoomas\Downloads\arduino-1.6.12-windows\arduino-1.6.12\libraries\FFT\fix_fft.cpp:55:7: error: 'prog_int8_t' does not name a type

 const prog_int8_t Sinewave[N_WAVE-N_WAVE/4] PROGMEM = {

       ^

In file included from C:\Users\thoomas\Downloads\arduino-1.6.12-windows\arduino-1.6.12\libraries\FFT\fix_fft.cpp:1:0:

C:\Users\thoomas\Downloads\arduino-1.6.12-windows\arduino-1.6.12\libraries\FFT\fix_fft.cpp: In function 'int fix_fft(char*, char*, int, int)':

C:\Users\thoomas\Downloads\arduino-1.6.12-windows\arduino-1.6.12\libraries\FFT\fix_fft.cpp:204:28: error: 'Sinewave' was not declared in this scope

   wr =  pgm_read_word_near(Sinewave + j+N_WAVE/4);

                            ^

C:\Users\thoomas\Downloads\arduino-1.6.12-windows\arduino-1.6.12\libraries\FFT\fix_fft.cpp:214:28: error: 'Sinewave' was not declared in this scope

   wi = -pgm_read_word_near(Sinewave + j);

                            ^

exit status 1

is there an easy way to fix / update this code or are there any other currently working libraries to analyse an audio spectrum?

Most people use the OpenMusicLabs FFT code. It works quite well, but the example given is not particularly instructive.

Some time ago I wrote a simple test program that (I hope) helps to understand the input and output better.
Note that windowing is important if the input frequencies do not match up with integer frequency bin values.

/*
 fft_test_sine
 example sketch for testing the fft library.
 This generates a simple sine wave data set consisting
 of two frequencies f1 and f2, transforms it, calculates 
 and prints the amplitude of the transform.
 */

// do #defines BEFORE #includes

#define LIN_OUT 1 // use the lin output function
#define FFT_N 64 // set to 64 point fft

#include <FFT.h> // include the library

void setup() {
  Serial.begin(9600); // output on the serial port
}

void loop() {
  int i,k;
  float f1=2.0,f2=5.0;  //the two input frequencies (bin values)
  for (i = 0 ; i < FFT_N ; i++) { // create samples
    // amplitudes are 1000 for f1 and 500 for f2
    k=1000*sin(2*PI*f1*i/FFT_N)+500.*sin(2*PI*f2*i/FFT_N);
    fft_input[2*i] = k; // put real data into even bins
    fft_input[2*i+1] = 0; // set odd bins to 0
  }
  
  fft_window();  //Try with and without this line, it smears

  fft_reorder(); // reorder the data before doing the fft
  fft_run(); // process the data using the fft
  fft_mag_lin(); // calculate the magnitude of the output

  // print the frequency index and amplitudes

  Serial.println("bin  amplitude");
  for (i=0; i<FFT_N/2; i++) {
    Serial.print(i);
    Serial.print("       ");
    Serial.println(2*fft_lin_out[i]); //*2 for "negative frequency" amplitude
  }
  Serial.println("Done");
  while(1); //wait here
}

When using this code with data taken from the analog input, the input sampling rate must be known and used to convert bin index values to actual frequencies.

Edit: This worked perfectly with older versions of the Arduino IDE (e.g. 1.0.5), but version 1.6.11 of the IDE objects to several statements in the OpenMusicLabs library on my computer. If there is interest I'll try to track that down.

 const prog_int8_t Sinewave[N_WAVE-N_WAVE/4] PROGMEM = {

The define for "prog_int8_t" was removed a while back. As long as you have the 'const' and 'PROGMEM' keywords you can just change it to "int8_t" (a.k.a. 'char').

thanks for the answers.
the code now compiles, but the output is always the same with 0 on all channels except the last one.

what i am searching for is a simple example to convert an audio input into an array. / light up leds

@jremington so i can use this other library for this? i also want to use fastLed, so is using an older arduino version (1.0.5) an option?

also there are a lot of different circuits around. what electronic parts do i need to connect to my arduino, if i have a audiojack as input?

And i found some people using an external board for the analysis, is this the overall better option to go?

i would be really happy to find an example with full documentation of working code & setup

edit: got it to work, turns out i used the digital pin (ouch)

now that i got a starting point, i can start trying stuff out.

i'll create the detailed instruction ive been searching for once i understand enough of it

it currently only reacts to frequencies below 300hz, and the lower freq are far stronger

Edit: yet im curious whether the OpenMusicLabs FFT code would be a better option

@jremington so i can use this other library for this? i also want to use fastLed, so is using an older arduino version (1.0.5) an option?

I don't know if fastLed works with 1.0.5. Try it and see!

jremington:
I don't know if fastLed works with 1.0.5. Try it and see!

it does not :frowning: