Go Down

Topic: Read Array From Arduino to Computer, FFT (Read 2594 times) previous topic - next topic


Hello all,

I am a novice and I am trying to implement FFT (Fast Fourier Transform) on Arduino. I am using this shield http://www.openmusiclabs.com/projects/codec-shield/

I uploaded the following example code on Arduino and tried to get the spectrum of an audio file. I played the file in my computer and sent the signal from the headphone jack to the line_in using a male-male audio cable. Could this be the problem?

Then I opened the serial monitor, set the baud rate to the one specified in the code, but I did not see any data except some characters flashing across. After a while the IDE froze.

I am inexperienced so I will appreciate it if you could tell me where I am making a mistake. Thanks in advance.

The following code is provided by the manufacturer along with the necessary libraries on their website
Code: [Select]
#define SAMPLE_RATE 44 // 44.1Khz
#define ADCS 0 // no ADCs are being used
#define LOG_OUT 1 // use the log output function
#define FFT_N 256 // set to 256 point fft

// include necessary libraries
#include "FFT.h"
#include <Wire.h>
#include <SPI.h>
#include <AudioCodec.h>

// create data variables for audio transfer
int left_in = 0x0000;
int left_out = 0x0000;
int right_in = 0x0000;
int right_out = 0x0000;
unsigned int count = 0;
volatile byte flag = 1;

void setup() {
  Serial.begin(115200); // use serial port

void loop() {
  while(1) { // reduces clock jitter
    while(flag); // wait for samples to be collected
    fft_window(); // window the data
    fft_reorder(); // reorder for fft input
    fft_run(); // process fft
    fft_mag_log(); // take output of fft
    Serial.write(255); // send out a start byte
    Serial.write(fft_log_out, FFT_N/2); // send out data bytes
    flag = 1; // tell the codec that processing is done

// timer1 interrupt routine - data collected here
ISR(TIMER1_COMPA_vect) { // store registers (NAKED removed)

  // &'s are necessary on data_in variables
  AudioCodec_data(&left_in, &right_in, left_out, right_out);
  left_out = left_in; // pass audio through
  right_out = right_in;
  if (flag) { // check if the fft is ready for more data
    fft_input[count] = left_in; // put real data into even bins
    fft_input[count + 1] = 0; // put zeros in odd bins
    count += 2; // increment to next bin
    if (count >= FFT_N*2) { // check if all bins are full
      flag = 0; // tell the fft to start running
      count = 0; // reset the bin counter


You are flooding the Serial line with binary data.  Of course it won't look good when displayed as characters.  You need a program that will accept the binary data and process it into readable form before displaying it.

You may also be exceeding the speed capability of the Serial Monitor.  If you fill all the buffer space that might be the cause of your hanging.

Perhaps you should send a triggering character from the PC to get each sample.  That will throttle the output of your sketch down to a pace the PC can keep up with.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


John's completely right about flooding the serial interface. You're using the standard serial in the arduino libraries, which a complete crock.
Basically you're got a learning path to follow to do anything useful with arduino FFT.
I'm playing with a network interface and the FFT library at the moment because I need a faster thruput.

Go Up