FFT and WebSockets « My Site/Blog
Untitled Document

FFT and WebSockets

I’ve been working on a project at work that involves breaking down the input from an electret microphone and shuttling it through a WebSocket for real-time interaction on a webpage. Basically it’s an audio spectrum analyzer on an html page.

Almost every aspect of this project was new to me as I have never worked with any sort of complex algorithms or networking in any of my Arduino projects. After some glancing through some tutorials I had a basic idea of what Fast Fourier transforms (FFT) were. If you’ve taken any sort of ODE you’re probably familiar with the Laplace transforms, which can be used to transform functions from the time domain f(t) to a function of the s domain F(s) and vice-versa. Fourier transforms are similar in that they transform functions between the time and frequency domains. I won’t go into the details of how this is done however, it allows one to break apart an audio sample into its range of frequencies. In this case it is broken down into 64 values starting at 7.8 Hz with each sample having a resolution of 500 Hz.

The code that does this can be found below:

for (i=0; i < 128; i++) {                                     
    val = analogRead(0);        //electret microphone attached to A0                            
    data[i] = val/4 -128;                                       
    im[i] = 0;                                                      
}

fix_fft(data,im,7,0);

for (i=1; i< 64;i++) {       //values from 0-500Hz are trash      
    data[i] = sqrt(data[i] * data[i] + im[i] * im[i]);
}

The first for loop collects the “raw” data values to be processed. The second for starts at 1 due to a bug with the formula being used to carry out the FFT, additionally it only goes to 64 to capture the absolute value of each sample. Once I started to get actual numbers, I adapted a simple processing script to map each to a bar whose height was based on the magnitude of the number. I ended up with something that resembled spectrum analysis and decided to move on to the second part, which was to stream it in real-time.

I ended up using a WebSocket library written by github user leg0, to set up a simple test client using an Arduino Ethernet board. I then found some sample code for a WebSocket server using node.js written by github user Worlize. I quickly got a test server up and running on my laptop, which would print any incoming UTF-8 messages to the console. All that was left was to point the Arduino to my server and see whether or not my FFT values were being echoed. Sure enough, I ended up getting my FFT strings printed to the console. The final step was to rebroadcast the messages from the Arduino client to all clients connected to the server (i.e. a web browser). From there, the browser can manipulate the data in any way it wants. I must apologize for the lack of pictures and code in this post. You can see the full readme and .ino file on github.

 

Untitled Document

2 comments

  1. Mike says:

    I am working on this as well. Trying to understand the frequency range.

    I thought the range was increments of 7.8 Hz, giving the following
    Sample HZ
    data[0] 7.8
    data[1] 15.6
    data[2] 23.4
    data[3] 31.2
    data[4] 39

    can you please comment

    • c says:

      Hi Mike,

      Thanks for taking the time to read over my post. The code used to calculate the FFT is taken from the function used in an Arduino spectrum analyzer and can be found here:

      https://github.com/slytown/arduino-spectrum-analyzer

      My apologies for not including this in the original post. The comments in the ino file will hopefully clear up any confusion about the range and resolution. Please let me know if there’s anything else I can do to help.

leave a response

Untitled Document