Spectrum Analyzer

Let's look at the first application of Fourier transforms by creating an audio spectrum analyzer. A spectrum analyzer is used to view the frequencies which make up a signal, like audio sampled from a microphone. Let's make the hardware visualize audio frequencies by changing the intensity of LEDs based on the intensity of audio at certain frequencies.

To get started, load the 'spectrum' sketch from the code download in Teensyduino. If necessary, adjust the AUDIO_INPUT_PIN and NEO_PIXEL_PIN variables at the top of the program to the values for your hardware. Compile and load the sketch onto your hardware.

If the sketch loaded successfully you should see the neo pixel lights start to pulse and flash based on the intensity of audio read from the microphone. Each pixel represents a different window of audio frequencies, with the first pixel representing the lowest frequencies and the last pixel representing the highest frequencies. The sample rate of the audio will determine the total range of frequencies--remember because of Nyquist's thereom only frequencies up to half the sample rate can be analyzed.

By default the spectrum program runs with a sample rate of 9000 hz and an FFT size of 256 bins. This means audio from 0 to 4500 hz can be analyzed. Each FFT result bin will represent about 35 hz of frequencies (calculated by taking sample rate divided by FFT size). The spectrum analyzer program works by assigning a range of frequencies to each LED, calculating the average intensity of the signal over those frequency ranges (by averaging the value of the FFT output bins associated with the frequency range), and lighting the LED's appropriately.

You can adjust some of the parameters to the spectrum analyzer by changing the variables and reloading the sketch, or by sending commands over the serial port to the device. For example try changing the sample rate by opening the serial monitor in Teensyduino and sending this command:

SET SAMPLE_RATE_HZ 4000;

You can also read the value of a variable by sending a get command, such as:

GET SAMPLE_RATE_HZ;

Be sure to type the command exactly, including the semicolon at the end. With a sample rate of 4000 hz you should see the LEDs pulse a little slower (a slower sample rate will take longer to fill the 256 samples necessary for running the FFT), and higher frequencies more easily light up from talking or other sounds.

Other variables you can adjust include:
  • SPECTRUM_MIN_DB - This is the minimum intensity in decibels (from 0 to 100) which corresponds to the lowest LED light intensity. Values around 20 to 30 decibels are best for audio in a quiet room.
  • SPECTRUM_MAX_DB - This is the maximum intensity in decibels which correpends to the highest LED light intensity. Values around 60-80 decibels are best, unless you're in a very loud environment.

The video below shows the spectrum display listening to powerful orchestral music with a sample rate of 4000 hz, 40 minimum decibels and 60 maximum decibels. You can see the effects of instruments playing at different frequencies and how the LEDs representing those frequencies respond.
Continue on to learn about how to better visualize the audio spectrum with your computer.
This guide was first published on Oct 03, 2013. It was last updated on Oct 03, 2013. This page (Spectrum Analyzer) was last updated on Sep 21, 2019.