I am trying to build the basics of an audio FFT display with Python. With the help of code from another forum post, I have managed to get something going but the resolution of the FFT display is only around 100Hz. I need the resolution to be much better than this - around a couple of Hz. I really don know how I can increase it but the reading I have done about FFT certainly suggests its possible.
Can anyone help me?
Regards
Dave
Code: Select all
#!/usr/bin/env python
import alsaaudio as aa
from time import sleep
from struct import unpack
import numpy as np
# Set up audio
sample_rate = 9600
no_channels = 1
chunk = 4096 # Use a multiple of 8
data_in = aa.PCM(aa.PCM_CAPTURE, aa.PCM_NORMAL)
data_in.setchannels(no_channels)
data_in.setrate(sample_rate)
data_in.setformat(aa.PCM_FORMAT_S16_LE)
data_in.setperiodsize(chunk)
def calculate_levels(data):
# 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
fourier=np.fft.rfft(data)
# Remove last element in array to make it the same size as chunk
fourier=np.delete(fourier,len(fourier)-1)
# Find amplitude
power = (np.log10(np.abs(fourier))**2)-5
# for i in range (0,len(fourier)):
for i in range (4,50):
for stars in range(0,int(power[i])):
print "*",
print ""
print "End"
return
while True:
# Read data
data_in.setperiodsize(chunk)
l,data = data_in.read()
calculate_levels(data)
/code]