Print this Post

Realtime FFT Audio Visualization with Python

I’m no stranger to visualizing linear data in the frequency-domain. Between the high definition spectrograph suite I wrote in my first year of dental school (QRSS-VD, which differentiates tones to sub-Hz resolution), to the various scripts over the years (which go into FFT imaginary number theory, linear data signal filtering with python, and real time audio graphing with wckgraph), I’ve tried dozens of combinations of techniques to capture data, analyze it, and display it with Python. Because I’m now branching into making microcontroller devices which measure and transfer analog data to a computer, I need a way to rapidly visualize data obtained in Python. Since my microcontroller device isn’t up and running yet, linear data from a PC microphone will have to do.  Here’s a quick and dirty start-to-finish project anyone can tease apart to figure out how to do some of these not-so-intuitive processes in Python. To my knowledge, this is a cross-platform solution too. For the sound card interaction, it relies on the cross-platform sound card interface library PyAudio. My python distro is 2.7 (python xy), but pythonxy doesn’t [yet] supply PyAudio.

The code behind it is a little jumbled, but it works. For recording, I wrote a class “SwhRecorder” which uses threading to continuously record audio and save it as a numpy array. When the class is loaded and started, your GUI can wait until it sees newAudio become True, then it can grab audio directly, or use fft() to pull the spectral component (which is what I do in the video). Note that my fft() relies on numpy.fft.fft(). The return is a nearly-symmetrical mirror image of the frequency components, which (get ready to cringe mathematicians) I simply split into two arrays, reverse one of them, and add together. To turn this absolute value into dB, I’d take the log10(fft) and multiply it by 20. You know, if you’re into that kind of thing, you should really check out a post I made about FFT theory and analyzing audio data in python.

Here’s the meat of the code. To run it, you should really grab the zip file at the bottom of the page. I’ll start with the recorder class:

import matplotlib
matplotlib.use('TkAgg') # THIS MAKES IT FAST!
import numpy
import scipy
import struct
import pyaudio
import threading
import pylab
import struct

