Spectrogram Tool

Flashing LEDs is a simple but limited view of the audio frequency spectrum. To get a full view of the frequencies let's hook the hardware up to our computer and build a better spectrogram. A spectrogram is a visual representation of the frequencies in a signal--in this case the audio frequencies being output by the FFT running on the hardware.

With the spectrum program from the last page still loaded on your hardware, make sure the hardware is connected to your computer's USB port so you have a serial connection to the device. You will also want to make sure you have all the python software dependencies installed (through a distribution such as Anaconda)--go back to the software page to learn more about how to install these dependencies if necessary.

Also if you're using a distribution like Anaconda, make sure the anaconda/bin folder is in your system path before continuing. You can check this by running the command 'python --version' (without quotes) at the command prompt. You should see something like 'Python 2.7.5 :: Anaconda 1.7.0 (64-bit)' in the response.

Now run the Spectrogram.py file included in the code download. Do this by running the following command:

python Spectrogram.py

Note for Mac OSX: On Mac OSX you might need to do the following first to work around a matplotlib bug:
1. First set the QT_API variable in your terminal session to the value 'pyside' by executing:
export QT_API=pyside
2. Next start the Spectrogram.py program by executing (notice the python.app instead of python command):
python.app Spectrogram.py

You should see a window load with empty graphs on the right and controls/parameters on the left. Click the Serial Port combo box on the left to select the serial port which your hardware is connected to, and click the Open button to establish communication with the device.

Once the device is connected you should see the two graphs on the right start to display data. The top graph is a histogram of the latest frequency intensities. This is similar to what the LEDs are displaying, but showing you the full spectrum of audio frequencies. The height of the bars in the chart represent the intensity in decibels of the audio at a frequency. Rolling your mouse over the chart allows you to highlight specific bars which represent bins in the FFT output. Look in the status bar at the bottom left of the program to see the exact frequency of the selected bin.

The bottom graph is a waterfall display which shows the intensity of frequencies over time. The oldest samples are at the bottom of the graph. You can see new samples come in at the top and roll down to the bottom over time. The frequency of the signal is on the X-axis so low frequencies are to the left and high frequencies to the right. The color of each point represents the intensity of the audio, and the bar at the far right shows the scale of colors to intensity values.

Finally on the left you can see some status and controls to manipulate the hardware. The FFT size and current sample rate should be displayed in the Device Parameters group. The Graphs group has a few sliders to change the scale of intensity values in the graphs--try dragging them up and down to see how the graphs change.
The pictures on the left show a spectrogram of audio in a quiet room. You might notice straight lines running down in the waterfall graph. This appears to be noise being picked up from the LEDs. You can disable the LEDs by sending the following command to the hardware from the serial monitor in Teensyduino (make sure to close the connection in the spectrogram application first):


With the LEDs disabled you should see the bars disappear from the waterfall graph, like the second photo shows.
The spectrogram is a powerful tool we'll use in this guide to analyze audio. For now try playing some audio or making noise to see how it's represented on the graphs. For example the picture on the left is showing the spectrogram of audio from the opening of this orchestral piece.

You can see low frequencies in the 50-300hz range are quite intense. The powerful brass instruments like the trombone, trumpet, and french horns in the music are generating a lot of audio at these frequencies.

You can also see many of the intensity peaks are at evenly spaced frequencies. Some of these are harmonics generated by the instruments. For example a violin string vibrating to play a note at a specific frequency is also generating sound at integer multiples of the note's frequency. How an instrument generates harmonics contributes greatly to the timbre, or character of sound, of the instrument.
Finally, try changing the sample rate by clicking the Modify button in the Device Parameters group on the left. You can use values from ~150hz to ~9000hz. Notice as the sample rate decreases, both the range of frequencies decreases (at a rate of half the sample rate) and the amount of time to get a new sample increases (because it takes longer to fill the 256 samples for running the FFT).

At lower sample rates you can see each frequency bin is smaller and represents a tighter range of frequencies. The image on the left is audio captured at a sample rate of 250hz. This means each frequency bin from the FFT represents about 1 hz. It's interesting to see a strong line at 60hz at the bottom of the graph. This is from a 60hz vibration being picked up when the hardware is resting on my desk--I suspect the fans and hard drives in my computer are generating this noise. You can see when I picked up the device off the table there was a strong bump in intensity (red horizontal line in the middle) and then the 60hz vibration disappeared (newest samples at the top).
Continue on to learn about how to use the spectrogram to analyze and detect a sequence of tones.
This guide was first published on Oct 03, 2013. It was last updated on Oct 03, 2013. This page (Spectrogram Tool) was last updated on Sep 17, 2019.