So I was hoping I could get some help with the FFT SpaceGerbil has posted here http://www.raspberrypi.org/phpBB3/viewtopic.php?p=314087#p314087 I've only just learned Python, and haven't been able to get a chance to look over the raspberry pi side of it. I was hoping to eventually output the FFT data to an arduino...somehow.

#!/usr/bin/env python
# 8 bar Audio equaliser using MCP2307

import alsaaudio as aa
import smbus
from time import sleep
from struct import unpack
import numpy as np

bus=smbus.SMBus(0)     #Use '1' for newer Pi boards;

ADDR   = 0x20         #The I2C address of MCP23017
DIRA   = 0x00         #PortA I/O direction, by pin. 0=output, 1=input
DIRB   = 0x01         #PortB I/O direction, by pin. 0=output, 1=input
BANKA  = 0x12         #Register address for Bank A
BANKB  = 0x13         #Register address for Bank B

#Set up the 23017 for 16 output pins
bus.write_byte_data(ADDR, DIRA, 0);  #all zeros = all outputs on Bank A
bus.write_byte_data(ADDR, DIRB, 0);  #all zeros = all outputs on Bank B

def TurnOffLEDS ():
   bus.write_byte_data(ADDR, BANKA, 0xFF)  #set all columns high
   bus.write_byte_data(ADDR, BANKB, 0x00)  #set all rows low

def Set_Column(row, col):
   bus.write_byte_data(ADDR, BANKA, col)
   bus.write_byte_data(ADDR, BANKB, row)

# Initialise matrix

# Set up audio
sample_rate = 44100
no_channels = 2
chunk = 512 # Use a multiple of 8
data_in = aa.PCM(aa.PCM_CAPTURE, aa.PCM_NORMAL)

def calculate_levels(data, chunk,sample_rate):
   # Convert raw data to numpy array
   data = unpack("%dh"%(len(data)/2),data)
   data = np.array(data, dtype='h')
   # Apply FFT - real data so rfft used
   # Remove last element in array to make it the same size as chunk
   # Find amplitude
   power = np.log10(np.abs(fourier))**2
   # Araange array into 8 rows for the 8 bars on LED matrix
   power = np.reshape(power,(8,chunk/8))
   matrix= np.int_(np.average(power,axis=1)/4)
   return matrix

print "Processing....."

while True:
   # Read data from device   
   l,data = data_in.read()
   data_in.pause(1) # Pause capture whilst RPi processes data
   if l:
      # catch frame error
         matrix=calculate_levels(data, chunk,sample_rate)
         for i in range (0,8):

      except audioop.error, e:
         if e.message !="not a whole number of frames":
            raise e
   data_in.pause(0) # Resume capture

I have a few questions about it actually

1) does this output the audio again or just take it in for processing?

2) Also so that is using a USB Audio thing, do I need a specific one or will any USB Audio work?

3) Does it matter which usb the usb audio goes into?

4) since this uses the usb audio, I assume its streaming the data in as such it is playing on a laptop and then processed and output somewhere else. Is it output through HDMI or the 3.5mm jack?

1 Answer 1


You should have read comments more careful, this code does not on RasPi and is not supposed to work, actually:

Sorry - I was being dumb. I've actually done this on my laptop (running an audio file on the hard drive). The same code did not work on the RPi. Actually it did, but with blips as the FFTs were processed and the pins written to. What I did was to read a chunk of audio and then write the chunk (play) whilst simultaneously processing it. No problems on my I7 PC and my I5 laptop. A different matter on the Pi.

  • Oh whoa, how did I miss that, I've read this thing so many times. Hmm well is there any way of doing the FFT?
    – Funkyguy
    Jun 14, 2013 at 14:03
  • please, read the comments. there was a source, that uses .wav file as input and HDMI as output.
    – lenik
    Jun 14, 2013 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.