class SwhRecorder:
    """Simple, cross-platform class to record from the microphone."""

    def __init__(self):
        """minimal garb is executed when class is loaded."""
        self.BUFFERSIZE=2**12 #1024 is a good buffer size

    def setup(self):
        """initialize sound card."""
        #TODO - windows detection vs. alsa or something for linux
        #TODO - try/except for sound card selection/initiation

        if self.buffersToRecord==0: self.buffersToRecord=1

        self.p = pyaudio.PyAudio()
        self.inStream = self.p.open(format=pyaudio.paInt16,channels=1,

    def close(self):
        """cleanly back out and release sound card."""

    ### RECORDING AUDIO ###  

    def getAudio(self):
        """get a single buffer size worth of audio."""
        return numpy.fromstring(audioString,dtype=numpy.int16)

    def record(self,forever=True):
        """record secToRecord seconds of audio."""
        while True:
            if self.threadsDieNow: break
            for i in range(self.chunksToRecord):
            if forever==False: break

    def continuousStart(self):
        """CALL THIS to start running forever."""
        self.t = threading.Thread(target=self.record)

    def continuousEnd(self):
        """shut down continuous recording."""

    ### MATH ###

    def downsample(self,data,mult):
        """Given 1D data, return the binned average."""
        if overhang: data=data[:-overhang]
        return data    

    def fft(self,data=None,trimBy=10,logScale=False,divBy=100):
        if data==None: 
        if logScale:
        if trimBy:
        if divBy:
        return xs,ys


    def plotAudio(self):
        """open a matplotlib popup window showing audio data."""

And now here’s the GUI launcher:

import ui_plot
import sys
import numpy
from PyQt4 import QtCore, QtGui
import PyQt4.Qwt5 as Qwt
from recorder import *

def plotSomething():
    if SR.newAudio==False: 

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    win_plot = ui_plot.QtGui.QMainWindow()
    uiplot = ui_plot.Ui_win_plot()
    #uiplot.btnB.clicked.connect(lambda: uiplot.timer.setInterval(100.0))
    #uiplot.btnC.clicked.connect(lambda: uiplot.timer.setInterval(10.0))
    #uiplot.btnD.clicked.connect(lambda: uiplot.timer.setInterval(1.0))

    uiplot.qwtPlot.setAxisScale(uiplot.qwtPlot.yLeft, 0, 1000)

    uiplot.timer = QtCore.QTimer()

    win_plot.connect(uiplot.timer, QtCore.SIGNAL('timeout()'), plotSomething) 



Note that by commenting-out the FFT line and using “c.setData(SR.xs,SR.audio)” you can plot linear PCM data to visualize sound waves like this:

Finally, here’s the zip file. It contains everything you need to run the program on your own computer (including the UI scripts which are not written on this page)


If you make a cool project based on this one, I’d love to hear about it. Good luck!


About the author

Scott W Harden

Scott Harden has had a lifelong passion for computer programming and electrical engineering, and recently has become interested in its relationship with biomolecular sciences. He has run a personal website since he was 15, which has changed names from HardenTechnologies.com, to KnightHacker.com, to ScottIsHot.com, to its current SWHarden.com. Scott has been in college for 10 years, with 3 more years to go. He has an AA in Biology (Valencia College), BS in Cell Biology (Union University), MS in Molecular Biology and Microbiology (University of Central Florida), and is currently in a combined DMD (doctor of dental medicine) / PhD (neuroscience) program through the collaboration of the College of Dentistry and College of Medicine (Interdisciplinary Program in Biomedical Science, IDP) at the University of Florida in Gainesville, Florida. In his spare time Scott builds small electrical devices (with an emphasis on radio frequency) and enjoys writing cross-platform open-source software.

Permanent link to this article: http://www.SWHarden.com/blog/2013-05-09-realtime-fft-audio-visualization-with-python/


  1. Mike Seese

    Cheers Scott! I was about to start programming something in C++ or Java, did a Google search, and you were on top! Curious as to what your big picture is here. I’m using it for some theories behind voice recognition.

  2. r3c4ll

    hi Scott, i try to run the realTimeAudio.py but obtain an error:

    Traceback (most recent call last):
    File “realTimeAudio.py”, line 1, in
    import ui_plot
    File “/home/r3c4ll/source/python/SWHRecorder/ui_plot.py”, line 56, in
    from PyQt4 import Qwt5
    ImportError: cannot import name Qwt5

    I’m running on a Debian 7. Could you please help me?

    1. Anonymous

      Try apt-get install python-qwt5-qt4

  3. Hmm

    “I simply split into two arrays, reverse one of them, and add together.”

    Wait, what? If the halves are identical, this does nothing but double your numbers. Why do that?

    If your signal is real, use numpy.fft.rfft, which is faster, too.

  4. Diego

    Hi Scott, your work is amazing!! Do you know hao to do the same but with an external sound card pluged it to a USB port??
    Thank you!

  5. Jake

    Hello Scott,

    I want to truly thank you for this post — It’s almost exactly what I’ve wanted to achieve (and read about) for a long time now, but my mathematical abilities are not up to par enough to tackle it myself. This code and post have been very informative, and will be very useful probably to many, definitely to myself, so thanks again.

  6. Richard

    hear is a tip use numpy.fft.rfft to compute the fft on real component data. It will give you n/2+1 complex variables for an even sized real array, though the two end components will be completely real as well.

  7. Anonymous

    I think this is better:

    ys = numpy.abs(numpy.fft.rfft(data))

  8. Chunwei

    hi friends:
    your this essay is very helpful,but i have one question about it, i want use your recorder file display by Tkinter, try two days alredy.
    i am quite new about python, can you just sent me some example code about that?
    display the curve by tkinter :)))
    think you very much

  9. dktak

    Hi Scott,

    It looks very interesting. But as Diego’s question, how would you use a USB external sound-card ?? I think it is somewhere around the “TODO” area, you would set some connection-informations.

  10. reinhard fuchs

    Hi Scott,
    Do you know hao to do the same but with an external sound card pluged it to a USB port??
    I have this same question as DIEGO.
    Did you reply.
    If ,could i get the answer ?
    Mit freundlichem Gruss Reinhard

  11. dktak

    Got it to work with an external sound card. Have played around with AlsaMixer, and after figuring out, that I had to launch realTimeAudio.py it worked.

    1. born2freedom


      Can you give some details of how you got it working with the USB soundcard?

      I am trying to modify the code to use on RaspberryPi and Beaglebone black.using Debian wheezey

      1. matt

        Hi born2freedom,

        have you firgured out how to run the program on a Raspberry Pi?
        I’m sitting here trying the same…


  12. Wael

    well done, Scott

  13. Matt


    Great program Scott. Just wondering if anybody as managed to get the program to work with a USB soundcard.
    If so how??

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>